From 67469b285e58dd8928875dfeeb1fbc438c6e1b76 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 1 Sep 2022 23:04:38 +0100 Subject: [PATCH] Query message deletion count from MessageDeletionService (#55) --- .../Infractions/InfractionCommand.Stats.cs | 2 +- Hammer/Commands/Infractions/InfractionCommand.cs | 3 +++ Hammer/Services/MessageDeletionService.cs | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Hammer/Commands/Infractions/InfractionCommand.Stats.cs b/Hammer/Commands/Infractions/InfractionCommand.Stats.cs index 69d1757..053c0cb 100644 --- a/Hammer/Commands/Infractions/InfractionCommand.Stats.cs +++ b/Hammer/Commands/Infractions/InfractionCommand.Stats.cs @@ -38,7 +38,7 @@ public async Task StatsAsync(InteractionContext context) embed.AddField("Bans", infractions.Count(i => i.Type is InfractionType.TemporaryBan or InfractionType.Ban).ToString("N0"), true); embed.AddField("Kicks", infractions.Count(i => i.Type is InfractionType.Kick).ToString("N0"), true); embed.AddField("Gags", infractions.Count(i => i.Type is InfractionType.Gag).ToString("N0"), true); - embed.AddField("Messages Deleted", infractions.Count(i => i.Type is InfractionType.MessageDeletion).ToString("N0"), true); + embed.AddField("Messages Deleted", (await _messageDeletionService.CountMessageDeletionsAsync(context.Guild)).ToString("N0"), true); } var builder = new DiscordWebhookBuilder(); diff --git a/Hammer/Commands/Infractions/InfractionCommand.cs b/Hammer/Commands/Infractions/InfractionCommand.cs index 42720cb..3ddf94a 100644 --- a/Hammer/Commands/Infractions/InfractionCommand.cs +++ b/Hammer/Commands/Infractions/InfractionCommand.cs @@ -11,6 +11,7 @@ internal sealed partial class InfractionCommand : ApplicationCommandModule { private readonly ConfigurationService _configurationService; private readonly InfractionService _infractionService; + private readonly MessageDeletionService _messageDeletionService; private readonly RuleService _ruleService; /// @@ -19,11 +20,13 @@ internal sealed partial class InfractionCommand : ApplicationCommandModule public InfractionCommand( ConfigurationService configurationService, InfractionService infractionService, + MessageDeletionService messageDeletionService, RuleService ruleService ) { _configurationService = configurationService; _infractionService = infractionService; + _messageDeletionService = messageDeletionService; _ruleService = ruleService; } } diff --git a/Hammer/Services/MessageDeletionService.cs b/Hammer/Services/MessageDeletionService.cs index 2e000b7..22fa757 100644 --- a/Hammer/Services/MessageDeletionService.cs +++ b/Hammer/Services/MessageDeletionService.cs @@ -36,6 +36,20 @@ DiscordLogService logService _logService = logService; } + /// + /// Returns the count of deleted messages in the specified guild. + /// + /// The guild whose deleted messages to count. + /// The count of deleted messages in . + public async Task CountMessageDeletionsAsync(DiscordGuild guild) + { + ArgumentNullException.ThrowIfNull(guild); + + await using AsyncServiceScope scope = _scopeFactory.CreateAsyncScope(); + await using var context = scope.ServiceProvider.GetRequiredService(); + return context.DeletedMessages.Count(m => m.GuildId == guild.Id); + } + /// /// Deletes a specified message, logging the deletion in the staff log and optionally notifying the author. ///