Replace RemovalType with bool

In general: `RemovalType.Ban` was replaced with `true` and
`RemovalType.Kick` with `false`. `RemovalType.None` was never used.
This commit is contained in:
Noi 2022-12-02 20:28:23 -08:00
parent 36c050afc8
commit 6bdf528c66
5 changed files with 32 additions and 47 deletions

View file

@ -135,11 +135,11 @@ class ResponseExecutor {
#region Response delegates
private static Task<ResponseResult> CmdComment(string? parameter) => Task.FromResult(FromSuccess(parameter));
private Task<ResponseResult> CmdBan(string? parameter) => CmdBanKick(RemovalType.Ban, parameter);
private Task<ResponseResult> CmdKick(string? parameter) => CmdBanKick(RemovalType.Kick, parameter);
private async Task<ResponseResult> CmdBanKick(RemovalType rt, string? parameter) {
private Task<ResponseResult> CmdBan(string? parameter) => CmdBanKick(true, parameter);
private Task<ResponseResult> CmdKick(string? parameter) => CmdBanKick(false, parameter);
private async Task<ResponseResult> 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 {

View file

@ -7,7 +7,6 @@ namespace RegexBot;
/// </summary>
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;
/// <summary>
@ -59,12 +58,22 @@ public class BanKickResult {
/// </value>
public bool MessageSendSuccess { get; }
/// <summary>
/// Gets a value indicating if this result represents a ban.
/// </summary>
public bool IsBan { get; }
/// <summary>
/// Gets a value indicating if this result represents a kick.
/// </summary>
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) {

View file

@ -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);
/// <summary>
/// Similar to <see cref="BanAsync(SocketGuild, string, ulong, int, string, bool)"/>, 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);
/// <summary>
/// Similar to <see cref="KickAsync(SocketGuild, string, ulong, string, bool)"/>, 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);
}
}

View file

@ -4,42 +4,41 @@ using Discord.Net;
namespace RegexBot.Services.CommonFunctions;
internal partial class CommonFunctionsService : Service {
// Hooked (indirectly)
internal async Task<BanKickResult> BanOrKickAsync(RemovalType t, SocketGuild guild, string source, ulong target,
internal async Task<BanKickResult> 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<bool> BanKickSendNotificationAsync(SocketGuildUser target, RemovalType action, string? reason) {
private async Task<bool> 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; }

View file

@ -1,21 +0,0 @@
namespace RegexBot;
/// <summary>
/// Specifies possible outcomes for the removal of a user from a guild.
/// </summary>
// 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 {
/// <summary>
/// Default value. Not used in any actual circumstances.
/// </summary>
None,
/// <summary>
/// Specifies that the type of removal includes placing the user on the guild's ban list.
/// </summary>
Ban,
/// <summary>
/// Specifies that the user is removed from the server via kick.
/// </summary>
Kick
}