From 287bb33d77a6e47ba0332c5126d1d924b68bb15b Mon Sep 17 00:00:00 2001 From: Noikoio Date: Wed, 21 Mar 2018 23:27:19 -0700 Subject: [PATCH] Removed manual module name setting Modules will now be named based on their existing class name. Their respective configuration sections are now also defined by the same value. Documentation has been updated to reflect this. --- BotModule.cs | 25 ++--------------- Configuration.cs | 15 ++++------ EntityCache/Module.cs | 2 -- Module/AutoMod/AutoMod.cs | 5 +--- Module/AutoRespond/AutoRespond.cs | 5 +--- Module/DMLogger/DMLogger.cs | 2 -- Module/EntryAutoRole/EntryAutoRole.cs | 5 +--- Module/ModCommands/Commands/BanKick.cs | 6 ++-- Module/ModCommands/Commands/ConfReload.cs | 2 +- .../ModCommands/Commands/RoleManipulation.cs | 6 ++-- Module/ModCommands/Commands/Say.cs | 2 +- Module/ModCommands/Commands/Unban.cs | 2 +- Module/ModCommands/Commands/_CommandBase.cs | 10 +++---- Module/ModCommands/ConfigItem.cs | 2 +- .../{CommandListener.cs => ModCommands.cs} | 7 ++--- Module/ModLogs/EventListener.cs | 28 ------------------- Module/ModLogs/ModLogs.cs | 5 +--- RegexBot.cs | 2 +- docs/automod.md | 2 +- docs/autorespond.md | 2 +- docs/serverdef.md | 4 +-- 21 files changed, 33 insertions(+), 106 deletions(-) rename Module/ModCommands/{CommandListener.cs => ModCommands.cs} (93%) delete mode 100644 Module/ModLogs/EventListener.cs diff --git a/BotModule.cs b/BotModule.cs index 6e1540c..7983dca 100644 --- a/BotModule.cs +++ b/BotModule.cs @@ -13,8 +13,8 @@ namespace Noikoio.RegexBot { private readonly DiscordSocketClient _client; private readonly AsyncLogger _logger; - - public abstract string Name { get; } + + public string Name => this.GetType().Name; protected DiscordSocketClient Client => _client; public BotModule(DiscordSocketClient client) @@ -80,25 +80,4 @@ namespace Noikoio.RegexBot public sealed override int GetHashCode() => base.GetHashCode(); public sealed override string ToString() => base.ToString(); } - - /// - /// Indicates which section under an individual Discord guild configuration should be passed to the - /// module's method during configuration load. - /// - [AttributeUsage(AttributeTargets.Method, Inherited = false)] - public class ConfigSectionAttribute : Attribute - { - private readonly string _sectionName; - - public string SectionName => _sectionName; - - public ConfigSectionAttribute(string sectionName) - { - if (string.IsNullOrWhiteSpace(sectionName)) - { - throw new ArgumentNullException("Configuration section name cannot be blank."); - } - _sectionName = sectionName; - } - } } diff --git a/Configuration.cs b/Configuration.cs index 6592ee5..f371e6c 100644 --- a/Configuration.cs +++ b/Configuration.cs @@ -162,16 +162,12 @@ namespace Noikoio.RegexBot Dictionary customConfs = new Dictionary(); foreach (var item in _bot.Modules) { - var attr = item.GetType().GetTypeInfo() - .GetMethod("ProcessConfiguration").GetCustomAttribute(); - if (attr == null) - { - await SLog("No additional configuration for " + item.Name); - continue; - } - var section = sconf[attr.SectionName]; + var confSection = item.Name; + + var section = sconf[confSection]; if (section == null) { + // Section not in config. Do not call loader method. await SLog("Additional configuration not defined for " + item.Name); continue; } @@ -191,8 +187,7 @@ namespace Noikoio.RegexBot customConfs.Add(item, result); } - - // Switch to using new data + // Switch to new configuration List> rulesfinal = new List>(); newservers.Add(new ServerConfig(sid, mods, new ReadOnlyDictionary(customConfs))); } diff --git a/EntityCache/Module.cs b/EntityCache/Module.cs index 0a4da61..c4945ab 100644 --- a/EntityCache/Module.cs +++ b/EntityCache/Module.cs @@ -15,8 +15,6 @@ namespace Noikoio.RegexBot.EntityCache { private readonly DatabaseConfig _db; - public override string Name => nameof(EntityCache); - public Module(DiscordSocketClient client) : base(client) { if (RegexBot.Config.DatabaseAvailable) diff --git a/Module/AutoMod/AutoMod.cs b/Module/AutoMod/AutoMod.cs index b6158cb..88c4b14 100644 --- a/Module/AutoMod/AutoMod.cs +++ b/Module/AutoMod/AutoMod.cs @@ -16,15 +16,12 @@ namespace Noikoio.RegexBot.Module.AutoMod /// class AutoMod : BotModule { - public override string Name => "AutoMod"; - public AutoMod(DiscordSocketClient client) : base(client) { client.MessageReceived += CMessageReceived; client.MessageUpdated += CMessageUpdated; } - - [ConfigSection("automod")] + public override async Task ProcessConfiguration(JToken configSection) { List rules = new List(); diff --git a/Module/AutoRespond/AutoRespond.cs b/Module/AutoRespond/AutoRespond.cs index ca15c31..e5cfb5a 100644 --- a/Module/AutoRespond/AutoRespond.cs +++ b/Module/AutoRespond/AutoRespond.cs @@ -22,8 +22,6 @@ namespace Noikoio.RegexBot.Module.AutoRespond partial class AutoRespond : BotModule { #region BotModule implementation - public override string Name => "AutoRespond"; - public AutoRespond(DiscordSocketClient client) : base(client) { client.MessageReceived += Client_MessageReceived; @@ -41,8 +39,7 @@ namespace Noikoio.RegexBot.Module.AutoRespond foreach (var def in defs) await Task.Run(async () => await ProcessMessage(arg, def)); } - - [ConfigSection("autoresponses")] + public override async Task ProcessConfiguration(JToken configSection) { var responses = new List(); diff --git a/Module/DMLogger/DMLogger.cs b/Module/DMLogger/DMLogger.cs index ce31b77..66bde53 100644 --- a/Module/DMLogger/DMLogger.cs +++ b/Module/DMLogger/DMLogger.cs @@ -12,8 +12,6 @@ namespace Noikoio.RegexBot.Module.DMLogger /// class DMLogger : BotModule { - public override string Name => nameof(DMLogger); - public DMLogger(DiscordSocketClient client) : base(client) { client.MessageReceived += Client_MessageReceived; diff --git a/Module/EntryAutoRole/EntryAutoRole.cs b/Module/EntryAutoRole/EntryAutoRole.cs index 1d210ec..19b5bec 100644 --- a/Module/EntryAutoRole/EntryAutoRole.cs +++ b/Module/EntryAutoRole/EntryAutoRole.cs @@ -14,8 +14,6 @@ namespace Noikoio.RegexBot.Module.EntryAutoRole /// class EntryAutoRole : BotModule { - public override string Name => "EntryAutoRole"; - private List _roleWaitlist; private object _roleWaitLock = new object(); @@ -37,8 +35,7 @@ namespace Noikoio.RegexBot.Module.EntryAutoRole _workerCancel = new CancellationTokenSource(); Task.Factory.StartNew(Worker, _workerCancel.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); } - - [ConfigSection("EntryAutoRole")] + public override Task ProcessConfiguration(JToken configSection) { if (configSection.Type != JTokenType.Object) diff --git a/Module/ModCommands/Commands/BanKick.cs b/Module/ModCommands/Commands/BanKick.cs index ec644cd..4e520cf 100644 --- a/Module/ModCommands/Commands/BanKick.cs +++ b/Module/ModCommands/Commands/BanKick.cs @@ -25,7 +25,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands // "notifymsg" - Message to send to the target user being acted upon. Default message is used // if the value is not specified. If a blank value is given, the feature is disabled. // Takes the special values $s for server name and $r for reason text. - protected BanKick(CommandListener l, string label, JObject conf, CommandMode mode) : base(l, label, conf) + protected BanKick(ModCommands l, string label, JObject conf, CommandMode mode) : base(l, label, conf) { _mode = mode; _forceReason = conf["forcereason"]?.Value() ?? false; @@ -199,13 +199,13 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands class Ban : BanKick { - public Ban(CommandListener l, string label, JObject conf) + public Ban(ModCommands l, string label, JObject conf) : base(l, label, conf, CommandMode.Ban) { } } class Kick : BanKick { - public Kick(CommandListener l, string label, JObject conf) + public Kick(ModCommands l, string label, JObject conf) : base(l, label, conf, CommandMode.Kick) { } } } diff --git a/Module/ModCommands/Commands/ConfReload.cs b/Module/ModCommands/Commands/ConfReload.cs index 22d66df..2a687c4 100644 --- a/Module/ModCommands/Commands/ConfReload.cs +++ b/Module/ModCommands/Commands/ConfReload.cs @@ -7,7 +7,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands class ConfReload : Command { // No configuration. - public ConfReload(CommandListener l, string label, JObject conf) : base(l, label, conf) { } + public ConfReload(ModCommands l, string label, JObject conf) : base(l, label, conf) { } // Usage: (command) public override async Task Invoke(SocketGuild g, SocketMessage msg) diff --git a/Module/ModCommands/Commands/RoleManipulation.cs b/Module/ModCommands/Commands/RoleManipulation.cs index e6a0769..94f22d2 100644 --- a/Module/ModCommands/Commands/RoleManipulation.cs +++ b/Module/ModCommands/Commands/RoleManipulation.cs @@ -19,7 +19,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands // "role" - string; The given role that applies to this command. // "successmsg" - string; Messages to display on command success. Overrides default. - protected RoleManipulation(CommandListener l, string label, JObject conf, CommandMode mode) : base(l, label, conf) + protected RoleManipulation(ModCommands l, string label, JObject conf, CommandMode mode) : base(l, label, conf) { _mode = mode; var rolestr = conf["role"]?.Value(); @@ -123,11 +123,11 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands class RoleAdd : RoleManipulation { - public RoleAdd(CommandListener l, string label, JObject conf) : base(l, label, conf, CommandMode.Add) { } + public RoleAdd(ModCommands l, string label, JObject conf) : base(l, label, conf, CommandMode.Add) { } } class RoleDel : RoleManipulation { - public RoleDel(CommandListener l, string label, JObject conf) : base(l, label, conf, CommandMode.Del) { } + public RoleDel(ModCommands l, string label, JObject conf) : base(l, label, conf, CommandMode.Del) { } } } diff --git a/Module/ModCommands/Commands/Say.cs b/Module/ModCommands/Commands/Say.cs index ea8518a..110e8d6 100644 --- a/Module/ModCommands/Commands/Say.cs +++ b/Module/ModCommands/Commands/Say.cs @@ -9,7 +9,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands { // No configuration at the moment. // TODO: Whitelist/blacklist - to limit which channels it can "say" into - public Say(CommandListener l, string label, JObject conf) : base(l, label, conf) { + public Say(ModCommands l, string label, JObject conf) : base(l, label, conf) { DefaultUsageMsg = $"{this.Trigger} [channel] [message]\n" + "Displays the given message exactly as specified to the given channel."; } diff --git a/Module/ModCommands/Commands/Unban.cs b/Module/ModCommands/Commands/Unban.cs index a2e74bf..4be1223 100644 --- a/Module/ModCommands/Commands/Unban.cs +++ b/Module/ModCommands/Commands/Unban.cs @@ -10,7 +10,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands // No configuration. // TODO bring in some options from BanKick. Particularly custom success msg. // TODO when ModLogs fully implemented, add a reason? - public Unban(CommandListener l, string label, JObject conf) : base(l, label, conf) { + public Unban(ModCommands l, string label, JObject conf) : base(l, label, conf) { DefaultUsageMsg = $"{this.Trigger} [user or user ID]\n" + "Unbans the given user, allowing them to rejoin the server."; } diff --git a/Module/ModCommands/Commands/_CommandBase.cs b/Module/ModCommands/Commands/_CommandBase.cs index fa58f9b..9525ebf 100644 --- a/Module/ModCommands/Commands/_CommandBase.cs +++ b/Module/ModCommands/Commands/_CommandBase.cs @@ -16,20 +16,20 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands /// /// Base class for a command within the module. /// After implementing, don't forget to add a reference to - /// . + /// . /// [DebuggerDisplay("Command def: {Label}")] abstract class Command { - private readonly CommandListener _mod; + private readonly ModCommands _mod; private readonly string _label; private readonly string _command; - protected CommandListener Module => _mod; + protected ModCommands Module => _mod; public string Label => _label; public string Trigger => _command; - public Command(CommandListener l, string label, JObject conf) + public Command(ModCommands l, string label, JObject conf) { _mod = l; _label = label; @@ -58,7 +58,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands { "delrole", typeof(RoleDel) } }); - public static Command CreateInstance(CommandListener root, JProperty def) + public static Command CreateInstance(ModCommands root, JProperty def) { string label = def.Name; if (string.IsNullOrWhiteSpace(label)) throw new RuleImportException("Label cannot be blank."); diff --git a/Module/ModCommands/ConfigItem.cs b/Module/ModCommands/ConfigItem.cs index 7657dff..4d8eeda 100644 --- a/Module/ModCommands/ConfigItem.cs +++ b/Module/ModCommands/ConfigItem.cs @@ -16,7 +16,7 @@ namespace Noikoio.RegexBot.Module.ModCommands public ReadOnlyDictionary Commands => _cmdInstances; - public ConfigItem(CommandListener instance, JToken inconf) + public ConfigItem(ModCommands instance, JToken inconf) { if (inconf.Type != JTokenType.Object) { diff --git a/Module/ModCommands/CommandListener.cs b/Module/ModCommands/ModCommands.cs similarity index 93% rename from Module/ModCommands/CommandListener.cs rename to Module/ModCommands/ModCommands.cs index 884e31c..82e4773 100644 --- a/Module/ModCommands/CommandListener.cs +++ b/Module/ModCommands/ModCommands.cs @@ -17,11 +17,9 @@ namespace Noikoio.RegexBot.Module.ModCommands /// done in a way that would easily allow for flexibility and modifications during runtime. /// Thus, reinventing the wheel right here. /// - class CommandListener : BotModule + class ModCommands : BotModule { - public override string Name => "ModCommands"; - - public CommandListener(DiscordSocketClient client) : base(client) + public ModCommands(DiscordSocketClient client) : base(client) { client.MessageReceived += Client_MessageReceived; } @@ -34,7 +32,6 @@ namespace Noikoio.RegexBot.Module.ModCommands if (arg.Channel is IGuildChannel) await CommandCheckInvoke(arg); } - [ConfigSection("ModCommands")] public override async Task ProcessConfiguration(JToken configSection) { // Constructor throws exception on config errors diff --git a/Module/ModLogs/EventListener.cs b/Module/ModLogs/EventListener.cs deleted file mode 100644 index 2ed1932..0000000 --- a/Module/ModLogs/EventListener.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using Discord.WebSocket; -using Newtonsoft.Json.Linq; - -namespace Noikoio.RegexBot.Module.ModLogs -{ - /// - /// Listens for Discord-based events and writes them to the log (database). - /// Additionally writes certain messages to a designated logging channel if configured. - /// - class EventListener : BotModule - { - public override string Name => "ModLogs"; - public EventListener(DiscordSocketClient client) : base(client) - { - - } - - [ConfigSection("modlogs")] - public override Task ProcessConfiguration(JToken configSection) - { - throw new NotImplementedException(); - } - } -} diff --git a/Module/ModLogs/ModLogs.cs b/Module/ModLogs/ModLogs.cs index 2024510..7f4c130 100644 --- a/Module/ModLogs/ModLogs.cs +++ b/Module/ModLogs/ModLogs.cs @@ -11,8 +11,6 @@ namespace Noikoio.RegexBot.Module.ModLogs /// class ModLogs : BotModule { - public override string Name => "ModLogs"; - private readonly MessageCache _msgCacheInstance; public ModLogs(DiscordSocketClient client) : base(client) @@ -26,8 +24,7 @@ namespace Noikoio.RegexBot.Module.ModLogs // TODO add handlers for detecting joins, leaves, bans, kicks, user edits (nick/username/discr) // TODO add handler for processing the log query command } - - [ConfigSection("ModLogs")] + public override async Task ProcessConfiguration(JToken configSection) { if (configSection.Type != JTokenType.Object) diff --git a/RegexBot.cs b/RegexBot.cs index d8e6a03..abd4620 100644 --- a/RegexBot.cs +++ b/RegexBot.cs @@ -56,7 +56,7 @@ namespace Noikoio.RegexBot { new Module.DMLogger.DMLogger(_client), new Module.AutoMod.AutoMod(_client), - new Module.ModCommands.CommandListener(_client), + new Module.ModCommands.ModCommands(_client), new Module.AutoRespond.AutoRespond(_client), new Module.EntryAutoRole.EntryAutoRole(_client), diff --git a/docs/automod.md b/docs/automod.md index c79067a..2477bf3 100644 --- a/docs/automod.md +++ b/docs/automod.md @@ -6,7 +6,7 @@ AutoMod is set up by defining rules within a JSON object named `automod` within Sample within a [server definition](serverdef.html): ``` -"automod": { +"AutoMod": { "Delete bilingual pirates": { "regex": [ "pira(te|cy)", "pirat(a|ería)", ], "response": [ diff --git a/docs/autorespond.md b/docs/autorespond.md index ba2db8b..6075347 100644 --- a/docs/autorespond.md +++ b/docs/autorespond.md @@ -6,7 +6,7 @@ This may seem like a redundant feature, given that the same can be accomplished Sample within a [server definition](serverdef.html): ``` -"autorespond": { +"AutoRespond": { "Help command": { "regex": "^!help$", "reply": "You can't be helped. Try again in 45 minutes.", diff --git a/docs/serverdef.md b/docs/serverdef.md index b3d794a..06a22c6 100644 --- a/docs/serverdef.md +++ b/docs/serverdef.md @@ -26,8 +26,8 @@ The following is a list of accepted members within a server definition. * id (*integer*) - **Required.** A value containing the server's [unique ID](https://support.discordapp.com/hc/en-us/articles/206346498-Where-can-I-find-my-User-Server-Message-ID-). * name (*string*) - Preferably a readable version of the server's name. Not used for anything other than internal logging. * moderators (*[entity list](entitylist.html)*) - A list of entities to consider as moderators. Actions done by members of this list are able to execute *ModCommands* commands and are exempt from certain *AutoMod* rules. See their respective pages for more details. -* [automod](automod.html) - See respective page. -* [autoresponses](autorespond.html) - See respective page. +* [AutoMod](automod.html) - See respective page. +* [AutoRespond](autorespond.html) - See respective page. * [EntryAutoRole](entryautorole.html) - See respective page. * [ModCommands](modcommands.html) - See respective page. * [ModLogs](modlogs.html) - See respective page. \ No newline at end of file