From 1d24e2d839cb1c9d6ff25a8a0b07faed3cbc414a Mon Sep 17 00:00:00 2001 From: Noikoio Date: Sat, 2 Dec 2017 22:14:00 -0800 Subject: [PATCH] Preparing to add additional features --- Module/ModTools/CommandBase.cs | 4 ++ Module/ModTools/ModTools.cs | 84 +++++++++++++++++----------------- 2 files changed, 47 insertions(+), 41 deletions(-) diff --git a/Module/ModTools/CommandBase.cs b/Module/ModTools/CommandBase.cs index d63cdc1..5b8a533 100644 --- a/Module/ModTools/CommandBase.cs +++ b/Module/ModTools/CommandBase.cs @@ -11,6 +11,10 @@ using System.Threading.Tasks; namespace Noikoio.RegexBot.Module.ModTools { + /// + /// Base class for ModTools command. + /// We are not using Discord.Net's Commands extension, as it does not allow for changes during runtime. + /// [DebuggerDisplay("{Label}-type command")] abstract class CommandBase { diff --git a/Module/ModTools/ModTools.cs b/Module/ModTools/ModTools.cs index 0d2cc78..a5fddde 100644 --- a/Module/ModTools/ModTools.cs +++ b/Module/ModTools/ModTools.cs @@ -1,20 +1,19 @@ -using Discord.WebSocket; +using Discord; +using Discord.WebSocket; using Newtonsoft.Json.Linq; using Noikoio.RegexBot.ConfigItem; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; -using System.Reflection; using System.Threading.Tasks; namespace Noikoio.RegexBot.Module.ModTools { /// - /// ModTools module object. - /// Implements moderation commands that are individually defined and enabled in configuration. + /// ModTools module. + /// This class manages reading configuration and creating instances based on it. /// - // We are not using Discord.Net's Commands extension, as it does not allow for changes during runtime. class ModTools : BotModule { public override string Name => "ModTools"; @@ -26,47 +25,14 @@ namespace Noikoio.RegexBot.Module.ModTools private async Task Client_MessageReceived(SocketMessage arg) { - // Disregard if not in a guild - SocketGuild g = (arg.Author as SocketGuildUser)?.Guild; - if (g == null) return; - - // Get guild config - ServerConfig sc = RegexBot.Config.Servers.FirstOrDefault(s => s.Id == g.Id); - if (sc == null) return; - - // Disregard if not a bot moderator - if (!sc.Moderators.ExistsInList(arg)) return; - - // Disregard if the message contains a newline character - if (arg.Content.Contains("\n")) return; - - // Check for and invoke command... - string cmdchk; - int spc = arg.Content.IndexOf(' '); - if (spc != -1) cmdchk = arg.Content.Substring(0, spc); - else cmdchk = arg.Content; - if (((IDictionary)GetConfig(g.Id)).TryGetValue(cmdchk, out var c)) - { - // ...on the thread pool. - await Task.Run(async () => - { - try - { - await Log($"'{c.Label}' invoked by {arg.Author.ToString()} in {g.Name}/#{arg.Channel.Name}"); - await c.Invoke(g, arg); - } - catch (Exception ex) - { - await Log($"Encountered an error for the command '{c.Label}'. Details follow:"); - await Log(ex.ToString()); - } - }); - } + await CommandCheckInvoke(arg); } [ConfigSection("modtools")] public override async Task ProcessConfiguration(JToken configSection) { + // TODO: put command definitions elsewhere, not in root of this config + var commands = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var def in configSection.Children()) @@ -85,5 +51,41 @@ namespace Noikoio.RegexBot.Module.ModTools } public new Task Log(string text) => base.Log(text); + + private async Task CommandCheckInvoke(SocketMessage arg) + { + // Disregard if not in a guild + SocketGuild g = (arg.Author as SocketGuildUser)?.Guild; + if (g == null) return; + + // Get guild config + ServerConfig sc = RegexBot.Config.Servers.FirstOrDefault(s => s.Id == g.Id); + if (sc == null) return; + + // Disregard if not a bot moderator + if (!sc.Moderators.ExistsInList(arg)) return; + + // Disregard if the message contains a newline character + if (arg.Content.Contains("\n")) return; + + // Check for and invoke command + string cmdchk; + int spc = arg.Content.IndexOf(' '); + if (spc != -1) cmdchk = arg.Content.Substring(0, spc); + else cmdchk = arg.Content; + if (((IDictionary)GetConfig(g.Id)).TryGetValue(cmdchk, out var c)) + { + try + { + await Log($"'{c.Label}' invoked by {arg.Author.ToString()} in {g.Name}/#{arg.Channel.Name}"); + await c.Invoke(g, arg); + } + catch (Exception ex) + { + await Log($"Encountered an error for the command '{c.Label}'. Details follow:"); + await Log(ex.ToString()); + } + } + } } }