Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 2f8eb9e

Browse files
Merge pull request #57 from InfinityGhost/log-command-exceptions
Log exceptions from commands and respond appropriately
2 parents b01f84c + 183d106 commit 2f8eb9e

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

TabletBot.Discord/LogExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23
using Discord;
34
using TabletBot.Common;
45
using LogMessage = Discord.LogMessage;

TabletBot.Discord/Watchers/Commands/CommandMessageWatcher.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,26 @@ public async Task Receive(IMessage message)
3838
{
3939
if (message.Content.StartsWith(_settings.CommandPrefix))
4040
{
41-
var context = new CommandContext(_discordClient, message as IUserMessage);
42-
await _commandService.ExecuteAsync(context, 1, _serviceProvider).ConfigureAwait(false);
41+
try
42+
{
43+
var context = new CommandContext(_discordClient, message as IUserMessage);
44+
await _commandService.ExecuteAsync(context, 1, _serviceProvider).ConfigureAwait(false);
45+
}
46+
catch (Exception e)
47+
{
48+
Log.Exception(e);
49+
try
50+
{
51+
string exMessage = e.GetType().FullName + ": " + e.Message;
52+
await (message as IUserMessage).ReplyAsync(exMessage);
53+
}
54+
catch (Exception respondEx)
55+
{
56+
Log.Exception(respondEx);
57+
}
58+
59+
throw;
60+
}
4361
}
4462
}
4563

TabletBot.Discord/Watchers/Commands/SlashCommandInteractionWatcher.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,32 @@ IEnumerable<SlashCommandModule> commands
3131

3232
public async Task HandleInteraction(SocketInteraction interaction)
3333
{
34-
await Task.WhenAll(_commands.Select(m => m.HandleInteraction(interaction)));
34+
try
35+
{
36+
await Task.WhenAll(_commands.Select(m => m.HandleInteraction(interaction)));
37+
}
38+
catch (Exception e)
39+
{
40+
Log.Exception(e);
41+
try
42+
{
43+
string message = e.GetType().FullName + ": " + e.Message;
44+
if (interaction.HasResponded)
45+
{
46+
await interaction.FollowupAsync(message, ephemeral: true);
47+
}
48+
else
49+
{
50+
await interaction.RespondAsync(message, ephemeral: true);
51+
}
52+
}
53+
catch (Exception responseEx)
54+
{
55+
Log.Exception(responseEx);
56+
}
57+
58+
throw;
59+
}
3560
}
3661

3762
public async Task InitializeAsync()

0 commit comments

Comments
 (0)