Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Centralising errors catching #1145

Closed
carlonicora opened this issue Apr 7, 2024 · 5 comments
Closed

Centralising errors catching #1145

carlonicora opened this issue Apr 7, 2024 · 5 comments
Labels

Comments

@carlonicora
Copy link

What is your question?

Hello,

First and foremost thanks for releasing this, much appreciated!

I am building a new version of a bot I previously created with discord.js. One of the features I previously used was a centralised error catching for all my slash commands. This includes old commands that I removed, but are still pending in the guild.

Is there a place where to create this catchall for errors?

Thanks

@NedcloarBR
Copy link
Contributor

Hello! you can create a Exception Filter passaing the DiscordAPIError in the @Catch decorator
You can ignore specific errors by checking it's code from RESTJSONErrorCodes that can be imported from discord-api-types

@SocketSomeone
Copy link
Member

@carlonicora Thank you for question!
@NedcloarBR right! Just read more about Exception Filters and global providers

@carlonicora
Copy link
Author

carlonicora commented Apr 7, 2024

@NedcloarBR thanks for the reply.
I have one additional question.

I have created a specific exception filter which traps the call; however I have two issues:

  1. If I trap a DiscordAPIError, I cannot find the interaction context, hence I cannot reply to the interaction with an error.
  2. If the error is because the server still has an old slash command (not sure why the old ones are not removed), the error is not in a command, hence I cannot use the UseFilters decorator and the application crashes

Am I doing something wrong in here, or are these two edge cases?

import { Catch, ExceptionFilter } from '@nestjs/common';
import { DiscordAPIError } from 'discord.js';

@Catch(DiscordAPIError)
export class DiscordExceptionFilter implements ExceptionFilter {
  catch(error: DiscordAPIError) {
    console.log(error);

//I normally create an error embed and return it to Discord to warn the user
    // return new EmbedBuilder()
    //   .setColor(Colour.Red)
    //   .setTitle('Error')
    //   .setDescription(error.message);
  }
}

@CharacterCommandsDecorator()
@UseFilters(DiscordExceptionFilter)
export class CharacterDetailsCommand {
  @Subcommand({
    name: 'details',
    description: 'Get the details of a character',
  })
  public async onDetails(
    @Context() [interaction]: SlashCommandContext,
    @Options() { player }: ...

thanks

EDIT: forgot to add I have this

app.useGlobalFilters(new DiscordExceptionFilter());

in my main.ts

EDIT 2: I realised one of my issues: I can create a filter for my own error type to fix the issue 2, but issue 1 is still there

@SocketSomeone
Copy link
Member

@carlonicora

If I trap a DiscordAPIError, I cannot find the interaction context, hence I cannot reply to the interaction with an error.

You can get host arguments.
image
Also, you can join to our Discord server for quick response

Discord message with response

@SocketSomeone
Copy link
Member

Btw, from arguments host you can get discovery item to create own typeguard of commands, events, modals and etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants