Implement mod logging commands

This commit is contained in:
Noi 2022-09-13 20:16:25 -07:00
parent 809197984a
commit d5728ad0a1
3 changed files with 82 additions and 3 deletions

View file

@ -60,7 +60,7 @@ abstract class BanKick : CommandConfig {
// "PurgeDays" - integer; Number of days of target's post history to delete, if banning. // "PurgeDays" - integer; Number of days of target's post history to delete, if banning.
// Must be between 0-7 inclusive. Defaults to 0. // Must be between 0-7 inclusive. Defaults to 0.
// "SendNotify" - boolean; Whether to send a notification DM explaining the action. Defaults to true. // "SendNotify" - boolean; Whether to send a notification DM explaining the action. Defaults to true.
// "SuccessMessage" - string; Message to display on command success. Overrides default. // "SuccessMessage" - string; Additional message to display on command success.
protected BanKick(ModCommands module, JObject config, bool ban) : base(module, config) { protected BanKick(ModCommands module, JObject config, bool ban) : base(module, config) {
ForceReason = config[nameof(ForceReason)]?.Value<bool>() ?? false; ForceReason = config[nameof(ForceReason)]?.Value<bool>() ?? false;
PurgeDays = config[nameof(PurgeDays)]?.Value<int>() ?? 0; PurgeDays = config[nameof(PurgeDays)]?.Value<int>() ?? 0;
@ -68,7 +68,7 @@ abstract class BanKick : CommandConfig {
SendNotify = config[nameof(SendNotify)]?.Value<bool>() ?? true; SendNotify = config[nameof(SendNotify)]?.Value<bool>() ?? true;
SuccessMessage = config[nameof(SuccessMessage)]?.Value<string>(); SuccessMessage = config[nameof(SuccessMessage)]?.Value<string>();
_usage = $"{Command} `user or user ID` `" + (ForceReason ? "reason" : "[reason]") + "`\n" _usage = $"{Command} `user ID or tag` `" + (ForceReason ? "reason" : "[reason]") + "`\n"
+ "Removes the given user from this server" + "Removes the given user from this server"
+ (ban ? " and prevents the user from rejoining" : "") + ". " + (ban ? " and prevents the user from rejoining" : "") + ". "
+ (ForceReason ? "L" : "Optionally l") + "ogs the reason for the " + (ForceReason ? "L" : "Optionally l") + "ogs the reason for the "
@ -80,7 +80,7 @@ abstract class BanKick : CommandConfig {
private readonly string _usage; private readonly string _usage;
protected override string DefaultUsageMsg => _usage; protected override string DefaultUsageMsg => _usage;
// Usage: (command) (mention) (reason) // Usage: (command) (user) (reason)
public override async Task Invoke(SocketGuild g, SocketMessage msg) { public override async Task Invoke(SocketGuild g, SocketMessage msg) {
var line = msg.Content.Split(new char[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries); var line = msg.Content.Split(new char[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries);
string targetstr; string targetstr;

View file

@ -0,0 +1,76 @@
using RegexBot.Common;
namespace RegexBot.Modules.ModCommands.Commands;
// Note and Warn commands are highly similar in implementnation, and thus are handled in a single class.
class Note : NoteWarn {
public Note(ModCommands module, JObject config) : base(module, config) { }
protected override string DefaultUsageMsg => string.Format(_usageHeader, Command)
+ "Appends a note to the moderation log for the given user.";
protected override async Task ContinueInvoke(SocketGuild g, SocketMessage msg, string logMessage, SocketUser targetUser) {
var result = await Module.Bot.AddUserNoteAsync(g, targetUser.Id, msg.Author.AsEntityNameString(), logMessage);
await msg.Channel.SendMessageAsync($":white_check_mark: Note \\#{result.LogId} logged for {targetUser}.");
}
}
class Warn : NoteWarn {
public Warn(ModCommands module, JObject config) : base(module, config) { }
protected override string DefaultUsageMsg => string.Format(_usageHeader, Command)
+ "Issues a warning to the given user, logging the instance to this bot's moderation log "
+ "and notifying the offending user over DM of the issued warning.";
protected override async Task ContinueInvoke(SocketGuild g, SocketMessage msg, string logMessage, SocketUser targetUser) {
// Won't warn a bot
if (targetUser.IsBot) {
await SendUsageMessageAsync(msg.Channel, ":x: I don't want to do that. If you must, please warn bots manually.");
return;
}
var (_, result) = await Module.Bot.AddUserWarnAsync(g, targetUser.Id, msg.Author.AsEntityNameString(), logMessage);
await msg.Channel.SendMessageAsync(result.GetResultString());
}
}
abstract class NoteWarn : CommandConfig {
protected string? SuccessMessage { get; }
protected const string _usageHeader = "{0} `user ID or tag` `message`\n";
// Configuration:
// "SuccessMessage" - string; Additional message to display on command success.
protected NoteWarn(ModCommands module, JObject config) : base(module, config) {
SuccessMessage = config[nameof(SuccessMessage)]?.Value<string>();
}
// Usage: (command) (user) (message)
public override async Task Invoke(SocketGuild g, SocketMessage msg) {
var line = msg.Content.Split(new char[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries);
if (line.Length != 3) {
await SendUsageMessageAsync(msg.Channel, ":x: Not all required parameters were specified.");
return;
}
var targetstr = line[1];
var logMessage = line[2];
// Get target user. Required to find for our purposes.
var targetQuery = Module.Bot.EcQueryGuildUser(g.Id, targetstr);
ulong targetId;
if (targetQuery != null) targetId = (ulong)targetQuery.UserId;
else if (ulong.TryParse(targetstr, out var parsed)) targetId = parsed;
else {
await SendUsageMessageAsync(msg.Channel, TargetNotFound);
return;
}
var targetUser = g.GetUser(targetId);
// Go to specific action
try {
await ContinueInvoke(g, msg, logMessage, targetUser);
} catch (Discord.Net.HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.Forbidden) {
await msg.Channel.SendMessageAsync(":x: " + Messages.ForbiddenGenericError);
}
}
protected abstract Task ContinueInvoke(SocketGuild g, SocketMessage msg, string logMessage, SocketUser targetUser);
}

View file

@ -33,6 +33,9 @@ class ModuleConfig {
{ "kick", typeof(Kick) }, { "kick", typeof(Kick) },
{ "say", typeof(Say) }, { "say", typeof(Say) },
{ "unban", typeof(Unban) }, { "unban", typeof(Unban) },
{ "note", typeof(Note) },
{ "addnote", typeof(Note) },
{ "warn", typeof(Warn) },
{ "addrole", typeof(RoleAdd) }, { "addrole", typeof(RoleAdd) },
{ "roleadd", typeof(RoleAdd) }, { "roleadd", typeof(RoleAdd) },
{ "delrole", typeof(RoleDel) }, { "delrole", typeof(RoleDel) },