Skip to content

Commit

Permalink
log exceptions on slash command log
Browse files Browse the repository at this point in the history
  • Loading branch information
FloatingMilkshake committed Mar 16, 2024
1 parent 025c3dc commit 56be370
Showing 1 changed file with 52 additions and 34 deletions.
86 changes: 52 additions & 34 deletions Events/InteractionEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
}

0 comments on commit 56be370

Please sign in to comment.