RegexBot/Module/AutoRespond/AutoRespond.cs

110 lines
4.1 KiB
C#
Raw Normal View History

2017-08-12 06:45:27 +00:00
using Discord.WebSocket;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
2017-09-05 17:26:13 +00:00
using System.Diagnostics;
using System.Threading.Tasks;
2017-11-12 03:12:24 +00:00
namespace Noikoio.RegexBot.Module.AutoRespond
{
/// <summary>
/// Similar to <see cref="AutoMod"/>, but lightweight.
/// Provides the capability to define autoresponses for fun or informational purposes.
/// <para>
/// The major differences between this and <see cref="AutoMod"/> include:
/// <list type="bullet">
/// <item><description>Does not listen for message edits.</description></item>
2017-08-12 06:45:27 +00:00
/// <item><description>Moderators are not exempt from any defined triggers.</description></item>
/// <item><description>Responses are limited to the invoking channel.</description></item>
/// <item><description>Per-channel rate limiting.</description></item>
/// </list>
/// </para>
/// </summary>
2017-11-12 03:12:24 +00:00
partial class AutoRespond : BotModule
{
2017-11-12 03:12:24 +00:00
#region BotModule implementation
public AutoRespond(DiscordSocketClient client) : base(client)
{
2017-08-11 15:23:20 +00:00
client.MessageReceived += Client_MessageReceived;
}
private async Task Client_MessageReceived(SocketMessage arg)
{
// Determine channel type - if not a guild channel, stop.
var ch = arg.Channel as SocketGuildChannel;
if (ch == null) return;
var defs = GetState<IEnumerable<ConfigItem>>(ch.Guild.Id);
2017-08-11 15:23:20 +00:00
if (defs == null) return;
foreach (var def in defs)
await Task.Run(async () => await ProcessMessage(arg, def));
}
public override async Task<object> CreateInstanceState(JToken configSection)
{
if (configSection == null) return null;
2017-09-05 17:18:07 +00:00
var responses = new List<ConfigItem>();
foreach (var def in configSection.Children<JProperty>())
2017-08-12 06:45:27 +00:00
{
2017-08-30 05:39:59 +00:00
// All validation is left to the constructor
2017-09-05 17:18:07 +00:00
var resp = new ConfigItem(def);
2017-08-30 05:39:59 +00:00
responses.Add(resp);
2017-08-12 06:45:27 +00:00
}
if (responses.Count > 0)
await Log($"Loaded {responses.Count} definition(s) from configuration.");
return responses.AsReadOnly();
}
2017-09-05 17:26:13 +00:00
#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;
}
}
}
}
}