Skip to content

Commit

Permalink
fix slash error handling and cat/dog api regex
Browse files Browse the repository at this point in the history
  • Loading branch information
FloatingMilkshake committed Dec 29, 2023
1 parent 1df6121 commit e6f81aa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Commands/RandomCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public static async Task RandomDog(InteractionContext ctx)
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{dog}"));
}

[GeneratedRegex(@"https:\/\/cdn2.thecatapi.com\/images\/.*(.png|.jpg)")]
[GeneratedRegex(@"https:\/\/cdn2.thecatapi.com\/images\/.*(\.[A-Za-z]{3,4})")]
private static partial Regex CatApiUrlPattern();
[GeneratedRegex(@"https:\\\/\\\/images.dog.ceo\\\/breeds\\\/.*(.png|.jpg)")]
[GeneratedRegex(@"https:\\\/\\\/images.dog.ceo\\\/breeds\\\/.*(\.[A-Za-z]{3,4})")]
private static partial Regex DogApiUrlPattern();
}
}
26 changes: 22 additions & 4 deletions Events/ErrorEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ await e.Context.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent(cm
embed.AddField("Exception Details",
$"```{exception.GetType()}: {exception.Message}:\n{exception.StackTrace}".Truncate(1020) + "\n```");

// For /cdn and /link, respond directly to the interaction with the exception embed
if (e.Context.CommandName is "cdn" or "link")
{
// Try to respond to the interaction
Expand All @@ -70,11 +71,28 @@ await e.Context.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent(cm

return;
}

// For other commands, send the embed to the bot's home channel, and respond with a friendlier message

var friendlyResponse = "It looks like this command is having issues! Sorry for the inconvenience."
+ " Bot owners have been alerted. Try again in a few minutes, or DM a bot owner if this"
+ $" keeps happening. See {SlashCmdMentionHelpers.GetSlashCmdMention("about")} for a list of owners.";

await e.Context.CreateResponseAsync(new DiscordInteractionResponseBuilder().WithContent(
"It looks like this command is having issues! Sorry for the inconvenience." +
" Bot owners have been alerted. Try again in a few minutes, or DM a bot owner if this" +
$" keeps happening. See {SlashCmdMentionHelpers.GetSlashCmdMention("about")} for a list of owners."));
// Try to respond to the interaction
try
{
await e.Context.CreateResponseAsync(
new DiscordInteractionResponseBuilder().WithContent(friendlyResponse));
}
catch (BadRequestException)
{
// If the interaction was deferred, CreateResponseAsync() won't work
// Try using FollowUpAsync() instead
await e.Context.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent(friendlyResponse));
}

// Send the exception embed to the bot's home channel
await Program.HomeChannel.SendMessageAsync(embed);

break;
}
Expand Down

0 comments on commit e6f81aa

Please sign in to comment.