From 2b625db4e22faf89692b8de9c6cd6a044dc16a78 Mon Sep 17 00:00:00 2001 From: krustykarbs <84251908+krustykarbs@users.noreply.github.com> Date: Wed, 6 Oct 2021 00:58:54 -0400 Subject: [PATCH 1/2] feat: Implement massban in ModerationModule --- .../Modules/Moderation/ModerationModule.cs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Zhongli.Bot/Modules/Moderation/ModerationModule.cs b/Zhongli.Bot/Modules/Moderation/ModerationModule.cs index 49a5afc..ec4879f 100644 --- a/Zhongli.Bot/Modules/Moderation/ModerationModule.cs +++ b/Zhongli.Bot/Modules/Moderation/ModerationModule.cs @@ -1,4 +1,5 @@ using System; +using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; @@ -49,6 +50,46 @@ public async Task BanAsync(IUser user, uint deleteDays = 1, TimeSpan? length = n else await ReplyReprimandAsync(result, details); } + + [Command("massban")] + [Summary("Ban a list of users permanently from the current guild, and delete messages.")] + [RequireAuthorization(AuthorizationScope.Ban)] + public async Task MassBanAsync(uint deleteDays = 1, string? reason = null, params IUser[] users) + { + var sb = new StringBuilder(); + var fail = new StringBuilder(); + var fails = 0; + + foreach (var u in users) + { + var details = await GetDetailsAsync(u, reason); + var result = await _moderation.TryBanAsync(deleteDays, null, details); + + if (result is null) + { + fail.AppendLine($"Failed to ban {u.GetFullUsername()}"); + fails += 1; + } + + else + sb.AppendLine(u.GetFullUsername()); + } + + if (sb.Length is not 0) + { + var embed = new EmbedBuilder() + .WithTitle($"Banned {users.Length - fails} user(s).") + .WithDescription(sb.ToString() + fail); + + await ReplyAsync(embed: embed.Build()); + } + else + { + await _error.AssociateError(Context.Message, fail.ToString()); + } + + + } [Command("kick")] [Summary("Kick a user from the current guild.")] From 57b044c3e1a1cfb6b62e8e9846f493ce749e581b Mon Sep 17 00:00:00 2001 From: krustykarbs <84251908+krustykarbs@users.noreply.github.com> Date: Wed, 6 Oct 2021 09:28:40 -0400 Subject: [PATCH 2/2] chore: Clean up code --- Zhongli.Bot/Modules/Moderation/ModerationModule.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Zhongli.Bot/Modules/Moderation/ModerationModule.cs b/Zhongli.Bot/Modules/Moderation/ModerationModule.cs index ec4879f..e5ca2b8 100644 --- a/Zhongli.Bot/Modules/Moderation/ModerationModule.cs +++ b/Zhongli.Bot/Modules/Moderation/ModerationModule.cs @@ -75,7 +75,12 @@ public async Task MassBanAsync(uint deleteDays = 1, string? reason = null, param sb.AppendLine(u.GetFullUsername()); } - if (sb.Length is not 0) + if (sb.Length is 0) + { + await _error.AssociateError(Context.Message, fail.ToString()); + + } + else { var embed = new EmbedBuilder() .WithTitle($"Banned {users.Length - fails} user(s).") @@ -83,12 +88,6 @@ public async Task MassBanAsync(uint deleteDays = 1, string? reason = null, param await ReplyAsync(embed: embed.Build()); } - else - { - await _error.AssociateError(Context.Message, fail.ToString()); - } - - } [Command("kick")]