From e20212a6cbeaaf4e6b3aa7960b320d224d10599b Mon Sep 17 00:00:00 2001 From: Kwoth Date: Tue, 26 Jul 2022 16:56:33 +0200 Subject: [PATCH] Added .repeatskip --- .../Common/Attributes/NadekoCommand.cs | 2 +- .../Utility/Repeater/RepeatCommands.cs | 28 ++++++++++++++++++- .../Utility/Repeater/RepeaterService.cs | 25 +++++++++++++++-- src/NadekoBot/data/aliases.yml | 3 ++ .../data/strings/commands/commands.en-US.yml | 10 +++++++ .../strings/responses/responses.en-US.json | 2 ++ 6 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/NadekoBot/Common/Attributes/NadekoCommand.cs b/src/NadekoBot/Common/Attributes/NadekoCommand.cs index 17389d841..39162ecc4 100644 --- a/src/NadekoBot/Common/Attributes/NadekoCommand.cs +++ b/src/NadekoBot/Common/Attributes/NadekoCommand.cs @@ -13,6 +13,6 @@ public sealed class CmdAttribute : CommandAttribute MethodName = memberName.ToLowerInvariant(); Aliases = CommandNameLoadHelper.GetAliasesFor(memberName); Remarks = memberName.ToLowerInvariant(); - // Summary = memberName.ToLowerInvariant(); + Summary = memberName.ToLowerInvariant(); } } \ No newline at end of file diff --git a/src/NadekoBot/Modules/Utility/Repeater/RepeatCommands.cs b/src/NadekoBot/Modules/Utility/Repeater/RepeatCommands.cs index ddb9352b6..d4fc0a392 100644 --- a/src/NadekoBot/Modules/Utility/Repeater/RepeatCommands.cs +++ b/src/NadekoBot/Modules/Utility/Repeater/RepeatCommands.cs @@ -9,6 +9,32 @@ public partial class Utility [Group] public partial class RepeatCommands : NadekoModule { + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageMessages)] + public async Task RepeatSkip(int index) + { + if (--index < 0) + return; + + var result = await _service.ToggleSkipNextAsync(ctx.Guild.Id, index); + + if (result is null) + { + await ReplyErrorLocalizedAsync(strs.index_out_of_range); + return; + } + + if (result is true) + { + await ReplyConfirmLocalizedAsync(strs.repeater_skip_next); + } + else + { + await ReplyConfirmLocalizedAsync(strs.repeater_dont_skip_next); + } + } + [Cmd] [RequireContext(ContextType.Guild)] [UserPerm(GuildPerm.ManageMessages)] @@ -149,7 +175,7 @@ public partial class Utility foreach (var runner in repeaters.OrderBy(r => r.Repeater.Id)) { var description = GetRepeaterInfoString(runner); - var name = $"#`{++i}`"; + var name = $"#`{++i}` {(_service.IsRepeaterSkipped(runner.Repeater.Id) ? "🦘" : "")}"; embed.AddField(name, description); } diff --git a/src/NadekoBot/Modules/Utility/Repeater/RepeaterService.cs b/src/NadekoBot/Modules/Utility/Repeater/RepeaterService.cs index a87ff5f0d..9fe259ae4 100644 --- a/src/NadekoBot/Modules/Utility/Repeater/RepeaterService.cs +++ b/src/NadekoBot/Modules/Utility/Repeater/RepeaterService.cs @@ -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 _repeaterQueue; private readonly ConcurrentHashSet _noRedundant; + private readonly ConcurrentHashSet _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 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); } \ No newline at end of file diff --git a/src/NadekoBot/data/aliases.yml b/src/NadekoBot/data/aliases.yml index a8de619e3..3f6354b74 100644 --- a/src/NadekoBot/data/aliases.yml +++ b/src/NadekoBot/data/aliases.yml @@ -58,6 +58,9 @@ repeat: repeatredundant: - repeatredun - repred +repeatskip: + - repeatskip + - repski rotateplaying: - rotateplaying - ropl diff --git a/src/NadekoBot/data/strings/commands/commands.en-US.yml b/src/NadekoBot/data/strings/commands/commands.en-US.yml index 60fc26147..8183d8fb6 100644 --- a/src/NadekoBot/data/strings/commands/commands.en-US.yml +++ b/src/NadekoBot/data/strings/commands/commands.en-US.yml @@ -128,6 +128,10 @@ repeatredundant: desc: "Specify repeater's index (use `{0}repli` to find it) to toggle whether that repeater's message should be reposted if the last message in the channel is the same repeater's message. This is useful if you want to remind everyone to be nice in the channel every so often, but don't want to have the bot spam the channel. This is NOT useful if you want to periodically ping someone." args: - "1" +repeatskip: + desc: "Specify a repeater's ID to toggle whether the next trigger of the repeater will be skipped. This setting is not stored in the database and will get reset if the bot is restarted." + args: + - "3" rotateplaying: desc: "Toggles rotation of playing status of the dynamic strings you previously specified." args: @@ -2162,6 +2166,12 @@ imageonlychannel: Users who send more than a few non-image messages will be banned from using the channel. args: - "" +linksonlychannel: + desc: |- + Toggles whether the channel only allows links. + Users who send more than a few non-link messages will be banned from using the channel. + args: + - "" coordreload: desc: "Reloads coordinator config" args: diff --git a/src/NadekoBot/data/strings/responses/responses.en-US.json b/src/NadekoBot/data/strings/responses/responses.en-US.json index f7d8af042..a32b848f4 100644 --- a/src/NadekoBot/data/strings/responses/responses.en-US.json +++ b/src/NadekoBot/data/strings/responses/responses.en-US.json @@ -571,6 +571,8 @@ "repeater_removed": "Repeater #{0} Removed", "repeater_exceed_limit": "You cannot have more than {0} repeaters per server.", "repeater_remove_fail": "Failed removing repeater on that index. Either you've specified invalid index, or repeater was in executing state at that time, in which case, try again in a few seconds.", + "repeater_skip_next": "Next trigger of this repeater will be skipped.", + "repeater_dont_skip_next": "Next trigger of this repeater won't be skipped.", "interval_colon": "Interval:", "executes_in_colon": "Executes in:", "message_colon": "Message:",