Reorganize commonly used code
This commit is contained in:
parent
f8fe48766b
commit
2f823a3730
9 changed files with 18 additions and 15 deletions
|
@ -30,7 +30,7 @@ public class AutoResponder : RegexbotModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task DiscordClient_MessageReceived(SocketMessage arg) {
|
private async Task DiscordClient_MessageReceived(SocketMessage arg) {
|
||||||
if (!Common.Misc.IsValidUserMessage(arg, out var ch)) return;
|
if (!Common.Utilities.IsValidUserMessage(arg, out var ch)) return;
|
||||||
|
|
||||||
var definitions = GetGuildState<IEnumerable<Definition>>(ch.Guild.Id);
|
var definitions = GetGuildState<IEnumerable<Definition>>(ch.Guild.Id);
|
||||||
if (definitions == null) return; // No configuration in this guild; do no further processing
|
if (definitions == null) return; // No configuration in this guild; do no further processing
|
||||||
|
|
|
@ -38,7 +38,7 @@ class Definition {
|
||||||
var regexRules = new List<Regex>();
|
var regexRules = new List<Regex>();
|
||||||
List<string> inputs;
|
List<string> inputs;
|
||||||
try {
|
try {
|
||||||
inputs = Misc.LoadStringOrStringArray(def[nameof(Regex)]);
|
inputs = Utilities.LoadStringOrStringArray(def[nameof(Regex)]);
|
||||||
} catch (ArgumentNullException) {
|
} catch (ArgumentNullException) {
|
||||||
throw new ModuleLoadException(ErrNoRegex + errpostfx);
|
throw new ModuleLoadException(ErrNoRegex + errpostfx);
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ class Definition {
|
||||||
// Reply options
|
// Reply options
|
||||||
var replyConf = def[nameof(Reply)];
|
var replyConf = def[nameof(Reply)];
|
||||||
try {
|
try {
|
||||||
Reply = Misc.LoadStringOrStringArray(replyConf);
|
Reply = Utilities.LoadStringOrStringArray(replyConf);
|
||||||
haveResponse = Reply.Count > 0;
|
haveResponse = Reply.Count > 0;
|
||||||
} catch (ArgumentNullException) {
|
} catch (ArgumentNullException) {
|
||||||
Reply = Array.Empty<string>();
|
Reply = Array.Empty<string>();
|
||||||
|
|
|
@ -50,7 +50,7 @@ class ConfDefinition {
|
||||||
var regexRules = new List<Regex>();
|
var regexRules = new List<Regex>();
|
||||||
List<string> regexStrings;
|
List<string> regexStrings;
|
||||||
try {
|
try {
|
||||||
regexStrings = Misc.LoadStringOrStringArray(def[nameof(Regex)]);
|
regexStrings = Utilities.LoadStringOrStringArray(def[nameof(Regex)]);
|
||||||
} catch (ArgumentNullException) {
|
} catch (ArgumentNullException) {
|
||||||
throw new ModuleLoadException($"No patterns were defined under '{nameof(Regex)}'{errpostfx}");
|
throw new ModuleLoadException($"No patterns were defined under '{nameof(Regex)}'{errpostfx}");
|
||||||
} catch (ArgumentException) {
|
} catch (ArgumentException) {
|
||||||
|
@ -75,7 +75,7 @@ class ConfDefinition {
|
||||||
|
|
||||||
// Load response(s) and response settings
|
// Load response(s) and response settings
|
||||||
try {
|
try {
|
||||||
Response = Misc.LoadStringOrStringArray(def[nameof(Response)]).AsReadOnly();
|
Response = Utilities.LoadStringOrStringArray(def[nameof(Response)]).AsReadOnly();
|
||||||
} catch (ArgumentNullException) {
|
} catch (ArgumentNullException) {
|
||||||
throw new ModuleLoadException($"No responses were defined under '{nameof(Response)}'{errpostfx}");
|
throw new ModuleLoadException($"No responses were defined under '{nameof(Response)}'{errpostfx}");
|
||||||
} catch (ArgumentException) {
|
} catch (ArgumentException) {
|
||||||
|
|
|
@ -36,7 +36,7 @@ public class RegexModerator : RegexbotModule {
|
||||||
/// Does initial message checking before further processing.
|
/// Does initial message checking before further processing.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private async Task ReceiveIncomingMessage(SocketMessage msg) {
|
private async Task ReceiveIncomingMessage(SocketMessage msg) {
|
||||||
if (!Common.Misc.IsValidUserMessage(msg, out var ch)) return;
|
if (!Common.Utilities.IsValidUserMessage(msg, out var ch)) return;
|
||||||
|
|
||||||
// Get config?
|
// Get config?
|
||||||
var defs = GetGuildState<IEnumerable<ConfDefinition>>(ch.Guild.Id);
|
var defs = GetGuildState<IEnumerable<ConfDefinition>>(ch.Guild.Id);
|
||||||
|
|
|
@ -10,8 +10,6 @@ namespace RegexBot.Modules.RegexModerator;
|
||||||
class ResponseExecutor {
|
class ResponseExecutor {
|
||||||
delegate Task<ResponseResult> ResponseHandler(string? parameter);
|
delegate Task<ResponseResult> ResponseHandler(string? parameter);
|
||||||
|
|
||||||
const string ForbiddenGenericError = "Failed to perform the action due to a permissions issue.";
|
|
||||||
|
|
||||||
private readonly ConfDefinition _rule;
|
private readonly ConfDefinition _rule;
|
||||||
private readonly RegexbotClient _bot;
|
private readonly RegexbotClient _bot;
|
||||||
|
|
||||||
|
@ -69,7 +67,7 @@ class ResponseExecutor {
|
||||||
var result = await runLine(param);
|
var result = await runLine(param);
|
||||||
_reports.Add((cmd, result));
|
_reports.Add((cmd, result));
|
||||||
} catch (Discord.Net.HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.Forbidden) {
|
} catch (Discord.Net.HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.Forbidden) {
|
||||||
_reports.Add((cmd, FromError(ForbiddenGenericError)));
|
_reports.Add((cmd, FromError(Strings.ForbiddenGenericError)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
6
RegexBot/Common/Strings.cs
Normal file
6
RegexBot/Common/Strings.cs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
/// <summary>
|
||||||
|
/// Commonly used strings used throughout the program and modules.
|
||||||
|
/// </summary>
|
||||||
|
public static class Strings {
|
||||||
|
public const string ForbiddenGenericError = "Failed to perform the action due to a permissions issue.";
|
||||||
|
}
|
|
@ -5,9 +5,9 @@ using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
namespace RegexBot.Common;
|
namespace RegexBot.Common;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Miscellaneous useful functions that don't have a particular place anywhere else.
|
/// Miscellaneous utility methods useful for the bot and modules.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Misc {
|
public static class Utilities {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Performs common checks on the specified message to see if it fits all the criteria of a
|
/// Performs common checks on the specified message to see if it fits all the criteria of a
|
||||||
/// typical, ordinary message sent by an ordinary guild user.
|
/// typical, ordinary message sent by an ordinary guild user.
|
|
@ -1,5 +1,4 @@
|
||||||
using Discord.Net;
|
using Discord.Net;
|
||||||
using static RegexBot.RegexbotClient;
|
|
||||||
|
|
||||||
// Instances of this class are created by CommonFunctionService and are meant to be sent to modules,
|
// 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.
|
// therefore we put this in the root RegexBot namespace despite being specific to this service.
|
||||||
|
@ -87,7 +86,7 @@ public class BanKickResult {
|
||||||
if (OperationSuccess) msg += "Kicked";
|
if (OperationSuccess) msg += "Kicked";
|
||||||
else msg += "kick";
|
else msg += "kick";
|
||||||
} else {
|
} else {
|
||||||
throw new System.InvalidOperationException("Cannot create a message for removal type of None.");
|
throw new InvalidOperationException("Cannot create a message for removal type of None.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_rptTargetId != 0) {
|
if (_rptTargetId != 0) {
|
||||||
|
@ -105,7 +104,7 @@ public class BanKickResult {
|
||||||
if (!MessageSendSuccess) msg += "\n(User was unable to receive notification message.)";
|
if (!MessageSendSuccess) msg += "\n(User was unable to receive notification message.)";
|
||||||
} else {
|
} else {
|
||||||
if (ErrorNotFound) msg += ": The specified user could not be found.";
|
if (ErrorNotFound) msg += ": The specified user could not be found.";
|
||||||
else if (ErrorForbidden) msg += ": I do not have the required permissions to perform that action.";
|
else if (ErrorForbidden) msg += ": " + Strings.ForbiddenGenericError;
|
||||||
}
|
}
|
||||||
|
|
||||||
return msg;
|
return msg;
|
||||||
|
|
|
@ -22,7 +22,7 @@ class MessageCachingSubservice {
|
||||||
=> AddOrUpdateCacheItemAsync(arg2, true);
|
=> AddOrUpdateCacheItemAsync(arg2, true);
|
||||||
|
|
||||||
private async Task AddOrUpdateCacheItemAsync(SocketMessage arg, bool isUpdate) {
|
private async Task AddOrUpdateCacheItemAsync(SocketMessage arg, bool isUpdate) {
|
||||||
if (!Common.Misc.IsValidUserMessage(arg, out _)) return;
|
if (!Common.Utilities.IsValidUserMessage(arg, out _)) return;
|
||||||
|
|
||||||
using var db = new BotDatabaseContext();
|
using var db = new BotDatabaseContext();
|
||||||
CachedGuildMessage? cachedMsg = db.GuildMessageCache.Where(m => m.MessageId == (long)arg.Id).SingleOrDefault();
|
CachedGuildMessage? cachedMsg = db.GuildMessageCache.Where(m => m.MessageId == (long)arg.Id).SingleOrDefault();
|
||||||
|
|
Loading…
Reference in a new issue