Skip to content

Commit

Permalink
Delegate statistics to service
Browse files Browse the repository at this point in the history
Fixes miscalculated user counts, calculation cached DistinctBy early and had incorrect results

This change also cleans up the embed a fair bit
  • Loading branch information
oliverbooth committed Mar 8, 2023
1 parent be1dfe9 commit b1bc298
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 65 deletions.
68 changes: 12 additions & 56 deletions Hammer/Commands/Infractions/InfractionCommand.Stats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using DSharpPlus.SlashCommands.Attributes;
using Hammer.Configuration;
using Hammer.Data;
using Humanizer;

namespace Hammer.Commands.Infractions;

Expand All @@ -13,73 +12,30 @@ internal sealed partial class InfractionCommand
[SlashRequireGuild]
public async Task StatsAsync(InteractionContext context)
{
await context.DeferAsync().ConfigureAwait(false);
var embed = new DiscordEmbedBuilder();

IReadOnlyList<Infraction> infractions = _infractionService.GetInfractions(context.Guild);

if (infractions.Count == 0)
{
var embed = new DiscordEmbedBuilder();
embed.WithColor(DiscordColor.Orange);
embed.WithTitle("No infractions on record");
embed.WithDescription("Statistics cannot be generated because there are no infractions on record.");
}
else
{
if (_configurationService.TryGetGuildConfiguration(context.Guild, out GuildConfiguration? guildConfiguration))
embed.WithColor(guildConfiguration.PrimaryColor);
else
embed.WithColor(DiscordColor.Purple);

int totalInfractions = infractions.Count;
Infraction[] distinctUserInfractions = infractions.DistinctBy(i => i.UserId).ToArray();

int infractedUsers = distinctUserInfractions.Length;
int warnedUsers = distinctUserInfractions.Count(i => i.Type == InfractionType.Warning);
int mutedUsers = distinctUserInfractions.Count(i => i.Type is InfractionType.Mute or InfractionType.TemporaryMute);
int bannedUsers = distinctUserInfractions.Count(i => i.Type is InfractionType.Ban or InfractionType.TemporaryBan);
int kickedUsers = distinctUserInfractions.Count(i => i.Type is InfractionType.Kick);
int gaggedUsers = distinctUserInfractions.Count(i => i.Type is InfractionType.Gag);

int warnings = infractions.Count(i => i.Type is InfractionType.Warning);
int gags = infractions.Count(i => i.Type is InfractionType.Gag);
int kicks = infractions.Count(i => i.Type is InfractionType.Kick);
int messagesDeleted = await _messageDeletionService.CountMessageDeletionsAsync(context.Guild);

int tempMuteCount = infractions.Count(i => i.Type is InfractionType.TemporaryMute);
int muteCount = infractions.Count(i => i.Type is InfractionType.Mute);
var mutes = $"{muteCount + tempMuteCount} ({tempMuteCount}T / {muteCount}P)";

int tempBanCount = infractions.Count(i => i.Type is InfractionType.TemporaryBan);
int banCount = infractions.Count(i => i.Type is InfractionType.Ban);
var bans = $"{banCount + tempBanCount} ({tempBanCount}T / {banCount}P)";

DateTimeOffset now = DateTimeOffset.UtcNow;
long remainingMuteDuration = _muteService.GetTemporaryMutes(context.Guild).Sum(m => (m.ExpiresAt!.Value - now).Ticks);
long remainingBanDuration = _banService.GetTemporaryBans(context.Guild).Sum(b => (b.ExpiresAt - now).Ticks);

embed.WithTitle("Infraction Statistics");
embed.AddField("Total Infractions", totalInfractions.ToString("N0"), true);
embed.AddField("Remaining Mute Duration", TimeSpan.FromTicks(remainingMuteDuration).Humanize(), true);
embed.AddField("Remaining Ban Duration", TimeSpan.FromTicks(remainingBanDuration).Humanize(), true);

embed.AddField("Total Infracted Users", infractedUsers.ToString("N0"), true);
embed.AddField("Total Warned Users", warnedUsers.ToString("N0"), true);
embed.AddField("Total Muted Users", mutedUsers.ToString("N0"), true);
embed.AddField("Total Banned Users", bannedUsers.ToString("N0"), true);
embed.AddField("Total Kicked Users", kickedUsers.ToString("N0"), true);
embed.AddField("Total Gagged Users", gaggedUsers.ToString("N0"), true);
await context.CreateResponseAsync(embed, true).ConfigureAwait(false);
return;
}

embed.AddField("Warnings", warnings.ToString("N0"), true);
embed.AddField("Mutes", mutes, true);
embed.AddField("Bans", bans, true);
embed.AddField("Kicks", kicks.ToString("N0"), true);
embed.AddField("Gags", gags.ToString("N0"), true);
embed.AddField("Messages Deleted", messagesDeleted.ToString("N0"), true);
if (!_configurationService.TryGetGuildConfiguration(context.Guild, out GuildConfiguration? guildConfiguration))
{
await context.CreateResponseAsync("Guild is not configured!", true).ConfigureAwait(false);
return;
}

await context.DeferAsync().ConfigureAwait(false);
DiscordEmbed result = await _infractionStatisticsService.CreateStatisticsEmbedAsync(context.Guild).ConfigureAwait(false);

var builder = new DiscordWebhookBuilder();
builder.AddEmbed(embed);
builder.AddEmbed(result);
await context.EditResponseAsync(builder).ConfigureAwait(false);
}
}
12 changes: 3 additions & 9 deletions Hammer/Commands/Infractions/InfractionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ internal sealed partial class InfractionCommand : ApplicationCommandModule
{
private readonly ConfigurationService _configurationService;
private readonly InfractionService _infractionService;
private readonly InfractionStatisticsService _infractionStatisticsService;
private readonly DiscordLogService _logService;
private readonly BanService _banService;
private readonly MessageDeletionService _messageDeletionService;
private readonly MuteService _muteService;
private readonly RuleService _ruleService;

/// <summary>
Expand All @@ -23,19 +21,15 @@ internal sealed partial class InfractionCommand : ApplicationCommandModule
public InfractionCommand(
ConfigurationService configurationService,
DiscordLogService logService,
BanService banService,
InfractionService infractionService,
MessageDeletionService messageDeletionService,
MuteService muteService,
InfractionStatisticsService infractionStatisticsService,
RuleService ruleService
)
{
_configurationService = configurationService;
_infractionService = infractionService;
_infractionStatisticsService = infractionStatisticsService;
_logService = logService;
_banService = banService;
_messageDeletionService = messageDeletionService;
_muteService = muteService;
_ruleService = ruleService;
}
}
1 change: 1 addition & 0 deletions Hammer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ await Host.CreateDefaultBuilder(args)

services.AddSingleton<HttpClient>();
services.AddSingleton<ConfigurationService>();
services.AddSingleton<InfractionStatisticsService>();
services.AddSingleton<MailmanService>();
services.AddSingleton<MessageService>();
services.AddSingleton<MessageDeletionService>();
Expand Down
Loading

0 comments on commit b1bc298

Please sign in to comment.