Add note and warn response type
This commit is contained in:
parent
5f00e8b4b2
commit
809197984a
4 changed files with 29 additions and 26 deletions
|
@ -8,6 +8,8 @@ namespace RegexBot.Modules.RegexModerator;
|
||||||
/// Transient helper class which handles response interpreting and execution.
|
/// Transient helper class which handles response interpreting and execution.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
class ResponseExecutor {
|
class ResponseExecutor {
|
||||||
|
private const string ErrParamNeedNone = "This response type does not accept parameters.";
|
||||||
|
private const string ErrParamWrongAmount = "Incorrect number of parameters defined in the response.";
|
||||||
delegate Task<ResponseResult> ResponseHandler(string? parameter);
|
delegate Task<ResponseResult> ResponseHandler(string? parameter);
|
||||||
|
|
||||||
private readonly ConfDefinition _rule;
|
private readonly ConfDefinition _rule;
|
||||||
|
@ -20,6 +22,8 @@ class ResponseExecutor {
|
||||||
private readonly List<(string, ResponseResult)> _reports;
|
private readonly List<(string, ResponseResult)> _reports;
|
||||||
private Action<string> Log { get; }
|
private Action<string> Log { get; }
|
||||||
|
|
||||||
|
private string LogSource => $"Rule '{_rule.Label}'";
|
||||||
|
|
||||||
public ResponseExecutor(ConfDefinition rule, RegexbotClient bot, SocketMessage msg, Action<string> logger) {
|
public ResponseExecutor(ConfDefinition rule, RegexbotClient bot, SocketMessage msg, Action<string> logger) {
|
||||||
_rule = rule;
|
_rule = rule;
|
||||||
_bot = bot;
|
_bot = bot;
|
||||||
|
@ -114,7 +118,7 @@ class ResponseExecutor {
|
||||||
)
|
)
|
||||||
.WithDescription(invokingLine)
|
.WithDescription(invokingLine)
|
||||||
.WithFooter(
|
.WithFooter(
|
||||||
text: $"Rule: {_rule.Label}",
|
text: LogSource,
|
||||||
iconUrl: _bot.DiscordClient.CurrentUser.GetAvatarUrl()
|
iconUrl: _bot.DiscordClient.CurrentUser.GetAvatarUrl()
|
||||||
)
|
)
|
||||||
.WithCurrentTimestamp()
|
.WithCurrentTimestamp()
|
||||||
|
@ -135,10 +139,10 @@ class ResponseExecutor {
|
||||||
private async Task<ResponseResult> CmdBanKick(RemovalType rt, string? parameter) {
|
private async Task<ResponseResult> CmdBanKick(RemovalType rt, string? parameter) {
|
||||||
BanKickResult result;
|
BanKickResult result;
|
||||||
if (rt == RemovalType.Ban) {
|
if (rt == RemovalType.Ban) {
|
||||||
result = await _bot.BanAsync(_guild, $"Rule '{_rule.Label}'", _user.Id,
|
result = await _bot.BanAsync(_guild, LogSource, _user.Id,
|
||||||
_rule.BanPurgeDays, parameter, _rule.NotifyUserOfRemoval);
|
_rule.BanPurgeDays, parameter, _rule.NotifyUserOfRemoval);
|
||||||
} else {
|
} else {
|
||||||
result = await _bot.KickAsync(_guild, $"Rule '{_rule.Label}'", _user.Id,
|
result = await _bot.KickAsync(_guild, LogSource, _user.Id,
|
||||||
parameter, _rule.NotifyUserOfRemoval);
|
parameter, _rule.NotifyUserOfRemoval);
|
||||||
}
|
}
|
||||||
if (result.ErrorForbidden) return FromError(Messages.ForbiddenGenericError);
|
if (result.ErrorForbidden) return FromError(Messages.ForbiddenGenericError);
|
||||||
|
@ -151,10 +155,9 @@ class ResponseExecutor {
|
||||||
private Task<ResponseResult> CmdRoleDel(string? parameter) => CmdRoleManipulation(parameter, false);
|
private Task<ResponseResult> CmdRoleDel(string? parameter) => CmdRoleManipulation(parameter, false);
|
||||||
private async Task<ResponseResult> CmdRoleManipulation(string? parameter, bool add) {
|
private async Task<ResponseResult> CmdRoleManipulation(string? parameter, bool add) {
|
||||||
// parameters: @_, &, reason?
|
// parameters: @_, &, reason?
|
||||||
// TODO add persistence option if/when implemented
|
if (string.IsNullOrWhiteSpace(parameter)) return FromError(ErrParamWrongAmount);
|
||||||
if (string.IsNullOrWhiteSpace(parameter)) return FromError("This response requires parameters.");
|
|
||||||
var param = parameter.Split(' ', 3, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
var param = parameter.Split(' ', 3, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||||
if (param.Length < 2) return FromError("Incorrect number of parameters.");
|
if (param.Length != 2) return FromError(ErrParamWrongAmount);
|
||||||
|
|
||||||
// Find targets
|
// Find targets
|
||||||
SocketGuildUser? tuser;
|
SocketGuildUser? tuser;
|
||||||
|
@ -176,21 +179,17 @@ class ResponseExecutor {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do action
|
// Do action
|
||||||
var rq = new RequestOptions() { AuditLogReason = $"Rule '{_rule.Label}'" };
|
var rq = new RequestOptions() { AuditLogReason = LogSource };
|
||||||
if (param.Length == 3 && !string.IsNullOrWhiteSpace(param[2])) {
|
|
||||||
rq.AuditLogReason += " - " + param[2];
|
|
||||||
}
|
|
||||||
if (add) await tuser.AddRoleAsync(trole, rq);
|
if (add) await tuser.AddRoleAsync(trole, rq);
|
||||||
else await tuser.RemoveRoleAsync(trole, rq);
|
else await tuser.RemoveRoleAsync(trole, rq);
|
||||||
return FromSuccess($"{(add ? "Set" : "Unset")} {trole.Mention}.");
|
return FromSuccess($"{(add ? "Set" : "Unset")} {trole.Mention}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<ResponseResult> CmdDelete(string? parameter) {
|
private async Task<ResponseResult> CmdDelete(string? parameter) {
|
||||||
// TODO detailed audit log deletion reason?
|
if (!string.IsNullOrWhiteSpace(parameter)) return FromError(ErrParamNeedNone);
|
||||||
if (parameter != null) return FromError("This response does not accept parameters.");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await _msg.DeleteAsync(new RequestOptions { AuditLogReason = $"Rule {_rule.Label}" });
|
await _msg.DeleteAsync(new RequestOptions { AuditLogReason = LogSource });
|
||||||
return FromSuccess();
|
return FromSuccess();
|
||||||
} catch (Discord.Net.HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.NotFound) {
|
} catch (Discord.Net.HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.NotFound) {
|
||||||
return FromError("The message had already been deleted.");
|
return FromError("The message had already been deleted.");
|
||||||
|
@ -199,9 +198,9 @@ class ResponseExecutor {
|
||||||
|
|
||||||
private async Task<ResponseResult> CmdSay(string? parameter) {
|
private async Task<ResponseResult> CmdSay(string? parameter) {
|
||||||
// parameters: [#_/@_] message
|
// parameters: [#_/@_] message
|
||||||
if (string.IsNullOrWhiteSpace(parameter)) return FromError("This response requires parameters.");
|
if (string.IsNullOrWhiteSpace(parameter)) return FromError(ErrParamWrongAmount);
|
||||||
var param = parameter.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
var param = parameter.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||||
if (param.Length != 2) return FromError("Incorrect number of parameters.");
|
if (param.Length != 2) return FromError(ErrParamWrongAmount);
|
||||||
|
|
||||||
// Get target
|
// Get target
|
||||||
IMessageChannel? targetCh;
|
IMessageChannel? targetCh;
|
||||||
|
@ -233,17 +232,21 @@ class ResponseExecutor {
|
||||||
return FromSuccess($"Sent to {(isUser ? "user DM" : $"<#{targetCh.Id}>")}.");
|
return FromSuccess($"Sent to {(isUser ? "user DM" : $"<#{targetCh.Id}>")}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task<ResponseResult> CmdNote(string? parameter) {
|
private async Task<ResponseResult> CmdNote(string? parameter) {
|
||||||
#warning Not implemented
|
if (string.IsNullOrWhiteSpace(parameter)) return FromError(ErrParamWrongAmount);
|
||||||
return Task.FromResult(FromError("not implemented"));
|
var log = await _bot.AddUserNoteAsync(_guild, _user.Id, LogSource, parameter);
|
||||||
|
return FromSuccess($"Note \\#{log.LogId} logged for {_user}.");
|
||||||
}
|
}
|
||||||
private Task<ResponseResult> CmdTimeout(string? parameter) {
|
private Task<ResponseResult> CmdTimeout(string? parameter) {
|
||||||
#warning Not implemented
|
#warning Not implemented
|
||||||
return Task.FromResult(FromError("not implemented"));
|
return Task.FromResult(FromError("not implemented"));
|
||||||
}
|
}
|
||||||
private Task<ResponseResult> CmdWarn(string? parameter) {
|
private async Task<ResponseResult> CmdWarn(string? parameter) {
|
||||||
#warning Not implemented
|
if (string.IsNullOrWhiteSpace(parameter)) return FromError(ErrParamWrongAmount);
|
||||||
return Task.FromResult(FromError("not implemented"));
|
var (log, result) = await _bot.AddUserWarnAsync(_guild, _user.Id, LogSource, parameter);
|
||||||
|
var resultMsg = $"Warning \\#{log.LogId} logged for {_user}.";
|
||||||
|
if (result.Success) return FromSuccess(resultMsg);
|
||||||
|
else return FromError(resultMsg + " Failed to send DM.");
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ partial class RegexbotClient {
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// The resulting <see cref="ModLogEntry"/> from the creation of this note.
|
/// The resulting <see cref="ModLogEntry"/> from the creation of this note.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public async Task<ModLogEntry> AddUserNote(SocketGuild guild, ulong targetUser, string source, string? message) {
|
public async Task<ModLogEntry> AddUserNoteAsync(SocketGuild guild, ulong targetUser, string source, string? message) {
|
||||||
var entry = new ModLogEntry() {
|
var entry = new ModLogEntry() {
|
||||||
GuildId = (long)guild.Id,
|
GuildId = (long)guild.Id,
|
||||||
UserId = (long)targetUser,
|
UserId = (long)targetUser,
|
||||||
|
|
|
@ -28,18 +28,18 @@ internal partial class CommonFunctionsService : Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task<HttpException?> SendUserWarningAsync(SocketGuildUser target, string? reason) {
|
internal async Task<HttpException?> SendUserWarningAsync(SocketGuildUser target, string? reason) {
|
||||||
const string DMTemplate = "You were warned in {0}";
|
const string DMTemplate = "You have been issued a warning in {0}";
|
||||||
const string DMTemplateReason = " with the following message:\n{1}";
|
const string DMTemplateReason = " with the following message:\n{1}";
|
||||||
|
|
||||||
var outMessage = string.IsNullOrWhiteSpace(reason)
|
var outMessage = string.IsNullOrWhiteSpace(reason)
|
||||||
? string.Format(DMTemplate + ".", target.Guild.Name)
|
? string.Format(DMTemplate + ".", target.Guild.Name)
|
||||||
: string.Format(DMTemplate + DMTemplateReason, target.Guild.Name, reason);
|
: string.Format(DMTemplate + DMTemplateReason, target.Guild.Name, reason);
|
||||||
var dch = await target.CreateDMChannelAsync();
|
|
||||||
try {
|
try {
|
||||||
|
var dch = await target.CreateDMChannelAsync();
|
||||||
await dch.SendMessageAsync(outMessage);
|
await dch.SendMessageAsync(outMessage);
|
||||||
} catch (HttpException ex) {
|
} catch (HttpException ex) {
|
||||||
return ex;
|
return ex;
|
||||||
}
|
}
|
||||||
return default;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -40,7 +40,7 @@ public class LogAppendResult {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string GetResultString() {
|
public string GetResultString() {
|
||||||
var msg = $":white_check_mark: Warning \\#{_logId} logged for {_rptDisplayName}.";
|
var msg = $":white_check_mark: Warning \\#{_logId} logged for {_rptDisplayName}.";
|
||||||
if (!Success) msg += "\n:warning: **User did not receive warning message.** This must be discussed manually.";
|
if (!Success) msg += "\n:warning: **User did not receive warning message.** Consider sending a message manually.";
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue