Skip to content

Commit

Permalink
feat(Interactions): improve error handling for ephemeral responses (#…
Browse files Browse the repository at this point in the history
…5892)

Co-authored-by: muchnameless <[email protected]>
  • Loading branch information
monbrey and muchnameless authored Jun 23, 2021
1 parent 2d7c12b commit bd9f56a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/errors/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ const Messages = {
"or from a guild's application command manager.",

INTERACTION_ALREADY_REPLIED: 'This interaction has already been deferred or replied to.',
INTERACTION_NOT_REPLIED: 'This interaction has not been deferred or replied to.',
INTERACTION_EPHEMERAL_REPLIED: 'Ephemeral responses cannot be fetched or deleted.',
};

for (const [name, message] of Object.entries(Messages)) register(name, message);
6 changes: 6 additions & 0 deletions src/structures/CommandInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ class CommandInteraction extends Interaction {
*/
this.replied = false;

/**
* Whether the reply to this interaction is ephemeral
* @type {?boolean}
*/
this.ephemeral = null;

/**
* An associated interaction webhook, can be used to further interact with this interaction
* @type {InteractionWebhook}
Expand Down
6 changes: 6 additions & 0 deletions src/structures/MessageComponentInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class MessageComponentInteraction extends Interaction {
*/
this.deferred = false;

/**
* Whether the reply to this interaction is ephemeral
* @type {?boolean}
*/
this.ephemeral = null;

/**
* Whether this interaction has already been replied to
* @type {boolean}
Expand Down
5 changes: 5 additions & 0 deletions src/structures/interfaces/InteractionResponses.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class InteractionResponses {
*/
async defer({ ephemeral } = {}) {
if (this.deferred || this.replied) throw new Error('INTERACTION_ALREADY_REPLIED');
this.ephemeral = ephemeral ?? false;
await this.client.api.interactions(this.id, this.token).callback.post({
data: {
type: InteractionResponseTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE,
Expand Down Expand Up @@ -69,6 +70,7 @@ class InteractionResponses {
*/
async reply(options) {
if (this.deferred || this.replied) throw new Error('INTERACTION_ALREADY_REPLIED');
this.ephemeral = options.ephemeral ?? false;

let apiMessage;
if (options instanceof APIMessage) apiMessage = options;
Expand Down Expand Up @@ -97,6 +99,7 @@ class InteractionResponses {
* .catch(console.error);
*/
fetchReply() {
if (this.ephemeral) throw new Error('INTERACTION_EPHEMERAL_REPLIED');
return this.webhook.fetchMessage('@original');
}

Expand All @@ -112,6 +115,7 @@ class InteractionResponses {
* .catch(console.error);
*/
editReply(options) {
if (!this.deferred && !this.replied) throw new Error('INTERACTION_NOT_REPLIED');
return this.webhook.editMessage('@original', options);
}

Expand All @@ -126,6 +130,7 @@ class InteractionResponses {
* .catch(console.error);
*/
async deleteReply() {
if (this.ephemeral) throw new Error('INTERACTION_EPHEMERAL_REPLIED');
await this.webhook.deleteMessage('@original');
}

Expand Down
2 changes: 2 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ declare module 'discord.js' {
public commandID: Snowflake;
public commandName: string;
public deferred: boolean;
public ephemeral: boolean | null;
public options: Collection<string, CommandInteractionOption>;
public replied: boolean;
public webhook: InteractionWebhook;
Expand Down Expand Up @@ -1368,6 +1369,7 @@ declare module 'discord.js' {
public componentType: MessageComponentType;
public customID: string;
public deferred: boolean;
public ephemeral: boolean | null;
public message: Message | RawMessage;
public replied: boolean;
public webhook: InteractionWebhook;
Expand Down

0 comments on commit bd9f56a

Please sign in to comment.