2022-03-29 05:03:01 +00:00
|
|
|
|
using Discord.Net;
|
2018-12-05 03:41:42 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
namespace RegexBot.Services.CommonFunctions;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Implements certain common actions that modules may want to perform. Using this service to perform those
|
|
|
|
|
/// functions may help enforce a sense of consistency across modules when performing common actions, and may
|
|
|
|
|
/// inform services which provide any additional features the ability to respond to those actions ahead of time.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class CommonFunctionsService : Service {
|
|
|
|
|
public CommonFunctionsService(RegexbotClient bot) : base(bot) { }
|
2018-12-05 03:41:42 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
#region Guild member removal
|
2022-05-26 02:27:53 +00:00
|
|
|
|
// Hooked (indirectly)
|
2022-05-12 03:26:28 +00:00
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static")]
|
2022-05-26 02:27:53 +00:00
|
|
|
|
internal async Task<BanKickResult> BanOrKickAsync(RemovalType t,
|
|
|
|
|
SocketGuild guild,
|
|
|
|
|
string source,
|
|
|
|
|
ulong target,
|
|
|
|
|
int banPurgeDays,
|
|
|
|
|
string? logReason,
|
|
|
|
|
bool sendDmToTarget) {
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (t == RemovalType.None) throw new ArgumentException("Removal type must be 'ban' or 'kick'.");
|
|
|
|
|
var dmSuccess = true;
|
2018-12-05 03:41:42 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
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);
|
2018-12-05 03:41:42 +00:00
|
|
|
|
|
2022-05-12 03:26:28 +00:00
|
|
|
|
// TODO notify services here as soon as we get some who will want to listen to this (use source parameter)
|
2018-12-05 03:41:42 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
// Send DM notification
|
|
|
|
|
if (sendDmToTarget) {
|
|
|
|
|
if (utarget != null) dmSuccess = await BanKickSendNotificationAsync(utarget, t, logReason);
|
|
|
|
|
else dmSuccess = false;
|
|
|
|
|
}
|
2018-12-05 03:41:42 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
// Perform the action
|
2022-06-10 23:21:17 +00:00
|
|
|
|
var auditReason = $"(By: {source}) {logReason}";
|
2022-03-29 05:03:01 +00:00
|
|
|
|
try {
|
2022-06-10 23:21:17 +00:00
|
|
|
|
if (t == RemovalType.Ban) await guild.AddBanAsync(target, banPurgeDays, auditReason);
|
|
|
|
|
else await utarget!.KickAsync(auditReason);
|
|
|
|
|
// TODO for kick: Figure out a way to specify invoker properly in audit log (as in mee6, etc).
|
2022-03-29 05:03:01 +00:00
|
|
|
|
} catch (HttpException ex) {
|
|
|
|
|
return new BanKickResult(ex, dmSuccess, false, t, target);
|
2018-12-05 03:41:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
return new BanKickResult(null, dmSuccess, false, t, target);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-10 23:21:17 +00:00
|
|
|
|
private static async Task<bool> BanKickSendNotificationAsync(SocketGuildUser target, RemovalType action, string? reason) {
|
|
|
|
|
const string DMTemplate = "You have been {0} from {1}";
|
|
|
|
|
const string DMTemplateReason = " for the following reason:\n{2}";
|
2018-12-05 03:41:42 +00:00
|
|
|
|
|
2022-06-10 23:21:17 +00:00
|
|
|
|
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);
|
2022-03-29 05:03:01 +00:00
|
|
|
|
var dch = await target.CreateDMChannelAsync();
|
2018-12-05 03:41:42 +00:00
|
|
|
|
|
2022-06-10 23:21:17 +00:00
|
|
|
|
try { await dch.SendMessageAsync(outMessage); } catch (HttpException) { return false; }
|
2018-12-05 03:41:42 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
return true;
|
2018-12-05 03:41:42 +00:00
|
|
|
|
}
|
2022-03-29 05:03:01 +00:00
|
|
|
|
#endregion
|
2018-12-05 03:41:42 +00:00
|
|
|
|
}
|