diff --git a/Events/InteractionEvents.cs b/Events/InteractionEvents.cs index 13b19ed..6c7d102 100644 --- a/Events/InteractionEvents.cs +++ b/Events/InteractionEvents.cs @@ -14,45 +14,63 @@ public static async Task ContextMenuExecuted(SlashCommandsExtension _, ContextMe private static async Task LogCmdUsage(BaseContext context) { - // Ignore home server, excluded servers, and authorized users - if (context.Guild.Id == Program.HomeServer.Id || - Program.ConfigJson.Logs.SlashCommands.CmdLogExcludedGuilds.Contains(context.Guild.Id.ToString()) || - Program.ConfigJson.Base.AuthorizedUsers.Contains(context.User.Id.ToString())) - return; - - // Increment count - if (await Program.Db.HashExistsAsync("commandCounts", context.CommandName)) - await Program.Db.HashIncrementAsync("commandCounts", context.CommandName); - else - await Program.Db.HashSetAsync("commandCounts", context.CommandName, 1); - - // Log to log channel if configured - if (Program.ConfigJson.Logs.SlashCommands.LogChannel is not null) + try { - var embed = new DiscordEmbedBuilder() - .WithColor(Program.BotColor) - .WithAuthor(context.User.Username, null, context.User.AvatarUrl) - .WithDescription( - $"{context.User.Username} (`{context.User.Id}`) used {SlashCmdMentionHelpers.GetSlashCmdMention(context.CommandName)} in `{context.Channel.Name}` (`{context.Channel.Id}`) in \"{context.Guild.Name}\" (`{context.Guild.Id}`)!") - .WithTimestamp(DateTime.Now); + // Ignore home server, excluded servers, and authorized users + if (context.Guild.Id == Program.HomeServer.Id || + Program.ConfigJson.Logs.SlashCommands.CmdLogExcludedGuilds.Contains(context.Guild.Id.ToString()) || + Program.ConfigJson.Base.AuthorizedUsers.Contains(context.User.Id.ToString())) + return; - try - { - await (await context.Client.GetChannelAsync( - Convert.ToUInt64(Program.ConfigJson.Logs.SlashCommands.LogChannel))).SendMessageAsync(embed); - } - catch (Exception ex) when (ex is UnauthorizedException or NotFoundException) + // Increment count + if (await Program.Db.HashExistsAsync("commandCounts", context.CommandName)) + await Program.Db.HashIncrementAsync("commandCounts", context.CommandName); + else + await Program.Db.HashSetAsync("commandCounts", context.CommandName, 1); + + // Log to log channel if configured + if (Program.ConfigJson.Logs.SlashCommands.LogChannel is not null) { - Program.Discord.Logger.LogError(Program.BotEventId, - "{User} used {Command} in {Guild} but it could not be logged because the log channel cannot be accessed", - context.User.Id, context.CommandName, context.Guild.Id); + var embed = new DiscordEmbedBuilder() + .WithColor(Program.BotColor) + .WithAuthor(context.User.Username, null, context.User.AvatarUrl) + .WithDescription( + $"{context.User.Username} (`{context.User.Id}`) used {SlashCmdMentionHelpers.GetSlashCmdMention(context.CommandName)} in `{context.Channel.Name}` (`{context.Channel.Id}`) in \"{context.Guild.Name}\" (`{context.Guild.Id}`)!") + .WithTimestamp(DateTime.Now); + + try + { + await (await context.Client.GetChannelAsync( + Convert.ToUInt64(Program.ConfigJson.Logs.SlashCommands.LogChannel))).SendMessageAsync(embed); + } + catch (Exception ex) when (ex is UnauthorizedException or NotFoundException) + { + Program.Discord.Logger.LogError(Program.BotEventId, + "{User} used {Command} in {Guild} but it could not be logged because the log channel cannot be accessed", + context.User.Id, context.CommandName, context.Guild.Id); + } + catch (FormatException) + { + Program.Discord.Logger.LogError(Program.BotEventId, + "{User} used {Command} in {Guild} but it could not be logged because the log channel ID is invalid", + context.User.Id, context.CommandName, context.Guild.Id); + } } - catch (FormatException) + } + catch (Exception ex) + { + DiscordEmbedBuilder embed = new() { - Program.Discord.Logger.LogError(Program.BotEventId, - "{User} used {Command} in {Guild} but it could not be logged because the log channel ID is invalid", - context.User.Id, context.CommandName, context.Guild.Id); - } + Title = "An exception was thrown when logging a slash command", + Description = + $"An exception was thrown when {context.User.Mention} used `/{context.CommandName}`. Details are below.", + Color = DiscordColor.Red + }; + embed.AddField("Exception Details", + $"```{ex.GetType()}: {ex.Message}:\n{ex.StackTrace}".Truncate(1020) + "\n```"); + + await Program.HomeChannel.SendMessageAsync(embed); } + } } \ No newline at end of file