RegexBot/Feature/AutoRespond/AutoRespond_Process.cs

61 lines
2.1 KiB
C#
Raw Normal View History

2017-08-11 15:23:20 +00:00
using Discord.WebSocket;
2017-08-12 06:45:27 +00:00
using System.Diagnostics;
2017-08-11 15:23:20 +00:00
using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.AutoRespond
{
partial class AutoRespond
{
private async Task ProcessMessage(SocketMessage msg, ResponseDefinition def)
{
2017-08-12 06:45:27 +00:00
// Checks before executing
if (def.Filter.IsFiltered(msg)) return;
if (!def.RateLimit.AddUsage(msg.Channel.Id)) return;
2017-08-11 15:23:20 +00:00
2017-08-12 06:45:27 +00:00
await Log($"'{def.Label}' triggered in #{msg.Channel.Name} by {msg.Author}");
var (type, text) = def.Response;
2017-08-11 15:23:20 +00:00
2017-08-12 06:45:27 +00:00
if (type == ResponseDefinition.ResponseType.Reply) await ProcessReply(msg, text);
else if (type == ResponseDefinition.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;
}
}
2017-08-11 15:23:20 +00:00
}
}
}