Try/catch bot msg sending and add exception handlers

This commit is contained in:
Sanae 2022-06-19 19:33:45 -06:00
parent e9136704c1
commit 22a8e91f85
1 changed files with 23 additions and 9 deletions

View File

@ -63,17 +63,31 @@ public class DiscordBot {
SettingsLoadHandler();
string mentionPrefix = $"{DiscordClient.CurrentUser.Mention} ";
DiscordClient.MessageCreated += async (_, args) => {
DiscordMessage msg = args.Message;
if (msg.Content.StartsWith(Prefix)) {
await msg.Channel.TriggerTypingAsync();
await msg.RespondAsync(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));
try {
DiscordMessage msg = args.Message;
if (msg.Content.StartsWith(Prefix)) {
await msg.Channel.TriggerTypingAsync();
await msg.RespondAsync(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));
}
} catch (Exception e) {
Logger.Error(e);
}
};
DiscordClient.ClientErrored += (_, args) => {
Logger.Error("Discord client caught an error in handler!");
Logger.Error(args.Exception);
return Task.CompletedTask;
};
DiscordClient.SocketErrored += (_, args) => {
Logger.Error("Discord client caught an error on socket!");
Logger.Error(args.Exception);
return Task.CompletedTask;
};
} catch (Exception e) {
Logger.Error("Exception occurred in discord runner!");
Logger.Error(e);