From 41d9c84d76a667e200c3f2995aa996937db3c3f0 Mon Sep 17 00:00:00 2001 From: Ske Date: Sat, 20 Apr 2019 22:36:54 +0200 Subject: [PATCH] bot: add generic runtime error handler --- PluralKit/Bot.cs | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/PluralKit/Bot.cs b/PluralKit/Bot.cs index 8d578d14..02e12e63 100644 --- a/PluralKit/Bot.cs +++ b/PluralKit/Bot.cs @@ -113,27 +113,38 @@ namespace PluralKit private async Task MessageReceived(SocketMessage _arg) { - // Ignore system messages (member joined, message pinned, etc) - var arg = _arg as SocketUserMessage; - if (arg == null) return; + try { + // Ignore system messages (member joined, message pinned, etc) + var arg = _arg as SocketUserMessage; + if (arg == null) return; - // Ignore bot messages - if (arg.Author.IsBot || arg.Author.IsWebhook) return; + // Ignore bot messages + if (arg.Author.IsBot || arg.Author.IsWebhook) return; - int argPos = 0; - // Check if message starts with the command prefix - if (arg.HasStringPrefix("pk;", ref argPos) || arg.HasStringPrefix("pk!", ref argPos) || arg.HasMentionPrefix(_client.CurrentUser, ref argPos)) - { - // If it does, fetch the sender's system (because most commands need that) into the context, - // and start command execution - var system = await _connection.QueryFirstAsync("select systems.* from systems, accounts where accounts.uid = @Id and systems.id = accounts.system", new { Id = arg.Author.Id }); - await _commands.ExecuteAsync(new PKCommandContext(_client, arg as SocketUserMessage, _connection, system), argPos, _services); - } - else - { - // If not, try proxying anyway - await _proxy.HandleMessageAsync(arg); + int argPos = 0; + // Check if message starts with the command prefix + if (arg.HasStringPrefix("pk;", ref argPos) || arg.HasStringPrefix("pk!", ref argPos) || arg.HasMentionPrefix(_client.CurrentUser, ref argPos)) + { + // If it does, fetch the sender's system (because most commands need that) into the context, + // and start command execution + var system = await _connection.QueryFirstAsync("select systems.* from systems, accounts where accounts.uid = @Id and systems.id = accounts.system", new { Id = arg.Author.Id }); + await _commands.ExecuteAsync(new PKCommandContext(_client, arg as SocketUserMessage, _connection, system), argPos, _services); + + } + else + { + // If not, try proxying anyway + await _proxy.HandleMessageAsync(arg); + } + } catch (Exception e) { + // Generic exception handler + HandleRuntimeError(_arg, e); } } + + private void HandleRuntimeError(SocketMessage arg, Exception e) + { + Console.Error.WriteLine(e); + } } } \ No newline at end of file