Added .repeatskip

This commit is contained in:
Kwoth
2022-07-26 16:56:33 +02:00
parent 0f8291c589
commit e20212a6cb
6 changed files with 66 additions and 4 deletions

View File

@@ -1,7 +1,6 @@
using LinqToDB;
using LinqToDB.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Nadeko.Common;
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Services.Database.Models;
@@ -16,6 +15,7 @@ public sealed class RepeaterService : IReadyExecutor, INService
private readonly DiscordSocketClient _client;
private readonly LinkedList<RunningRepeater> _repeaterQueue;
private readonly ConcurrentHashSet<int> _noRedundant;
private readonly ConcurrentHashSet<int> _skipNext = new();
private readonly object _queueLocker = new();
@@ -82,7 +82,7 @@ public sealed class RepeaterService : IReadyExecutor, INService
// execute
foreach (var chunk in toExecute.Chunk(5))
await chunk.Select(Trigger).WhenAll();
await chunk.Where(x => !_skipNext.TryRemove(x.Repeater.Id)).Select(Trigger).WhenAll();
// reinsert
foreach (var rep in toExecute)
@@ -396,6 +396,27 @@ public sealed class RepeaterService : IReadyExecutor, INService
return newValue;
}
public async Task<bool?> ToggleSkipNextAsync(ulong guildId, int index)
{
await using var ctx = _db.GetDbContext();
var toSkip = await ctx.Repeaters
.Where(x => x.GuildId == guildId)
.Skip(index)
.FirstOrDefaultAsyncEF();
if (toSkip is null)
return null;
if (_skipNext.Add(toSkip.Id))
return true;
_skipNext.TryRemove(toSkip.Id);
return false;
}
public bool IsNoRedundant(int repeaterId)
=> _noRedundant.Contains(repeaterId);
public bool IsRepeaterSkipped(int repeaterId)
=> _skipNext.Contains(repeaterId);
}