1
Fork 0
mirror of https://gitlab.com/Kwoth/nadekobot.git synced 2024-10-02 20:13:13 +00:00

change: unkept leave now has a configurable delay

This commit is contained in:
Kwoth 2024-08-28 02:22:58 +00:00
parent 52b87c7776
commit c9ed2cf4b5
4 changed files with 15 additions and 15 deletions

View file

@ -42,7 +42,7 @@ public partial class Administration
[Cmd] [Cmd]
[OwnerOnly] [OwnerOnly]
public async Task LeaveUnkeptServers(int shardId = 0) public async Task LeaveUnkeptServers(int shardId = 0, int delay = 1000)
{ {
var keptGuildCount = await _svc.GetKeptGuildCount(); var keptGuildCount = await _svc.GetKeptGuildCount();
@ -58,7 +58,7 @@ public partial class Administration
if (!response) if (!response)
return; return;
await _svc.LeaveUnkeptServers(shardId); await _svc.LeaveUnkeptServers(shardId, delay);
await ctx.OkAsync(); await ctx.OkAsync();
} }
} }

View file

@ -2,10 +2,8 @@
using LinqToDB.Data; using LinqToDB.Data;
using LinqToDB.EntityFrameworkCore; using LinqToDB.EntityFrameworkCore;
using LinqToDB.Mapping; using LinqToDB.Mapping;
using LinqToDB.Tools;
using NadekoBot.Common.ModuleBehaviors; using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Db.Models; using NadekoBot.Db.Models;
using System.Security.Cryptography;
namespace NadekoBot.Modules.Administration.DangerousCommands; namespace NadekoBot.Modules.Administration.DangerousCommands;
@ -14,7 +12,7 @@ public sealed class CleanupService : ICleanupService, IReadyExecutor, INService
private TypedKey<KeepReport> _cleanupReportKey = new("cleanup:report"); private TypedKey<KeepReport> _cleanupReportKey = new("cleanup:report");
private TypedKey<bool> _cleanupTriggerKey = new("cleanup:trigger"); private TypedKey<bool> _cleanupTriggerKey = new("cleanup:trigger");
private TypedKey<int> _keepTriggerKey = new("keep:trigger"); private TypedKey<(int, int)> _keepTriggerKey = new("keep:trigger");
private readonly IPubSub _pubSub; private readonly IPubSub _pubSub;
private readonly DiscordSocketClient _client; private readonly DiscordSocketClient _client;
@ -47,19 +45,19 @@ public sealed class CleanupService : ICleanupService, IReadyExecutor, INService
private bool keepTriggered = false; 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) if (_client.ShardId != shardId)
return; return;
if (keepTriggered) if (keepTriggered)
return; return;
keepTriggered = true; keepTriggered = true;
try try
{ {
await Task.Delay(10 + (10 * _client.ShardId));
var allGuildIds = _client.Guilds.Select(x => x.Id); var allGuildIds = _client.Guilds.Select(x => x.Id);
HashSet<ulong> dontDelete; HashSet<ulong> dontDelete;
@ -82,8 +80,7 @@ public sealed class CleanupService : ICleanupService, IReadyExecutor, INService
if (dontDelete.Contains(guildId)) if (dontDelete.Contains(guildId))
continue; continue;
// 1 leave per 20 seconds per shard await Task.Delay(delay);
await Task.Delay(500);
SocketGuild? guild = null; SocketGuild? guild = null;
try try
@ -239,8 +236,8 @@ public sealed class CleanupService : ICleanupService, IReadyExecutor, INService
return await table.CountAsync(); return await table.CountAsync();
} }
public async Task LeaveUnkeptServers(int shardId) public async Task LeaveUnkeptServers(int shardId, int delay)
=> await _pubSub.Pub(_keepTriggerKey, shardId); => await _pubSub.Pub(_keepTriggerKey, (shardId, delay));
private ValueTask OnKeepReport(KeepReport report) private ValueTask OnKeepReport(KeepReport report)
{ {

View file

@ -5,5 +5,5 @@ public interface ICleanupService
Task<KeepResult?> DeleteMissingGuildDataAsync(); Task<KeepResult?> DeleteMissingGuildDataAsync();
Task<bool> KeepGuild(ulong guildId); Task<bool> KeepGuild(ulong guildId);
Task<int> GetKeptGuildCount(); Task<int> GetKeptGuildCount();
Task LeaveUnkeptServers(int shardId); Task LeaveUnkeptServers(int shardId, int delay);
} }

View file

@ -4562,4 +4562,7 @@ leaveunkeptservers:
ex: ex:
- '' - ''
params: params:
- { } - shardId:
desc: "Shard id from which to start leaving unkept servers."
- delay:
desc: "Delay in miliseconds between leaves"