From 6bdf528c666bf4f112ea3222d56ea0c8b338a568 Mon Sep 17 00:00:00 2001 From: Noi Date: Fri, 2 Dec 2022 20:28:23 -0800 Subject: [PATCH] Replace RemovalType with bool In general: `RemovalType.Ban` was replaced with `true` and `RemovalType.Kick` with `false`. `RemovalType.None` was never used. --- Modules/RegexModerator/ResponseExecutor.cs | 8 +++---- Services/CommonFunctions/BanKickResult.cs | 21 ++++++++++++------- Services/CommonFunctions/CF_Removals.Hooks.cs | 8 +++---- Services/CommonFunctions/CF_Removals.cs | 21 +++++++++---------- Services/CommonFunctions/RemovalType.cs | 21 ------------------- 5 files changed, 32 insertions(+), 47 deletions(-) delete mode 100644 Services/CommonFunctions/RemovalType.cs diff --git a/Modules/RegexModerator/ResponseExecutor.cs b/Modules/RegexModerator/ResponseExecutor.cs index e7fba21..3124549 100644 --- a/Modules/RegexModerator/ResponseExecutor.cs +++ b/Modules/RegexModerator/ResponseExecutor.cs @@ -135,11 +135,11 @@ class ResponseExecutor { #region Response delegates private static Task CmdComment(string? parameter) => Task.FromResult(FromSuccess(parameter)); - private Task CmdBan(string? parameter) => CmdBanKick(RemovalType.Ban, parameter); - private Task CmdKick(string? parameter) => CmdBanKick(RemovalType.Kick, parameter); - private async Task CmdBanKick(RemovalType rt, string? parameter) { + private Task CmdBan(string? parameter) => CmdBanKick(true, parameter); + private Task CmdKick(string? parameter) => CmdBanKick(false, parameter); + private async Task CmdBanKick(bool isBan, string? parameter) { BanKickResult result; - if (rt == RemovalType.Ban) { + if (isBan) { result = await _bot.BanAsync(_guild, LogSource, _user.Id, _rule.BanPurgeDays, parameter, _rule.NotifyUser); } else { diff --git a/Services/CommonFunctions/BanKickResult.cs b/Services/CommonFunctions/BanKickResult.cs index 1021179..ac8d574 100644 --- a/Services/CommonFunctions/BanKickResult.cs +++ b/Services/CommonFunctions/BanKickResult.cs @@ -7,7 +7,6 @@ namespace RegexBot; /// public class BanKickResult { private readonly bool _userNotFound; // possible to receive this error by means other than exception - private readonly RemovalType _rptRemovalType; private readonly ulong _rptTargetId; /// @@ -59,12 +58,22 @@ public class BanKickResult { /// public bool MessageSendSuccess { get; } + /// + /// Gets a value indicating if this result represents a ban. + /// + public bool IsBan { get; } + + /// + /// Gets a value indicating if this result represents a kick. + /// + public bool IsKick { get => !IsBan; } + internal BanKickResult(HttpException? error, bool notificationSuccess, bool errorNotFound, - RemovalType rtype, ulong rtarget) { + bool isBan, ulong rtarget) { OperationError = error; MessageSendSuccess = notificationSuccess; _userNotFound = errorNotFound; - _rptRemovalType = rtype; + IsBan = isBan; _rptTargetId = rtarget; } @@ -78,14 +87,12 @@ public class BanKickResult { if (OperationSuccess) msg = ":white_check_mark: "; else msg = ":x: Failed to "; - if (_rptRemovalType == RemovalType.Ban) { + if (IsBan) { if (OperationSuccess) msg += "Banned"; else msg += "ban"; - } else if (_rptRemovalType == RemovalType.Kick) { + } else { if (OperationSuccess) msg += "Kicked"; else msg += "kick"; - } else { - throw new InvalidOperationException("Cannot create a message for removal type of None."); } if (_rptTargetId != 0) { diff --git a/Services/CommonFunctions/CF_Removals.Hooks.cs b/Services/CommonFunctions/CF_Removals.Hooks.cs index bb62344..c8d97f0 100644 --- a/Services/CommonFunctions/CF_Removals.Hooks.cs +++ b/Services/CommonFunctions/CF_Removals.Hooks.cs @@ -18,7 +18,7 @@ partial class RegexbotClient { int purgeDays, string? reason, bool sendDMToTarget) - => _svcCommonFunctions.BanOrKickAsync(RemovalType.Ban, guild, source, targetUser, purgeDays, reason, sendDMToTarget); + => _svcCommonFunctions.BanOrKickAsync(true, guild, source, targetUser, purgeDays, reason, sendDMToTarget); /// /// Similar to , but making use of an @@ -37,7 +37,7 @@ partial class RegexbotClient { string? reason, bool sendDMToTarget) { var result = EcQueryGuildUser(guild.Id, targetSearch); - if (result == null) return new BanKickResult(null, false, true, RemovalType.Ban, 0); + if (result == null) return new BanKickResult(null, false, true, true, 0); return await BanAsync(guild, source, (ulong)result.UserId, purgeDays, reason, sendDMToTarget); } @@ -60,7 +60,7 @@ partial class RegexbotClient { ulong targetUser, string? reason, bool sendDMToTarget) - => _svcCommonFunctions.BanOrKickAsync(RemovalType.Kick, guild, source, targetUser, default, reason, sendDMToTarget); + => _svcCommonFunctions.BanOrKickAsync(false, guild, source, targetUser, default, reason, sendDMToTarget); /// /// Similar to , but making use of an @@ -80,7 +80,7 @@ partial class RegexbotClient { string? reason, bool sendDMToTarget) { var result = EcQueryGuildUser(guild.Id, targetSearch); - if (result == null) return new BanKickResult(null, false, true, RemovalType.Kick, 0); + if (result == null) return new BanKickResult(null, false, true, false, 0); return await KickAsync(guild, source, (ulong)result.UserId, reason, sendDMToTarget); } } diff --git a/Services/CommonFunctions/CF_Removals.cs b/Services/CommonFunctions/CF_Removals.cs index e543203..941b4cf 100644 --- a/Services/CommonFunctions/CF_Removals.cs +++ b/Services/CommonFunctions/CF_Removals.cs @@ -4,42 +4,41 @@ using Discord.Net; namespace RegexBot.Services.CommonFunctions; internal partial class CommonFunctionsService : Service { // Hooked (indirectly) - internal async Task BanOrKickAsync(RemovalType t, SocketGuild guild, string source, ulong target, + internal async Task BanOrKickAsync(bool isBan, SocketGuild guild, string source, ulong target, int banPurgeDays, string? logReason, bool sendDmToTarget) { - if (t == RemovalType.None) throw new ArgumentException("Removal type must be 'ban' or 'kick'."); var dmSuccess = true; SocketGuildUser utarget = guild.GetUser(target); // Can't kick without obtaining user object. Quit here. - if (t == RemovalType.Kick && utarget == null) return new BanKickResult(null, false, true, RemovalType.Kick, 0); + if (isBan == false && utarget == null) return new BanKickResult(null, false, true, false, 0); // Send DM notification // Must be done before removal, or we risk not being able to send a notification afterwards if (sendDmToTarget) { - if (utarget != null) dmSuccess = await BanKickSendNotificationAsync(utarget, t, logReason); + if (utarget != null) dmSuccess = await BanKickSendNotificationAsync(utarget, isBan, logReason); else dmSuccess = false; } // Perform the action var auditReason = $"(By: {source}) {logReason}"; try { - if (t == RemovalType.Ban) await guild.AddBanAsync(target, banPurgeDays, auditReason); + if (isBan) await guild.AddBanAsync(target, banPurgeDays, auditReason); else await utarget!.KickAsync(auditReason); } catch (HttpException ex) { - return new BanKickResult(ex, dmSuccess, false, t, target); + return new BanKickResult(ex, dmSuccess, false, isBan, target); } - ModLogsProcessRemoval(guild.Id, target, t == RemovalType.Ban ? ModLogType.Ban : ModLogType.Kick, source, logReason); + ModLogsProcessRemoval(guild.Id, target, isBan ? ModLogType.Ban : ModLogType.Kick, source, logReason); - return new BanKickResult(null, dmSuccess, false, t, target); + return new BanKickResult(null, dmSuccess, false, isBan, target); } - private async Task BanKickSendNotificationAsync(SocketGuildUser target, RemovalType action, string? reason) { + private async Task BanKickSendNotificationAsync(SocketGuildUser target, bool isBan, string? reason) { const string DMTemplate = "You have been {0} from {1}"; const string DMTemplateReason = " for the following reason:\n{2}"; var outMessage = string.IsNullOrWhiteSpace(reason) - ? string.Format(DMTemplate + ".", action == RemovalType.Ban ? "banned" : "kicked", target.Guild.Name) - : string.Format(DMTemplate + DMTemplateReason, action == RemovalType.Ban ? "banned" : "kicked", target.Guild.Name, reason); + ? string.Format(DMTemplate + ".", isBan ? "banned" : "kicked", target.Guild.Name) + : string.Format(DMTemplate + DMTemplateReason, isBan ? "banned" : "kicked", target.Guild.Name, reason); var dch = await target.CreateDMChannelAsync(); try { await dch.SendMessageAsync(outMessage); } catch (HttpException) { return false; } diff --git a/Services/CommonFunctions/RemovalType.cs b/Services/CommonFunctions/RemovalType.cs deleted file mode 100644 index 750054c..0000000 --- a/Services/CommonFunctions/RemovalType.cs +++ /dev/null @@ -1,21 +0,0 @@ -namespace RegexBot; -/// -/// Specifies possible outcomes for the removal of a user from a guild. -/// -// Despite specific to CommonFunctionsService, this enum is meant to be visible by modules too, -// thus it is placed within the root namespace. -// TODO Tends to be unused except internally. Look into removing. -public enum RemovalType { - /// - /// Default value. Not used in any actual circumstances. - /// - None, - /// - /// Specifies that the type of removal includes placing the user on the guild's ban list. - /// - Ban, - /// - /// Specifies that the user is removed from the server via kick. - /// - Kick -}