Skip to content

Commit

Permalink
Honour clearHistory option for /ban (#62)
Browse files Browse the repository at this point in the history
Discord API offers a way to clear history natively.
This option is still not honoured for /kick
  • Loading branch information
oliverbooth committed Mar 22, 2023
1 parent 45c20e5 commit 32308d8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 26 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 1 addition & 25 deletions Hammer/Services/BanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DiscordChannel> 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<Task>();

foreach (DiscordChannel channel in channels)
{
var messagesToDelete = new List<DiscordMessage>();
IReadOnlyList<DiscordMessage> 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);
}

Expand Down

0 comments on commit 32308d8

Please sign in to comment.