Separate code files between feature sets
This commit is contained in:
parent
e9178bc149
commit
a419dd2554
4 changed files with 72 additions and 68 deletions
|
@ -1,8 +1,6 @@
|
||||||
using Discord.Net;
|
using Discord.Net;
|
||||||
using RegexBot.Common;
|
using RegexBot.Common;
|
||||||
|
|
||||||
// Instances of this class are created by CommonFunctionService and are meant to be sent to modules,
|
|
||||||
// therefore we put this in the root RegexBot namespace despite being specific to this service.
|
|
||||||
namespace RegexBot;
|
namespace RegexBot;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Contains information on various success/failure outcomes for a ban or kick operation.
|
/// Contains information on various success/failure outcomes for a ban or kick operation.
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
using RegexBot.Services.CommonFunctions;
|
namespace RegexBot;
|
||||||
|
|
||||||
namespace RegexBot;
|
|
||||||
partial class RegexbotClient {
|
partial class RegexbotClient {
|
||||||
private readonly CommonFunctionsService _svcCommonFunctions;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Attempts to ban the given user from the specified guild. It is greatly preferred to call this method
|
/// Attempts to ban the given user from the specified guild. It is greatly preferred to call this method
|
||||||
/// instead of manually executing the equivalent method found in Discord.Net. It notifies other services
|
/// instead of manually executing the equivalent method found in Discord.Net. It notifies other services
|
54
Services/CommonFunctions/CF_Removals.cs
Normal file
54
Services/CommonFunctions/CF_Removals.cs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
using Discord.Net;
|
||||||
|
|
||||||
|
namespace RegexBot.Services.CommonFunctions;
|
||||||
|
internal partial class CommonFunctionsService : Service {
|
||||||
|
// Hooked (indirectly)
|
||||||
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static")]
|
||||||
|
internal async Task<BanKickResult> BanOrKickAsync(RemovalType t,
|
||||||
|
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);
|
||||||
|
|
||||||
|
// TODO notify services here as soon as we get some who will want to listen to this (use source parameter)
|
||||||
|
|
||||||
|
// Send DM notification
|
||||||
|
if (sendDmToTarget) {
|
||||||
|
if (utarget != null) dmSuccess = await BanKickSendNotificationAsync(utarget, t, logReason);
|
||||||
|
else dmSuccess = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perform the action
|
||||||
|
var auditReason = $"(By: {source}) {logReason}";
|
||||||
|
try {
|
||||||
|
if (t == RemovalType.Ban) 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(null, dmSuccess, false, t, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
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}";
|
||||||
|
|
||||||
|
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);
|
||||||
|
var dch = await target.CreateDMChannelAsync();
|
||||||
|
|
||||||
|
try { await dch.SendMessageAsync(outMessage); } catch (HttpException) { return false; }
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,64 +1,20 @@
|
||||||
using Discord.Net;
|
using RegexBot.Services.CommonFunctions;
|
||||||
|
|
||||||
namespace RegexBot.Services.CommonFunctions;
|
namespace RegexBot.Services.CommonFunctions {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Implements certain common actions that modules may want to perform. Using this service to perform those
|
/// 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
|
/// 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.
|
/// inform services which provide any additional features the ability to respond to those actions ahead of time.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class CommonFunctionsService : Service {
|
internal partial class CommonFunctionsService : Service {
|
||||||
|
// Note: Several classes within this service created by its hooks are meant to be sent to modules,
|
||||||
|
// therefore those public classes are placed into the root RegexBot namespace for the developer's convenience.
|
||||||
public CommonFunctionsService(RegexbotClient bot) : base(bot) { }
|
public CommonFunctionsService(RegexbotClient bot) : base(bot) { }
|
||||||
|
|
||||||
#region Guild member removal
|
|
||||||
// Hooked (indirectly)
|
|
||||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static")]
|
|
||||||
internal async Task<BanKickResult> BanOrKickAsync(RemovalType t,
|
|
||||||
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);
|
|
||||||
|
|
||||||
// TODO notify services here as soon as we get some who will want to listen to this (use source parameter)
|
|
||||||
|
|
||||||
// Send DM notification
|
|
||||||
if (sendDmToTarget) {
|
|
||||||
if (utarget != null) dmSuccess = await BanKickSendNotificationAsync(utarget, t, logReason);
|
|
||||||
else dmSuccess = false;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Perform the action
|
|
||||||
var auditReason = $"(By: {source}) {logReason}";
|
namespace RegexBot {
|
||||||
try {
|
partial class RegexbotClient {
|
||||||
if (t == RemovalType.Ban) await guild.AddBanAsync(target, banPurgeDays, auditReason);
|
private readonly CommonFunctionsService _svcCommonFunctions;
|
||||||
else await utarget!.KickAsync(auditReason);
|
}
|
||||||
// TODO for kick: Figure out a way to specify invoker properly in audit log (as in mee6, etc).
|
|
||||||
} catch (HttpException ex) {
|
|
||||||
return new BanKickResult(ex, dmSuccess, false, t, target);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new BanKickResult(null, dmSuccess, false, t, target);
|
|
||||||
}
|
|
||||||
|
|
||||||
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}";
|
|
||||||
|
|
||||||
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);
|
|
||||||
var dch = await target.CreateDMChannelAsync();
|
|
||||||
|
|
||||||
try { await dch.SendMessageAsync(outMessage); } catch (HttpException) { return false; }
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue