Implement failsafe for large message output from custom commands

This commit is contained in:
Daniel_I_Am 2020-09-27 15:44:44 +02:00
parent fe5dde6918
commit 838c4847f8
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84

View File

@ -1,4 +1,6 @@
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ChaosBot.Models;
@ -40,7 +42,16 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
string output = task.Result;
if (output.Length > 0)
if (output.Length > 2000)
{
context.Channel.SendMessageAsync($"Command '{customCommand.Command}' has {output.Length} characters of output. That is more than the 2000 limit. Packaging into file...").GetAwaiter().GetResult();
Stream stream = new MemoryStream(Encoding.ASCII.GetBytes(output));
if (stream.Length > 8 * 1024 * 1024)
context.Channel.SendMessageAsync($"Packaged filesize of {stream.Length / 1024 / 1024} MB is too big.");
else
context.Channel.SendFileAsync(stream, "command-output.txt");
}
else if (output.Length > 0)
context.Channel.SendMessageAsync(output);
else
context.Channel.SendMessageAsync($"Command '{customCommand.Command}' had no output.");