diff --git a/RegexBot-Modules/AutoResponder/AutoResponder.cs b/RegexBot-Modules/AutoResponder/AutoResponder.cs index c5993d6..239823a 100644 --- a/RegexBot-Modules/AutoResponder/AutoResponder.cs +++ b/RegexBot-Modules/AutoResponder/AutoResponder.cs @@ -30,7 +30,7 @@ public class AutoResponder : RegexbotModule { } 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>(ch.Guild.Id); if (definitions == null) return; // No configuration in this guild; do no further processing diff --git a/RegexBot-Modules/AutoResponder/Definition.cs b/RegexBot-Modules/AutoResponder/Definition.cs index cd0c04f..e31f5f7 100644 --- a/RegexBot-Modules/AutoResponder/Definition.cs +++ b/RegexBot-Modules/AutoResponder/Definition.cs @@ -38,7 +38,7 @@ class Definition { var regexRules = new List(); List inputs; try { - inputs = Misc.LoadStringOrStringArray(def[nameof(Regex)]); + inputs = Utilities.LoadStringOrStringArray(def[nameof(Regex)]); } catch (ArgumentNullException) { throw new ModuleLoadException(ErrNoRegex + errpostfx); } @@ -60,7 +60,7 @@ class Definition { // Reply options var replyConf = def[nameof(Reply)]; try { - Reply = Misc.LoadStringOrStringArray(replyConf); + Reply = Utilities.LoadStringOrStringArray(replyConf); haveResponse = Reply.Count > 0; } catch (ArgumentNullException) { Reply = Array.Empty(); diff --git a/RegexBot-Modules/RegexModerator/ConfDefinition.cs b/RegexBot-Modules/RegexModerator/ConfDefinition.cs index 8b8edef..77ae58d 100644 --- a/RegexBot-Modules/RegexModerator/ConfDefinition.cs +++ b/RegexBot-Modules/RegexModerator/ConfDefinition.cs @@ -50,7 +50,7 @@ class ConfDefinition { var regexRules = new List(); List regexStrings; try { - regexStrings = Misc.LoadStringOrStringArray(def[nameof(Regex)]); + regexStrings = Utilities.LoadStringOrStringArray(def[nameof(Regex)]); } catch (ArgumentNullException) { throw new ModuleLoadException($"No patterns were defined under '{nameof(Regex)}'{errpostfx}"); } catch (ArgumentException) { @@ -75,7 +75,7 @@ class ConfDefinition { // Load response(s) and response settings try { - Response = Misc.LoadStringOrStringArray(def[nameof(Response)]).AsReadOnly(); + Response = Utilities.LoadStringOrStringArray(def[nameof(Response)]).AsReadOnly(); } catch (ArgumentNullException) { throw new ModuleLoadException($"No responses were defined under '{nameof(Response)}'{errpostfx}"); } catch (ArgumentException) { diff --git a/RegexBot-Modules/RegexModerator/RegexModerator.cs b/RegexBot-Modules/RegexModerator/RegexModerator.cs index 1041e27..cb76ac2 100644 --- a/RegexBot-Modules/RegexModerator/RegexModerator.cs +++ b/RegexBot-Modules/RegexModerator/RegexModerator.cs @@ -36,7 +36,7 @@ public class RegexModerator : RegexbotModule { /// Does initial message checking before further processing. /// 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? var defs = GetGuildState>(ch.Guild.Id); diff --git a/RegexBot-Modules/RegexModerator/ResponseExecutor.cs b/RegexBot-Modules/RegexModerator/ResponseExecutor.cs index 4d045e2..f3cb06f 100644 --- a/RegexBot-Modules/RegexModerator/ResponseExecutor.cs +++ b/RegexBot-Modules/RegexModerator/ResponseExecutor.cs @@ -10,8 +10,6 @@ namespace RegexBot.Modules.RegexModerator; class ResponseExecutor { delegate Task ResponseHandler(string? parameter); - const string ForbiddenGenericError = "Failed to perform the action due to a permissions issue."; - private readonly ConfDefinition _rule; private readonly RegexbotClient _bot; @@ -69,7 +67,7 @@ class ResponseExecutor { var result = await runLine(param); _reports.Add((cmd, result)); } catch (Discord.Net.HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.Forbidden) { - _reports.Add((cmd, FromError(ForbiddenGenericError))); + _reports.Add((cmd, FromError(Strings.ForbiddenGenericError))); } } diff --git a/RegexBot/Common/Strings.cs b/RegexBot/Common/Strings.cs new file mode 100644 index 0000000..453b60a --- /dev/null +++ b/RegexBot/Common/Strings.cs @@ -0,0 +1,6 @@ +/// +/// Commonly used strings used throughout the program and modules. +/// +public static class Strings { + public const string ForbiddenGenericError = "Failed to perform the action due to a permissions issue."; +} \ No newline at end of file diff --git a/RegexBot/Common/Misc.cs b/RegexBot/Common/Utilities.cs similarity index 94% rename from RegexBot/Common/Misc.cs rename to RegexBot/Common/Utilities.cs index a709e75..0375466 100644 --- a/RegexBot/Common/Misc.cs +++ b/RegexBot/Common/Utilities.cs @@ -5,9 +5,9 @@ using System.Diagnostics.CodeAnalysis; namespace RegexBot.Common; /// -/// Miscellaneous useful functions that don't have a particular place anywhere else. +/// Miscellaneous utility methods useful for the bot and modules. /// -public static class Misc { +public static class Utilities { /// /// 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. diff --git a/RegexBot/Services/CommonFunctions/BanKickResult.cs b/RegexBot/Services/CommonFunctions/BanKickResult.cs index 265582d..4ecf41d 100644 --- a/RegexBot/Services/CommonFunctions/BanKickResult.cs +++ b/RegexBot/Services/CommonFunctions/BanKickResult.cs @@ -1,5 +1,4 @@ using Discord.Net; -using static RegexBot.RegexbotClient; // 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. @@ -87,7 +86,7 @@ public class BanKickResult { if (OperationSuccess) msg += "Kicked"; else msg += "kick"; } 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) { @@ -105,7 +104,7 @@ public class BanKickResult { if (!MessageSendSuccess) msg += "\n(User was unable to receive notification message.)"; } else { 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; diff --git a/RegexBot/Services/EntityCache/MessageCachingSubservice.cs b/RegexBot/Services/EntityCache/MessageCachingSubservice.cs index c9838f2..dbea345 100644 --- a/RegexBot/Services/EntityCache/MessageCachingSubservice.cs +++ b/RegexBot/Services/EntityCache/MessageCachingSubservice.cs @@ -22,7 +22,7 @@ class MessageCachingSubservice { => AddOrUpdateCacheItemAsync(arg2, true); 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(); CachedGuildMessage? cachedMsg = db.GuildMessageCache.Where(m => m.MessageId == (long)arg.Id).SingleOrDefault();