Organized AutoRespond code
This commit is contained in:
parent
3ff2ef28f0
commit
a40c115d87
3 changed files with 72 additions and 65 deletions
|
@ -1,6 +1,7 @@
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Noikoio.RegexBot.Feature.AutoRespond
|
namespace Noikoio.RegexBot.Feature.AutoRespond
|
||||||
|
@ -20,6 +21,7 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
|
||||||
/// </summary>
|
/// </summary>
|
||||||
partial class AutoRespond : BotFeature
|
partial class AutoRespond : BotFeature
|
||||||
{
|
{
|
||||||
|
#region BotFeature implementation
|
||||||
public override string Name => "AutoRespond";
|
public override string Name => "AutoRespond";
|
||||||
|
|
||||||
public AutoRespond(DiscordSocketClient client) : base(client)
|
public AutoRespond(DiscordSocketClient client) : base(client)
|
||||||
|
@ -56,5 +58,55 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
|
||||||
await Log($"Loaded {responses.Count} definition(s) from configuration.");
|
await Log($"Loaded {responses.Count} definition(s) from configuration.");
|
||||||
return responses.AsReadOnly();
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,4 +1,5 @@
|
||||||
using Newtonsoft.Json.Linq;
|
using Discord.WebSocket;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
using Noikoio.RegexBot.ConfigItem;
|
using Noikoio.RegexBot.ConfigItem;
|
||||||
using System;
|
using System;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
@ -97,6 +98,24 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks given message to see if it matches this rule's constraints.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>If true, the rule's response(s) should be executed.</returns>
|
||||||
|
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}'";
|
public override string ToString() => $"Autoresponse definition '{Label}'";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue