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

feat(Interactions): auto-fetch replies where possible #5830

Closed
wants to merge 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/structures/MessageComponentInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class MessageComponentInteraction extends Interaction {

/**
* The message to which the component was attached
* @type {?Message|Object}
* @type {Message|Object}
*/
this.message = data.message ? this.channel?.messages.add(data.message) ?? data.message : null;
this.message = this.channel?.messages.add(data.message) ?? data.message;

/**
* The custom ID of the component which was interacted with
Expand Down
30 changes: 22 additions & 8 deletions src/structures/interfaces/InteractionResponses.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class InteractionResponses {
*/

/**
* Defers the reply to this interaction.
* Defers the reply to this interaction. Resolves to the sent Message if not ephemeral.
* @param {InteractionDeferOptions} [options] Options for deferring the reply to this interaction
* @returns {Promise<void>}
* @returns {Promise<Message|void>}
* @example
* // Defer the reply to this interaction
* interaction.defer()
Expand All @@ -49,12 +49,14 @@ class InteractionResponses {
},
});
this.deferred = true;

return ephemeral ? undefined : this.fetchReply();
}

/**
* Creates a reply to this interaction.
* Creates a reply to this interaction. Resolves to the sent Message if not ephemeral.
* @param {string|APIMessage|InteractionReplyOptions} options The options for the reply
* @returns {Promise<void>}
* @returns {Promise<Message|void>}
* @example
* // Reply to the interaction with an embed
* const embed = new MessageEmbed().setDescription('Pong!');
Expand Down Expand Up @@ -85,6 +87,8 @@ class InteractionResponses {
files,
});
this.replied = true;

return options.ephemeral ? undefined : this.fetchReply();
}

/**
Expand Down Expand Up @@ -140,8 +144,9 @@ class InteractionResponses {
}

/**
* Defers an update to the message to which the component was attached
* @returns {Promise<void>}
* Defers an update to the message to which the component was attached.
* Resolves to that message, if it was not ephemeral.
* @returns {Promise<Message|void>}
* @example
* // Defer updating and reset the component's loading state
* interaction.deferUpdate()
Expand All @@ -156,12 +161,17 @@ class InteractionResponses {
},
});
this.deferred = true;

return (this.message.flags & MessageFlags.FLAGS.EPHEMERAL) === MessageFlags.FLAGS.EPHEMERAL
? undefined
: this.fetchReply();
}

/**
* Updates the original message whose button was pressed
* Updates the original message whose button was pressed.
* Resolves to that message, if it was not ephemeral.
* @param {string|APIMessage|WebhookEditMessageOptions} options The options for the reply
* @returns {Promise<void>}
* @returns {Promise<Message|void>}
* @example
* // Remove the components from the message
* interaction.update({
Expand All @@ -188,6 +198,10 @@ class InteractionResponses {
files,
});
this.replied = true;

return (this.message.flags & MessageFlags.FLAGS.EPHEMERAL) === MessageFlags.FLAGS.EPHEMERAL
? undefined
: this.fetchReply();
}

static applyToClass(structure, ignore = []) {
Expand Down
20 changes: 14 additions & 6 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,16 @@ declare module 'discord.js' {
public options: Collection<string, CommandInteractionOption>;
public replied: boolean;
public webhook: InteractionWebhook;
public defer(options?: InteractionDeferOptions): Promise<void>;
public defer(options?: InteractionDeferOptions & { ephemeral: true }): Promise<void>;
public defer(options?: InteractionDeferOptions & { ephemeral?: false }): Promise<Message | RawMessage>;
public deleteReply(): Promise<void>;
public editReply(options: string | APIMessage | WebhookEditMessageOptions): Promise<Message | RawMessage>;
public fetchReply(): Promise<Message | RawMessage>;
public followUp(options: string | APIMessage | InteractionReplyOptions): Promise<Message | RawMessage>;
public reply(options: string | APIMessage | InteractionReplyOptions): Promise<void>;
public reply(options: string | APIMessage | (InteractionReplyOptions & { ephemeral: true })): Promise<void>;
public reply(
options: string | APIMessage | (InteractionReplyOptions & { ephemeral?: false }),
): Promise<Message | RawMessage>;
private transformOption(option: unknown, resolved: unknown): CommandInteractionOption;
private _createOptionsCollection(options: unknown, resolved: unknown): Collection<string, CommandInteractionOption>;
}
Expand Down Expand Up @@ -1337,14 +1341,18 @@ declare module 'discord.js' {
public message: Message | RawMessage;
public replied: boolean;
public webhook: InteractionWebhook;
public defer(options?: InteractionDeferOptions): Promise<void>;
public deferUpdate(): Promise<void>;
public defer(options?: InteractionDeferOptions & { ephemeral: true }): Promise<void>;
public defer(options?: InteractionDeferOptions & { ephemeral?: false }): Promise<Message | RawMessage>;
public deferUpdate(): Promise<Message | RawMessage | undefined>;
public deleteReply(): Promise<void>;
public editReply(options: string | APIMessage | WebhookEditMessageOptions): Promise<Message | RawMessage>;
public fetchReply(): Promise<Message | RawMessage>;
public followUp(options: string | APIMessage | InteractionReplyOptions): Promise<Message | RawMessage>;
public reply(options: string | APIMessage | InteractionReplyOptions): Promise<void>;
public update(content: string | APIMessage | WebhookEditMessageOptions): Promise<void>;
public reply(options: string | APIMessage | (InteractionReplyOptions & { ephemeral: true })): Promise<void>;
public reply(
options: string | APIMessage | (InteractionReplyOptions & { ephemeral?: false }),
): Promise<Message | RawMessage>;
public update(content: string | APIMessage | WebhookEditMessageOptions): Promise<Message | RawMessage | undefined>;
public static resolveType(type: MessageComponentTypeResolvable): MessageComponentType;
}

Expand Down