0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-22 03:05:16 +00:00

Messages are now split up into lengths small enough to not cause the discord bot to throw an exception with too long a message.

This commit is contained in:
TheUbMunster 2022-07-28 19:16:24 -06:00
parent 66114bdecb
commit d3b8ede229
2 changed files with 31 additions and 8 deletions

View file

@ -46,11 +46,21 @@ public class DiscordBot {
}
}
private static List<string> SplitMessage(string message, int maxSizePerElem = 2000)
{
List<string> result = new List<string>();
for (int i = 0; i < message.Length; i += maxSizePerElem)
{
result.Add(message.Substring(i, message.Length - i < maxSizePerElem ? message.Length - i : maxSizePerElem));
}
return result;
}
private async void Log(string source, string level, string text, ConsoleColor _) {
try {
if (DiscordClient != null && LogChannel != null) {
await DiscordClient.SendMessageAsync(LogChannel,
$"```{Logger.PrefixNewLines(text, $"{level} [{source}]")}```");
foreach (string mesg in SplitMessage(Logger.PrefixNewLines(text, $"{level} [{source}]"), 1994)) //room for 6 '`'
await DiscordClient.SendMessageAsync(LogChannel, $"```{mesg}```");
}
} catch (Exception e) {
// don't log again, it'll just stack overflow the server!
@ -83,18 +93,25 @@ public class DiscordBot {
if (args.Author.IsCurrent) return;
try {
DiscordMessage msg = args.Message;
string? resp = null;
if (string.IsNullOrEmpty(Prefix)) {
await msg.Channel.TriggerTypingAsync();
await msg.RespondAsync(string.Join('\n',
CommandHandler.GetResult(msg.Content).ReturnStrings));
resp = string.Join('\n', CommandHandler.GetResult(msg.Content).ReturnStrings);
} else if (msg.Content.StartsWith(Prefix)) {
await msg.Channel.TriggerTypingAsync();
await msg.RespondAsync(string.Join('\n',
CommandHandler.GetResult(msg.Content[Prefix.Length..]).ReturnStrings));
resp = string.Join('\n', CommandHandler.GetResult(msg.Content[Prefix.Length..]).ReturnStrings);
} else if (msg.Content.StartsWith(mentionPrefix)) {
await msg.Channel.TriggerTypingAsync();
await msg.RespondAsync(string.Join('\n',
CommandHandler.GetResult(msg.Content[mentionPrefix.Length..]).ReturnStrings));
resp = string.Join('\n', CommandHandler.GetResult(msg.Content[mentionPrefix.Length..]).ReturnStrings);
}
if (resp != null)
{
foreach (string mesg in SplitMessage(resp))
await msg.RespondAsync(mesg);
}
else
{
await msg.RespondAsync("Couldn't determine a response for the query.");
}
} catch (Exception e) {
Logger.Error(e);

View file

@ -196,6 +196,12 @@ server.PacketHandler = (c, p) => {
return true;
};
#region Testing
//CommandHandler.RegisterCommand("testreallylongcommand", args => {
// return new string('a', 7000);
//});
#endregion
CommandHandler.RegisterCommand("rejoin", args => {
if (args.Length == 0) {
return "Usage: rejoin <* | usernames...>";