From 1c53371598bd438bd16e3309aa89918c553a8a55 Mon Sep 17 00:00:00 2001 From: Toastie Date: Wed, 28 Aug 2024 19:57:33 +1200 Subject: [PATCH] unkept leave now has a configurable delay --- .../DangerousCommands/CleanupCommands.cs | 4 ++-- .../DangerousCommands/CleanupService.cs | 19 ++++++++----------- .../_common/ICleanupService.cs | 2 +- .../data/strings/commands/commands.en-US.yml | 5 ++++- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/EllieBot/Modules/Administration/DangerousCommands/CleanupCommands.cs b/src/EllieBot/Modules/Administration/DangerousCommands/CleanupCommands.cs index 55deace..0d282d0 100644 --- a/src/EllieBot/Modules/Administration/DangerousCommands/CleanupCommands.cs +++ b/src/EllieBot/Modules/Administration/DangerousCommands/CleanupCommands.cs @@ -42,7 +42,7 @@ public partial class Administration [Cmd] [OwnerOnly] - public async Task LeaveUnkeptServers(int shardId = 0) + public async Task LeaveUnkeptServers(int shardId = 0, int delay = 1000) { var keptGuildCount = await _svc.GetKeptGuildCount(); @@ -58,7 +58,7 @@ public partial class Administration if (!response) return; - await _svc.LeaveUnkeptServers(shardId); + await _svc.LeaveUnkeptServers(shardId, delay); await ctx.OkAsync(); } } diff --git a/src/EllieBot/Modules/Administration/DangerousCommands/CleanupService.cs b/src/EllieBot/Modules/Administration/DangerousCommands/CleanupService.cs index dc84027..4e01f4b 100644 --- a/src/EllieBot/Modules/Administration/DangerousCommands/CleanupService.cs +++ b/src/EllieBot/Modules/Administration/DangerousCommands/CleanupService.cs @@ -2,10 +2,8 @@ using LinqToDB.Data; using LinqToDB.EntityFrameworkCore; using LinqToDB.Mapping; -using LinqToDB.Tools; using EllieBot.Common.ModuleBehaviors; using EllieBot.Db.Models; -using System.Security.Cryptography; namespace EllieBot.Modules.Administration.DangerousCommands; @@ -14,7 +12,7 @@ public sealed class CleanupService : ICleanupService, IReadyExecutor, IEService private TypedKey _cleanupReportKey = new("cleanup:report"); private TypedKey _cleanupTriggerKey = new("cleanup:trigger"); - private TypedKey _keepTriggerKey = new("keep:trigger"); + private TypedKey<(int, int)> _keepTriggerKey = new("keep:trigger"); private readonly IPubSub _pubSub; private readonly DiscordSocketClient _client; @@ -47,19 +45,19 @@ public sealed class CleanupService : ICleanupService, IReadyExecutor, IEService private bool keepTriggered = false; - private async ValueTask InternalTriggerKeep(int shardId) + private async ValueTask InternalTriggerKeep((int shardId, int delay) data) { + var (shardId, delay) = data; + if (_client.ShardId != shardId) return; - + if (keepTriggered) return; keepTriggered = true; try { - await Task.Delay(10 + (10 * _client.ShardId)); - var allGuildIds = _client.Guilds.Select(x => x.Id); HashSet dontDelete; @@ -82,8 +80,7 @@ public sealed class CleanupService : ICleanupService, IReadyExecutor, IEService if (dontDelete.Contains(guildId)) continue; - // 1 leave per 20 seconds per shard - await Task.Delay(500); + await Task.Delay(delay); SocketGuild? guild = null; try @@ -239,8 +236,8 @@ public sealed class CleanupService : ICleanupService, IReadyExecutor, IEService return await table.CountAsync(); } - public async Task LeaveUnkeptServers(int shardId) - => await _pubSub.Pub(_keepTriggerKey, shardId); + public async Task LeaveUnkeptServers(int shardId, int delay) + => await _pubSub.Pub(_keepTriggerKey, (shardId, delay)); private ValueTask OnKeepReport(KeepReport report) { diff --git a/src/EllieBot/Modules/Administration/DangerousCommands/_common/ICleanupService.cs b/src/EllieBot/Modules/Administration/DangerousCommands/_common/ICleanupService.cs index 318197c..d2e4add 100644 --- a/src/EllieBot/Modules/Administration/DangerousCommands/_common/ICleanupService.cs +++ b/src/EllieBot/Modules/Administration/DangerousCommands/_common/ICleanupService.cs @@ -5,5 +5,5 @@ public interface ICleanupService Task DeleteMissingGuildDataAsync(); Task KeepGuild(ulong guildId); Task GetKeptGuildCount(); - Task LeaveUnkeptServers(int shardId); + Task LeaveUnkeptServers(int shardId, int delay); } \ No newline at end of file diff --git a/src/EllieBot/data/strings/commands/commands.en-US.yml b/src/EllieBot/data/strings/commands/commands.en-US.yml index 912ca40..ccfb54d 100644 --- a/src/EllieBot/data/strings/commands/commands.en-US.yml +++ b/src/EllieBot/data/strings/commands/commands.en-US.yml @@ -4562,4 +4562,7 @@ leaveunkeptservers: ex: - '' params: - - { } \ No newline at end of file + - shardId: + desc: "Shard id from which to start leaving unkept servers." + - delay: + desc: "Delay in miliseconds between leaves" \ No newline at end of file