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:
parent
36c050afc8
commit
6bdf528c66
5 changed files with 32 additions and 47 deletions
|
@ -135,11 +135,11 @@ class ResponseExecutor {
|
||||||
#region Response delegates
|
#region Response delegates
|
||||||
private static Task<ResponseResult> CmdComment(string? parameter) => Task.FromResult(FromSuccess(parameter));
|
private static Task<ResponseResult> CmdComment(string? parameter) => Task.FromResult(FromSuccess(parameter));
|
||||||
|
|
||||||
private Task<ResponseResult> CmdBan(string? parameter) => CmdBanKick(RemovalType.Ban, parameter);
|
private Task<ResponseResult> CmdBan(string? parameter) => CmdBanKick(true, parameter);
|
||||||
private Task<ResponseResult> CmdKick(string? parameter) => CmdBanKick(RemovalType.Kick, parameter);
|
private Task<ResponseResult> CmdKick(string? parameter) => CmdBanKick(false, parameter);
|
||||||
private async Task<ResponseResult> CmdBanKick(RemovalType rt, string? parameter) {
|
private async Task<ResponseResult> CmdBanKick(bool isBan, string? parameter) {
|
||||||
BanKickResult result;
|
BanKickResult result;
|
||||||
if (rt == RemovalType.Ban) {
|
if (isBan) {
|
||||||
result = await _bot.BanAsync(_guild, LogSource, _user.Id,
|
result = await _bot.BanAsync(_guild, LogSource, _user.Id,
|
||||||
_rule.BanPurgeDays, parameter, _rule.NotifyUser);
|
_rule.BanPurgeDays, parameter, _rule.NotifyUser);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -7,7 +7,6 @@ namespace RegexBot;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class BanKickResult {
|
public class BanKickResult {
|
||||||
private readonly bool _userNotFound; // possible to receive this error by means other than exception
|
private readonly bool _userNotFound; // possible to receive this error by means other than exception
|
||||||
private readonly RemovalType _rptRemovalType;
|
|
||||||
private readonly ulong _rptTargetId;
|
private readonly ulong _rptTargetId;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -59,12 +58,22 @@ public class BanKickResult {
|
||||||
/// </value>
|
/// </value>
|
||||||
public bool MessageSendSuccess { get; }
|
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,
|
internal BanKickResult(HttpException? error, bool notificationSuccess, bool errorNotFound,
|
||||||
RemovalType rtype, ulong rtarget) {
|
bool isBan, ulong rtarget) {
|
||||||
OperationError = error;
|
OperationError = error;
|
||||||
MessageSendSuccess = notificationSuccess;
|
MessageSendSuccess = notificationSuccess;
|
||||||
_userNotFound = errorNotFound;
|
_userNotFound = errorNotFound;
|
||||||
_rptRemovalType = rtype;
|
IsBan = isBan;
|
||||||
_rptTargetId = rtarget;
|
_rptTargetId = rtarget;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,14 +87,12 @@ public class BanKickResult {
|
||||||
if (OperationSuccess) msg = ":white_check_mark: ";
|
if (OperationSuccess) msg = ":white_check_mark: ";
|
||||||
else msg = ":x: Failed to ";
|
else msg = ":x: Failed to ";
|
||||||
|
|
||||||
if (_rptRemovalType == RemovalType.Ban) {
|
if (IsBan) {
|
||||||
if (OperationSuccess) msg += "Banned";
|
if (OperationSuccess) msg += "Banned";
|
||||||
else msg += "ban";
|
else msg += "ban";
|
||||||
} else if (_rptRemovalType == RemovalType.Kick) {
|
} else {
|
||||||
if (OperationSuccess) msg += "Kicked";
|
if (OperationSuccess) msg += "Kicked";
|
||||||
else msg += "kick";
|
else msg += "kick";
|
||||||
} else {
|
|
||||||
throw new InvalidOperationException("Cannot create a message for removal type of None.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_rptTargetId != 0) {
|
if (_rptTargetId != 0) {
|
||||||
|
|
|
@ -18,7 +18,7 @@ partial class RegexbotClient {
|
||||||
int purgeDays,
|
int purgeDays,
|
||||||
string? reason,
|
string? reason,
|
||||||
bool sendDMToTarget)
|
bool sendDMToTarget)
|
||||||
=> _svcCommonFunctions.BanOrKickAsync(RemovalType.Ban, guild, source, targetUser, purgeDays, reason, sendDMToTarget);
|
=> _svcCommonFunctions.BanOrKickAsync(true, guild, source, targetUser, purgeDays, reason, sendDMToTarget);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Similar to <see cref="BanAsync(SocketGuild, string, ulong, int, string, bool)"/>, but making use of an
|
/// 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,
|
string? reason,
|
||||||
bool sendDMToTarget) {
|
bool sendDMToTarget) {
|
||||||
var result = EcQueryGuildUser(guild.Id, targetSearch);
|
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);
|
return await BanAsync(guild, source, (ulong)result.UserId, purgeDays, reason, sendDMToTarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ partial class RegexbotClient {
|
||||||
ulong targetUser,
|
ulong targetUser,
|
||||||
string? reason,
|
string? reason,
|
||||||
bool sendDMToTarget)
|
bool sendDMToTarget)
|
||||||
=> _svcCommonFunctions.BanOrKickAsync(RemovalType.Kick, guild, source, targetUser, default, reason, sendDMToTarget);
|
=> _svcCommonFunctions.BanOrKickAsync(false, guild, source, targetUser, default, reason, sendDMToTarget);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Similar to <see cref="KickAsync(SocketGuild, string, ulong, string, bool)"/>, but making use of an
|
/// Similar to <see cref="KickAsync(SocketGuild, string, ulong, string, bool)"/>, but making use of an
|
||||||
|
@ -80,7 +80,7 @@ partial class RegexbotClient {
|
||||||
string? reason,
|
string? reason,
|
||||||
bool sendDMToTarget) {
|
bool sendDMToTarget) {
|
||||||
var result = EcQueryGuildUser(guild.Id, targetSearch);
|
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);
|
return await KickAsync(guild, source, (ulong)result.UserId, reason, sendDMToTarget);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,42 +4,41 @@ using Discord.Net;
|
||||||
namespace RegexBot.Services.CommonFunctions;
|
namespace RegexBot.Services.CommonFunctions;
|
||||||
internal partial class CommonFunctionsService : Service {
|
internal partial class CommonFunctionsService : Service {
|
||||||
// Hooked (indirectly)
|
// 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) {
|
int banPurgeDays, string? logReason, bool sendDmToTarget) {
|
||||||
if (t == RemovalType.None) throw new ArgumentException("Removal type must be 'ban' or 'kick'.");
|
|
||||||
var dmSuccess = true;
|
var dmSuccess = true;
|
||||||
|
|
||||||
SocketGuildUser utarget = guild.GetUser(target);
|
SocketGuildUser utarget = guild.GetUser(target);
|
||||||
// Can't kick without obtaining user object. Quit here.
|
// 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
|
// Send DM notification
|
||||||
// Must be done before removal, or we risk not being able to send a notification afterwards
|
// Must be done before removal, or we risk not being able to send a notification afterwards
|
||||||
if (sendDmToTarget) {
|
if (sendDmToTarget) {
|
||||||
if (utarget != null) dmSuccess = await BanKickSendNotificationAsync(utarget, t, logReason);
|
if (utarget != null) dmSuccess = await BanKickSendNotificationAsync(utarget, isBan, logReason);
|
||||||
else dmSuccess = false;
|
else dmSuccess = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform the action
|
// Perform the action
|
||||||
var auditReason = $"(By: {source}) {logReason}";
|
var auditReason = $"(By: {source}) {logReason}";
|
||||||
try {
|
try {
|
||||||
if (t == RemovalType.Ban) await guild.AddBanAsync(target, banPurgeDays, auditReason);
|
if (isBan) await guild.AddBanAsync(target, banPurgeDays, auditReason);
|
||||||
else await utarget!.KickAsync(auditReason);
|
else await utarget!.KickAsync(auditReason);
|
||||||
} catch (HttpException ex) {
|
} 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 DMTemplate = "You have been {0} from {1}";
|
||||||
const string DMTemplateReason = " for the following reason:\n{2}";
|
const string DMTemplateReason = " for the following reason:\n{2}";
|
||||||
|
|
||||||
var outMessage = string.IsNullOrWhiteSpace(reason)
|
var outMessage = string.IsNullOrWhiteSpace(reason)
|
||||||
? string.Format(DMTemplate + ".", action == RemovalType.Ban ? "banned" : "kicked", target.Guild.Name)
|
? string.Format(DMTemplate + ".", isBan ? "banned" : "kicked", target.Guild.Name)
|
||||||
: string.Format(DMTemplate + DMTemplateReason, action == RemovalType.Ban ? "banned" : "kicked", target.Guild.Name, reason);
|
: string.Format(DMTemplate + DMTemplateReason, isBan ? "banned" : "kicked", target.Guild.Name, reason);
|
||||||
|
|
||||||
var dch = await target.CreateDMChannelAsync();
|
var dch = await target.CreateDMChannelAsync();
|
||||||
try { await dch.SendMessageAsync(outMessage); } catch (HttpException) { return false; }
|
try { await dch.SendMessageAsync(outMessage); } catch (HttpException) { return false; }
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in a new issue