From a40c115d87eb2f258a389ea1d29b46d6439ea359 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Tue, 5 Sep 2017 10:26:13 -0700 Subject: [PATCH] Organized AutoRespond code --- Feature/AutoRespond/AutoRespond.cs | 52 ++++++++++++++++++ Feature/AutoRespond/AutoRespond_Process.cs | 64 ---------------------- Feature/AutoRespond/ConfigItem.cs | 21 ++++++- 3 files changed, 72 insertions(+), 65 deletions(-) delete mode 100644 Feature/AutoRespond/AutoRespond_Process.cs diff --git a/Feature/AutoRespond/AutoRespond.cs b/Feature/AutoRespond/AutoRespond.cs index 9897c9b..2de818c 100644 --- a/Feature/AutoRespond/AutoRespond.cs +++ b/Feature/AutoRespond/AutoRespond.cs @@ -1,6 +1,7 @@ using Discord.WebSocket; using Newtonsoft.Json.Linq; using System.Collections.Generic; +using System.Diagnostics; using System.Threading.Tasks; namespace Noikoio.RegexBot.Feature.AutoRespond @@ -20,6 +21,7 @@ namespace Noikoio.RegexBot.Feature.AutoRespond /// partial class AutoRespond : BotFeature { + #region BotFeature implementation public override string Name => "AutoRespond"; public AutoRespond(DiscordSocketClient client) : base(client) @@ -56,5 +58,55 @@ namespace Noikoio.RegexBot.Feature.AutoRespond await Log($"Loaded {responses.Count} definition(s) from configuration."); return responses.AsReadOnly(); } + #endregion + + private async Task ProcessMessage(SocketMessage msg, ConfigItem def) + { + if (!def.Match(msg)) return; + + await Log($"'{def.Label}' triggered by {msg.Author} in {((SocketGuildChannel)msg.Channel).Guild.Name}/#{msg.Channel.Name}"); + + var (type, text) = def.Response; + if (type == ConfigItem.ResponseType.Reply) await ProcessReply(msg, text); + else if (type == ConfigItem.ResponseType.Exec) await ProcessExec(msg, text); + } + + private async Task ProcessReply(SocketMessage msg, string text) + { + await msg.Channel.SendMessageAsync(text); + } + + private async Task ProcessExec(SocketMessage msg, string text) + { + string[] cmdline = text.Split(new char[] { ' ' }, 2); + + ProcessStartInfo ps = new ProcessStartInfo() + { + FileName = cmdline[0], + Arguments = (cmdline.Length == 2 ? cmdline[1] : ""), + UseShellExecute = false, // ??? + CreateNoWindow = true, + RedirectStandardOutput = true + }; + using (Process p = Process.Start(ps)) + { + p.WaitForExit(5000); // waiting at most 5 seconds + if (p.HasExited) + { + if (p.ExitCode != 0) await Log("exec: Process returned exit code " + p.ExitCode); + using (var stdout = p.StandardOutput) + { + var result = await stdout.ReadToEndAsync(); + await msg.Channel.SendMessageAsync(result); + } + } + else + { + await Log("exec: Process is taking too long to exit. Killing process."); + p.Kill(); + return; + } + } + } } } diff --git a/Feature/AutoRespond/AutoRespond_Process.cs b/Feature/AutoRespond/AutoRespond_Process.cs deleted file mode 100644 index aca8689..0000000 --- a/Feature/AutoRespond/AutoRespond_Process.cs +++ /dev/null @@ -1,64 +0,0 @@ -using Discord.WebSocket; -using System.Diagnostics; -using System.Threading.Tasks; - -namespace Noikoio.RegexBot.Feature.AutoRespond -{ - partial class AutoRespond - { - private async Task ProcessMessage(SocketMessage msg, ConfigItem def) - { - // Check filters - if (def.Filter.IsFiltered(msg)) return; - - // Check if the trigger is a match - if (!def.Trigger.IsMatch(msg.Content)) return; - - // Check rate limit - if (!def.RateLimit.AllowUsage(msg.Channel.Id)) return; - - await Log($"'{def.Label}' triggered by {msg.Author} in {((SocketGuildChannel)msg.Channel).Guild.Name}/#{msg.Channel.Name}"); - var (type, text) = def.Response; - if (type == ConfigItem.ResponseType.Reply) await ProcessReply(msg, text); - else if (type == ConfigItem.ResponseType.Exec) await ProcessExec(msg, text); - } - - private async Task ProcessReply(SocketMessage msg, string text) - { - await msg.Channel.SendMessageAsync(text); - } - - private async Task ProcessExec(SocketMessage msg, string text) - { - string[] cmdline = text.Split(new char[] { ' ' }, 2); - - ProcessStartInfo ps = new ProcessStartInfo() - { - FileName = cmdline[0], - Arguments = (cmdline.Length == 2 ? cmdline[1] : ""), - UseShellExecute = false, // ??? - CreateNoWindow = true, - RedirectStandardOutput = true - }; - using (Process p = Process.Start(ps)) - { - p.WaitForExit(5000); // waiting at most 5 seconds - if (p.HasExited) - { - if (p.ExitCode != 0) await Log("exec: Process returned exit code " + p.ExitCode); - using (var stdout = p.StandardOutput) - { - var result = await stdout.ReadToEndAsync(); - await msg.Channel.SendMessageAsync(result); - } - } - else - { - await Log("exec: Process is taking too long to exit. Killing process."); - p.Kill(); - return; - } - } - } - } -} diff --git a/Feature/AutoRespond/ConfigItem.cs b/Feature/AutoRespond/ConfigItem.cs index c489ca5..e48d9a2 100644 --- a/Feature/AutoRespond/ConfigItem.cs +++ b/Feature/AutoRespond/ConfigItem.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json.Linq; +using Discord.WebSocket; +using Newtonsoft.Json.Linq; using Noikoio.RegexBot.ConfigItem; using System; using System.Text.RegularExpressions; @@ -97,6 +98,24 @@ namespace Noikoio.RegexBot.Feature.AutoRespond } } + /// + /// Checks given message to see if it matches this rule's constraints. + /// + /// If true, the rule's response(s) should be executed. + public bool Match(SocketMessage m) + { + // Filter check + if (Filter.IsFiltered(m)) return false; + + // Match check + if (!Trigger.IsMatch(m.Content)) return false; + + // Rate limit check - currently per channel + if (!RateLimit.AllowUsage(m.Channel.Id)) return false; + + return true; + } + public override string ToString() => $"Autoresponse definition '{Label}'"; } }