0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-25 12:45:18 +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 _) { private async void Log(string source, string level, string text, ConsoleColor _) {
try { try {
if (DiscordClient != null && LogChannel != null) { if (DiscordClient != null && LogChannel != null) {
await DiscordClient.SendMessageAsync(LogChannel, foreach (string mesg in SplitMessage(Logger.PrefixNewLines(text, $"{level} [{source}]"), 1994)) //room for 6 '`'
$"```{Logger.PrefixNewLines(text, $"{level} [{source}]")}```"); await DiscordClient.SendMessageAsync(LogChannel, $"```{mesg}```");
} }
} catch (Exception e) { } catch (Exception e) {
// don't log again, it'll just stack overflow the server! // don't log again, it'll just stack overflow the server!
@ -83,18 +93,25 @@ public class DiscordBot {
if (args.Author.IsCurrent) return; if (args.Author.IsCurrent) return;
try { try {
DiscordMessage msg = args.Message; DiscordMessage msg = args.Message;
string? resp = null;
if (string.IsNullOrEmpty(Prefix)) { if (string.IsNullOrEmpty(Prefix)) {
await msg.Channel.TriggerTypingAsync(); await msg.Channel.TriggerTypingAsync();
await msg.RespondAsync(string.Join('\n', resp = string.Join('\n', CommandHandler.GetResult(msg.Content).ReturnStrings);
CommandHandler.GetResult(msg.Content).ReturnStrings));
} else if (msg.Content.StartsWith(Prefix)) { } else if (msg.Content.StartsWith(Prefix)) {
await msg.Channel.TriggerTypingAsync(); await msg.Channel.TriggerTypingAsync();
await msg.RespondAsync(string.Join('\n', resp = string.Join('\n', CommandHandler.GetResult(msg.Content[Prefix.Length..]).ReturnStrings);
CommandHandler.GetResult(msg.Content[Prefix.Length..]).ReturnStrings));
} else if (msg.Content.StartsWith(mentionPrefix)) { } else if (msg.Content.StartsWith(mentionPrefix)) {
await msg.Channel.TriggerTypingAsync(); await msg.Channel.TriggerTypingAsync();
await msg.RespondAsync(string.Join('\n', resp = string.Join('\n', CommandHandler.GetResult(msg.Content[mentionPrefix.Length..]).ReturnStrings);
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) { } catch (Exception e) {
Logger.Error(e); Logger.Error(e);

View file

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