From 32308d85176b5d4ae1d1d886c9c35390a5c21513 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 22 Mar 2023 22:30:12 +0000 Subject: [PATCH] Honour clearHistory option for /ban (#62) Discord API offers a way to clear history natively. This option is still not honoured for /kick --- CHANGELOG.md | 7 ++++++- Hammer/Services/BanService.cs | 26 +------------------------- 2 files changed, 7 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2b17af..96581fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Fixed + +- `clearMessageHistory` is now honoured when `/ban` is issued against a user. (#62) + ### Changed -- Report enumerations are no longer async. This should mildly (though negligibly) improve performance as we no longer allocate an async state machine. +- Report enumerations are no longer async. This should mildly (though negligibly) improve performance as we no longer allocate an + async state machine. ## [5.1.1] - 2023-03-21 diff --git a/Hammer/Services/BanService.cs b/Hammer/Services/BanService.cs index 85d3363..2010d2f 100644 --- a/Hammer/Services/BanService.cs +++ b/Hammer/Services/BanService.cs @@ -149,31 +149,7 @@ bool clearHistory await _logService.LogAsync(guild, embed).ConfigureAwait(false); await _mailmanService.SendInfractionAsync(infraction, infractionCount, options).ConfigureAwait(false); - await guild.BanMemberAsync(user.Id, reason: reason).ConfigureAwait(false); - - if (clearHistory) - { - _ = Task.Run(async () => - { - IEnumerable channels = guild.Channels.Values - .Concat(guild.Threads.Values) - .Where(c => c.Type is ChannelType.Text or ChannelType.PublicThread or ChannelType.PrivateThread); - - var tasks = new List(); - - foreach (DiscordChannel channel in channels) - { - var messagesToDelete = new List(); - IReadOnlyList messages = await channel.GetMessagesAsync().ConfigureAwait(false); - messagesToDelete.AddRange(messages.Where(m => m.Author.Id == user.Id)); - if (messagesToDelete.Count > 0) - tasks.Add(channel.DeleteMessagesAsync(messagesToDelete, "User was banned")); - } - - await Task.WhenAll(tasks).ConfigureAwait(false); - }); - } - + await guild.BanMemberAsync(user.Id, reason: reason, delete_message_days: clearHistory ? 7 : 0).ConfigureAwait(false); return (infraction, success); }