Updated Administration module

This commit is contained in:
Toastie (DCS Team) 2024-06-27 18:23:14 +12:00
parent eb1ebc72db
commit b8fa164f8d
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
8 changed files with 123 additions and 33 deletions

View file

@ -61,17 +61,66 @@ public sealed class CleanupService : ICleanupService, IReadyExecutor, IEService
})); }));
} }
// delete guild configs
await ctx.GetTable<GuildConfig>() await ctx.GetTable<GuildConfig>()
.Where(x => !tempTable.Select(x => x.GuildId) .Where(x => !tempTable.Select(x => x.GuildId)
.Contains(x.GuildId)) .Contains(x.GuildId))
.DeleteAsync(); .DeleteAsync();
// delete guild xp
await ctx.GetTable<UserXpStats>() await ctx.GetTable<UserXpStats>()
.Where(x => !tempTable.Select(x => x.GuildId) .Where(x => !tempTable.Select(x => x.GuildId)
.Contains(x.GuildId)) .Contains(x.GuildId))
.DeleteAsync(); .DeleteAsync();
// delete expressions
await ctx.GetTable<EllieExpression>()
.Where(x => x.GuildId != null && !tempTable.Select(x => x.GuildId)
.Contains(x.GuildId.Value))
.DeleteAsync();
// delete quotes
await ctx.GetTable<Quote>()
.Where(x => !tempTable.Select(x => x.GuildId)
.Contains(x.GuildId))
.DeleteAsync();
// delete planted currencies
await ctx.GetTable<PlantedCurrency>()
.Where(x => !tempTable.Select(x => x.GuildId)
.Contains(x.GuildId))
.DeleteAsync();
// delete image only channels
await ctx.GetTable<ImageOnlyChannel>()
.Where(x => !tempTable.Select(x => x.GuildId)
.Contains(x.GuildId))
.DeleteAsync();
// delete reaction roles
await ctx.GetTable<ReactionRoleV2>()
.Where(x => !tempTable.Select(x => x.GuildId)
.Contains(x.GuildId))
.DeleteAsync();
// delete ignored users
await ctx.GetTable<DiscordPermOverride>()
.Where(x => x.GuildId != null && !tempTable.Select(x => x.GuildId)
.Contains(x.GuildId.Value))
.DeleteAsync();
// delete perm overrides
await ctx.GetTable<DiscordPermOverride>()
.Where(x => x.GuildId != null && !tempTable.Select(x => x.GuildId)
.Contains(x.GuildId.Value))
.DeleteAsync();
// delete repeaters
await ctx.GetTable<Repeater>()
.Where(x => !tempTable.Select(x => x.GuildId)
.Contains(x.GuildId))
.DeleteAsync();
return new() return new()
{ {
GuildCount = guildIds.Keys.Count, GuildCount = guildIds.Keys.Count,

View file

@ -293,7 +293,7 @@ public class MuteService : IEService
{ {
ArgumentNullException.ThrowIfNull(guild); ArgumentNullException.ThrowIfNull(guild);
const string defaultMuteRoleName = "ellie-mute"; const string defaultMuteRoleName = "nadeko-mute";
var muteRoleName = GuildMuteRoles.GetOrAdd(guild.Id, defaultMuteRoleName); var muteRoleName = GuildMuteRoles.GetOrAdd(guild.Id, defaultMuteRoleName);

View file

@ -45,23 +45,43 @@ public partial class Administration
var progressMsg = await Response().Pending(strs.prune_progress(0, 100)).SendAsync(); var progressMsg = await Response().Pending(strs.prune_progress(0, 100)).SendAsync();
var progress = GetProgressTracker(progressMsg); var progress = GetProgressTracker(progressMsg);
PruneResult result;
if (opts.Safe) if (opts.Safe)
await _service.PruneWhere((ITextChannel)ctx.Channel, result = await _service.PruneWhere((ITextChannel)ctx.Channel,
100, 100,
x => x.Author.Id == user.Id && !x.IsPinned, x => x.Author.Id == user.Id && !x.IsPinned,
progress, progress,
opts.After); opts.After);
else else
await _service.PruneWhere((ITextChannel)ctx.Channel, result = await _service.PruneWhere((ITextChannel)ctx.Channel,
100, 100,
x => x.Author.Id == user.Id, x => x.Author.Id == user.Id,
progress, progress,
opts.After); opts.After);
ctx.Message.DeleteAfter(3); ctx.Message.DeleteAfter(3);
await SendResult(result);
await progressMsg.DeleteAsync(); await progressMsg.DeleteAsync();
} }
private async Task SendResult(PruneResult result)
{
switch (result)
{
case PruneResult.Success:
break;
case PruneResult.AlreadyRunning:
break;
case PruneResult.FeatureLimit:
await Response().Pending(strs.feature_limit_reached_owner).SendAsync();
break;
default:
throw new ArgumentOutOfRangeException(nameof(result), result, null);
}
}
// prune x // prune x
[Cmd] [Cmd]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
@ -83,19 +103,21 @@ public partial class Administration
var progressMsg = await Response().Pending(strs.prune_progress(0, count)).SendAsync(); var progressMsg = await Response().Pending(strs.prune_progress(0, count)).SendAsync();
var progress = GetProgressTracker(progressMsg); var progress = GetProgressTracker(progressMsg);
PruneResult result;
if (opts.Safe) if (opts.Safe)
await _service.PruneWhere((ITextChannel)ctx.Channel, result = await _service.PruneWhere((ITextChannel)ctx.Channel,
count, count,
x => !x.IsPinned && x.Id != progressMsg.Id, x => !x.IsPinned && x.Id != progressMsg.Id,
progress, progress,
opts.After); opts.After);
else else
await _service.PruneWhere((ITextChannel)ctx.Channel, result = await _service.PruneWhere((ITextChannel)ctx.Channel,
count, count,
x => x.Id != progressMsg.Id, x => x.Id != progressMsg.Id,
progress, progress,
opts.After); opts.After);
await SendResult(result);
await progressMsg.DeleteAsync(); await progressMsg.DeleteAsync();
} }
@ -155,9 +177,10 @@ public partial class Administration
var progressMsg = await Response().Pending(strs.prune_progress(0, count)).SendAsync(); var progressMsg = await Response().Pending(strs.prune_progress(0, count)).SendAsync();
var progress = GetProgressTracker(progressMsg); var progress = GetProgressTracker(progressMsg);
PruneResult result;
if (opts.Safe) if (opts.Safe)
{ {
await _service.PruneWhere((ITextChannel)ctx.Channel, result = await _service.PruneWhere((ITextChannel)ctx.Channel,
count, count,
m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks && !m.IsPinned, m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks && !m.IsPinned,
progress, progress,
@ -166,7 +189,7 @@ public partial class Administration
} }
else else
{ {
await _service.PruneWhere((ITextChannel)ctx.Channel, result = await _service.PruneWhere((ITextChannel)ctx.Channel,
count, count,
m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks, m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks,
progress, progress,
@ -174,6 +197,7 @@ public partial class Administration
); );
} }
await SendResult(result);
await progressMsg.DeleteAsync(); await progressMsg.DeleteAsync();
} }

View file

@ -0,0 +1,9 @@
#nullable disable
namespace EllieBot.Modules.Administration.Services;
public enum PruneResult
{
Success,
AlreadyRunning,
FeatureLimit,
}

View file

@ -1,4 +1,6 @@
#nullable disable #nullable disable
using EllieBot.Modules.Patronage;
namespace EllieBot.Modules.Administration.Services; namespace EllieBot.Modules.Administration.Services;
public class PruneService : IEService public class PruneService : IEService
@ -7,11 +9,15 @@ public class PruneService : IEService
private readonly ConcurrentDictionary<ulong, CancellationTokenSource> _pruningGuilds = new(); private readonly ConcurrentDictionary<ulong, CancellationTokenSource> _pruningGuilds = new();
private readonly TimeSpan _twoWeeks = TimeSpan.FromDays(14); private readonly TimeSpan _twoWeeks = TimeSpan.FromDays(14);
private readonly ILogCommandService _logService; private readonly ILogCommandService _logService;
private readonly IPatronageService _ps;
public PruneService(ILogCommandService logService) public PruneService(ILogCommandService logService, IPatronageService ps)
=> _logService = logService; {
_logService = logService;
_ps = ps;
}
public async Task PruneWhere( public async Task<PruneResult> PruneWhere(
ITextChannel channel, ITextChannel channel,
int amount, int amount,
Func<IMessage, bool> predicate, Func<IMessage, bool> predicate,
@ -26,8 +32,13 @@ public class PruneService : IEService
using var cancelSource = new CancellationTokenSource(); using var cancelSource = new CancellationTokenSource();
if (!_pruningGuilds.TryAdd(channel.GuildId, cancelSource)) if (!_pruningGuilds.TryAdd(channel.GuildId, cancelSource))
return; return PruneResult.AlreadyRunning;
if (!await _ps.LimitHitAsync(LimitedFeatureName.Prune, channel.Guild.OwnerId))
{
return PruneResult.FeatureLimit;
}
try try
{ {
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
@ -47,7 +58,7 @@ public class PruneService : IEService
.ToArray(); .ToArray();
if (!msgs.Any()) if (!msgs.Any())
return; return PruneResult.Success;
lastMessage = msgs[^1]; lastMessage = msgs[^1];
@ -88,6 +99,8 @@ public class PruneService : IEService
{ {
_pruningGuilds.TryRemove(channel.GuildId, out _); _pruningGuilds.TryRemove(channel.GuildId, out _);
} }
return PruneResult.Success;
} }
public async Task<bool> CancelAsync(ulong guildId) public async Task<bool> CancelAsync(ulong guildId)

View file

@ -18,7 +18,7 @@ public interface IReactionRoleService
/// <param name="group"></param> /// <param name="group"></param>
/// <param name="levelReq"></param> /// <param name="levelReq"></param>
/// <returns>The result of the operation</returns> /// <returns>The result of the operation</returns>
Task<OneOf<Success, FeatureLimit>> AddReactionRole( Task<OneOf<Success, Error>> AddReactionRole(
IGuild guild, IGuild guild,
IMessage msg, IMessage msg,
string emote, string emote,

View file

@ -55,12 +55,10 @@ public partial class Administration
await res.Match( await res.Match(
_ => ctx.OkAsync(), _ => ctx.OkAsync(),
fl => async fl =>
{ {
_ = msg.RemoveReactionAsync(emote, ctx.Client.CurrentUser); _ = msg.RemoveReactionAsync(emote, ctx.Client.CurrentUser);
return !fl.IsPatronLimit await Response().Pending(strs.feature_limit_reached_owner).SendAsync();
? Response().Error(strs.limit_reached(fl.Quota)).SendAsync()
: Response().Pending(strs.feature_limit_reached_owner(fl.Quota, fl.Name)).SendAsync();
}); });
} }

View file

@ -21,22 +21,16 @@ public sealed class ReactionRolesService : IReadyExecutor, IEService, IReactionR
private readonly SemaphoreSlim _assignementLock = new(1, 1); private readonly SemaphoreSlim _assignementLock = new(1, 1);
private readonly IPatronageService _ps; private readonly IPatronageService _ps;
private static readonly FeatureLimitKey _reroFLKey = new()
{
Key = "rero:max_count",
PrettyName = "Reaction Role"
};
public ReactionRolesService( public ReactionRolesService(
DiscordSocketClient client, DiscordSocketClient client,
IPatronageService ps,
DbService db, DbService db,
IBotCredentials creds, IBotCredentials creds)
IPatronageService ps)
{ {
_db = db; _db = db;
_ps = ps;
_client = client; _client = client;
_creds = creds; _creds = creds;
_ps = ps;
_cache = new(); _cache = new();
} }
@ -242,7 +236,7 @@ public sealed class ReactionRolesService : IReadyExecutor, IEService, IReactionR
/// <param name="group"></param> /// <param name="group"></param>
/// <param name="levelReq"></param> /// <param name="levelReq"></param>
/// <returns>The result of the operation</returns> /// <returns>The result of the operation</returns>
public async Task<OneOf<Success, FeatureLimit>> AddReactionRole( public async Task<OneOf<Success, Error>> AddReactionRole(
IGuild guild, IGuild guild,
IMessage msg, IMessage msg,
string emote, string emote,
@ -261,9 +255,12 @@ public sealed class ReactionRolesService : IReadyExecutor, IEService, IReactionR
.Where(x => x.GuildId == guild.Id) .Where(x => x.GuildId == guild.Id)
.CountAsync(); .CountAsync();
var result = await _ps.TryGetFeatureLimitAsync(_reroFLKey, guild.OwnerId, 50); var limit = await _ps.GetUserLimit(LimitedFeatureName.ReactionRole, guild.OwnerId);
if (result.Quota != -1 && activeReactionRoles >= result.Quota)
return result; if (!_creds.IsOwner(guild.OwnerId) && (activeReactionRoles >= limit.Quota && limit.Quota >= 0))
{
return new Error();
}
await ctx.GetTable<ReactionRoleV2>() await ctx.GetTable<ReactionRoleV2>()
.InsertOrUpdateAsync(() => new() .InsertOrUpdateAsync(() => new()