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

Better error handling #5665

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/structures/CommandInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class CommandInteraction extends Interaction {
* @type {boolean}
*/
this.deferred = false;

/**
* Whether the reply to this interaction is ephemeral
* @type {boolean}
*/
this.ephemeral = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe initialize it with null since the interaction wasn't replied to yet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay


/**
* The options passed to the command.
Expand Down Expand Up @@ -115,6 +121,7 @@ class CommandInteraction extends Interaction {
*/
async reply(content, options) {
if (this.deferred || this.replied) throw new Error('INTERACTION_ALREADY_REPLIED');
this.ephemeral = options.ephemeral ?? false;
const apiMessage = content instanceof APIMessage ? content : APIMessage.create(this, content, options);
const { data, files } = await apiMessage.resolveData().resolveFiles();

Expand All @@ -139,6 +146,7 @@ class CommandInteraction extends Interaction {
* .catch(console.error);
*/
async fetchReply() {
if (this.ephemeral) throw new Error('Unable to fetch ephemeral response');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current error surely does not look right 😄

Error: An invalid error message key was used: Unable to fetch ephemeral response.

You need to register your error in this object here

INTERACTION_ALREADY_REPLIED: 'This interaction has already been deferred or replied to.',

Maybe INTERACTION_EHPEMERAL_REPLIED?

const raw = await this.webhook.fetchMessage('@original');
return this.channel?.messages.add(raw) ?? raw;
}
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ declare module 'discord.js' {
public commandID: string;
public commandName: string;
public deferred: boolean;
public ephemeral: boolean;
public options: CommandInteractionOption[];
public replied: boolean;
public webhook: WebhookClient;
Expand Down