2022-08-26 01:40:57 +00:00
|
|
|
#pragma warning disable CA1822 // "Mark members as static" - members should only be callable by code with access to this instance
|
2022-08-16 03:08:00 +00:00
|
|
|
using Discord.Net;
|
|
|
|
|
|
|
|
namespace RegexBot.Services.CommonFunctions;
|
|
|
|
internal partial class CommonFunctionsService : Service {
|
|
|
|
// Hooked (indirectly)
|
2022-12-03 04:28:23 +00:00
|
|
|
internal async Task<BanKickResult> BanOrKickAsync(bool isBan, SocketGuild guild, string source, ulong target,
|
2022-08-16 19:37:06 +00:00
|
|
|
int banPurgeDays, string? logReason, bool sendDmToTarget) {
|
2022-08-16 03:08:00 +00:00
|
|
|
var dmSuccess = true;
|
|
|
|
|
|
|
|
SocketGuildUser utarget = guild.GetUser(target);
|
|
|
|
// Can't kick without obtaining user object. Quit here.
|
2022-12-03 04:28:23 +00:00
|
|
|
if (isBan == false && utarget == null) return new BanKickResult(null, false, true, false, 0);
|
2022-08-16 03:08:00 +00:00
|
|
|
|
|
|
|
// Send DM notification
|
2022-09-21 03:39:37 +00:00
|
|
|
// Must be done before removal, or we risk not being able to send a notification afterwards
|
2022-08-16 03:08:00 +00:00
|
|
|
if (sendDmToTarget) {
|
2022-12-03 04:28:23 +00:00
|
|
|
if (utarget != null) dmSuccess = await BanKickSendNotificationAsync(utarget, isBan, logReason);
|
2022-08-16 03:08:00 +00:00
|
|
|
else dmSuccess = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the action
|
|
|
|
var auditReason = $"(By: {source}) {logReason}";
|
|
|
|
try {
|
2022-12-03 04:28:23 +00:00
|
|
|
if (isBan) await guild.AddBanAsync(target, banPurgeDays, auditReason);
|
2022-08-16 03:08:00 +00:00
|
|
|
else await utarget!.KickAsync(auditReason);
|
|
|
|
} catch (HttpException ex) {
|
2022-12-03 04:28:23 +00:00
|
|
|
return new BanKickResult(ex, dmSuccess, false, isBan, target);
|
2022-08-16 03:08:00 +00:00
|
|
|
}
|
2022-12-03 04:28:23 +00:00
|
|
|
ModLogsProcessRemoval(guild.Id, target, isBan ? ModLogType.Ban : ModLogType.Kick, source, logReason);
|
2022-09-21 03:39:37 +00:00
|
|
|
|
2022-12-03 04:28:23 +00:00
|
|
|
return new BanKickResult(null, dmSuccess, false, isBan, target);
|
2022-08-16 03:08:00 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 04:28:23 +00:00
|
|
|
private async Task<bool> BanKickSendNotificationAsync(SocketGuildUser target, bool isBan, string? reason) {
|
2022-08-16 03:08:00 +00:00
|
|
|
const string DMTemplate = "You have been {0} from {1}";
|
|
|
|
const string DMTemplateReason = " for the following reason:\n{2}";
|
|
|
|
|
|
|
|
var outMessage = string.IsNullOrWhiteSpace(reason)
|
2022-12-03 04:28:23 +00:00
|
|
|
? string.Format(DMTemplate + ".", isBan ? "banned" : "kicked", target.Guild.Name)
|
|
|
|
: string.Format(DMTemplate + DMTemplateReason, isBan ? "banned" : "kicked", target.Guild.Name, reason);
|
2022-09-21 03:39:37 +00:00
|
|
|
|
2022-08-16 03:08:00 +00:00
|
|
|
var dch = await target.CreateDMChannelAsync();
|
|
|
|
try { await dch.SendMessageAsync(outMessage); } catch (HttpException) { return false; }
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|