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

refactor(InteractionResponses): refactor .embed() #35

Merged
merged 3 commits into from
Jun 18, 2024
Merged
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
2 changes: 1 addition & 1 deletion packages/discord.js/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "@draftbot/discord.js",
"version": "14.14.1-2",
"version": "14.14.1-4",
"description": "A powerful library for interacting with the Discord API",
"scripts": {
"test": "pnpm run docs:test && pnpm run test:typescript",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ class MessageComponentInteraction extends BaseInteraction {
*/
this.replied = false;

/**
* Whether this interaction has already been updated to
* @type {boolean}
*/
this.updated = false;

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

/**
* Whether this interaction has already been updated to
* @type {boolean}
*/
this.updated = false;

/**
* Whether the reply to this interaction is ephemeral
* @type {?boolean}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,32 +125,6 @@ class InteractionResponses {
return options.fetchReply ? this.fetchReply() : new InteractionResponse(this);
}

/**
* Creates a embed reply to this interaction.
* <info>Use the `fetchReply` option to get the bot's reply message.</info>
* @param {Embed|APIEmbed} embed The embed for the reply
* @param {MessagePayload|InteractionReplyOptions} [options] The options for the reply
* @returns {Promise<Message|InteractionResponse>}
* @example
* // Create an embed reply
* const embed = new MessageEmbed().setDescription('Pong!');
*
* interaction.replyEmbed(embed)
* .then((message) => console.log(`Embed reply sent`))
* .catch(console.error);
* @example
* // Create an ephemeral embed reply
* const embed = new MessageEmbed().setDescription('Pong!');
*
* interaction.replyEmbed(embed, { ephemeral: true })
* .then(() => console.log('Embed reply sent.'))
* .catch(console.error);
*/
replyEmbed(embed, options = {}) {
options.embeds = [embed];
return this.reply(options);
}

/**
* Fetches a reply to this interaction.
* @see Webhook#fetchMessage
Expand Down Expand Up @@ -193,21 +167,6 @@ class InteractionResponses {
return msg;
}

/**
* Edits the initial reply to this interaction.
* @see Webhook#editMessage
* @param {string|MessagePayload|WebhookEditMessageOptions} options The new options for the message
* @returns {Promise<Message>}
* @example
* // Edit the reply to this interaction
* interaction.edit('New content')
* .then(console.log)
* .catch(console.error);
*/
edit(options) {
return this.editReply(options);
}

/**
* Deletes a reply to this interaction.
* @see Webhook#deleteMessage
Expand All @@ -223,21 +182,6 @@ class InteractionResponses {
await this.webhook.deleteMessage(message);
}

/**
* Deletes the initial reply to this interaction.
* @see Webhook#deleteMessage
* @param {number} [timeout] Timeout delete
* @returns {Promise<void>}
* @example
* // Delete the reply to this interaction
* interaction.delete()
* .then(console.log)
* .catch(console.error);
*/
delete() {
return this.deleteReply();
}

/**
* Send a follow-up message to this interaction.
* @param {string|MessagePayload|InteractionReplyOptions} options The options for the reply
Expand All @@ -248,27 +192,22 @@ class InteractionResponses {
return this.webhook.send(options);
}

/**
* Send a follow-up message to this interaction.
* @param {string|MessagePayload|InteractionReplyOptions} [options] The options for the reply
* @returns {Promise<Message|APIMessage>}
*/
send(options = {}) {
if (options.forceFollowUp) return this.followUp(options);
return this[this.replied || this.deferred ? 'editReply' : 'reply'](options);
}

/**
* Send a follow-up embed message to this interaction.
* @param {Embed|APIEmbed} embed The embed for the reply
* @param {MessagePayload|InteractionReplyOptions} [options] The options for the reply
* @param {MessagePayload|InteractionReplyOptions|boolean} [options] The options for the reply
* @returns {Promise<Message>}
*/
embed(embed, options = {}) {
if (typeof options === 'boolean') {
options = { ephemeral: options };
}

options.embeds = [embed];

if (options.forceFollowUp) return this.followUp(options);
return this[this.replied || this.deferred ? 'editReply' : 'reply'](options);
if (this.updated || options.forceFollowUp) return this.followUp(options);
if (this.deferred || this.replied) return this.editReply(options);
return this.reply(options);
}

/**
Expand Down Expand Up @@ -326,6 +265,10 @@ class InteractionResponses {
});
this.replied = true;

// Used to detect the impossibiliity to reply to the interaction in a new message,
// any editReply would be a editReply of the initial message
this.updated = true;

return options.fetchReply ? this.fetchReply() : new InteractionResponse(this, this.message.interaction?.id);
}

Expand Down Expand Up @@ -398,14 +341,10 @@ class InteractionResponses {
const props = [
'deferReply',
'reply',
'replyEmbed',
'fetchReply',
'edit',
'editReply',
'deleteReply',
'delete',
'followUp',
'send',
'embed',
'deferUpdate',
'update',
Expand Down
43 changes: 5 additions & 38 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,32 +583,18 @@ export abstract class CommandInteraction<Cached extends CacheType = CacheType> e
): Promise<Message<BooleanCache<Cached>>>;
public deferReply(options?: InteractionDeferReplyOptions): Promise<InteractionResponse<BooleanCache<Cached>>>;
public deleteReply(message?: MessageResolvable | '@original'): Promise<void>;
public delete(): Promise<void>;
public editReply(
options: string | MessagePayload | InteractionEditReplyOptions,
): Promise<Message<BooleanCache<Cached>>>;
public edit(options: string | MessagePayload | WebhookMessageEditOptions): Promise<Message<BooleanCache<Cached>>>;
public fetchReply(message?: Snowflake | '@original'): Promise<Message<BooleanCache<Cached>>>;
public followUp(options: string | MessagePayload | InteractionReplyOptions): Promise<Message<BooleanCache<Cached>>>;
public send(options: InteractionReplyOptions & { fetchReply: true }): Promise<Message<BooleanCache<Cached>>>;
public send(
options: string | MessagePayload | InteractionReplyOptions,
): Promise<InteractionResponse<BooleanCache<Cached>>>;
public reply(options: InteractionReplyOptions & { fetchReply: true }): Promise<Message<BooleanCache<Cached>>>;
public reply(
options: string | MessagePayload | InteractionReplyOptions,
): Promise<InteractionResponse<BooleanCache<Cached>>>;
public replyEmbed(
embed: JSONEncodable<APIEmbed> | APIEmbed,
options: Omit<InteractionReplyOptions & { fetchReply: true }, 'embeds'>,
): Promise<Message<BooleanCache<Cached>>>;
public replyEmbed(
embed: JSONEncodable<APIEmbed> | APIEmbed,
options?: Omit<InteractionReplyOptions, 'embeds'>,
): Promise<InteractionResponse<BooleanCache<Cached>>>;
public embed(
embed: JSONEncodable<APIEmbed> | APIEmbed,
options?: Omit<InteractionReplyOptions, 'embeds'>,
options?: Omit<InteractionReplyOptions, 'embeds'> | boolean,
): Promise<Message>;
public showModal(
modal:
Expand Down Expand Up @@ -2247,6 +2233,7 @@ export class MessageComponentInteraction<Cached extends CacheType = CacheType> e
public ephemeral: boolean | null;
public message: Message<BooleanCache<Cached>>;
public replied: boolean;
public updated: boolean;
public webhook: InteractionWebhook;
public inGuild(): this is MessageComponentInteraction<'raw' | 'cached'>;
public inCachedGuild(): this is MessageComponentInteraction<'cached'>;
Expand All @@ -2260,29 +2247,18 @@ export class MessageComponentInteraction<Cached extends CacheType = CacheType> e
): Promise<Message<BooleanCache<Cached>>>;
public deferUpdate(options?: InteractionDeferUpdateOptions): Promise<InteractionResponse<BooleanCache<Cached>>>;
public deleteReply(message?: MessageResolvable | '@original'): Promise<void>;
public delete(): Promise<void>;
public editReply(
options: string | MessagePayload | InteractionEditReplyOptions,
): Promise<Message<BooleanCache<Cached>>>;
public edit(options: string | MessagePayload | WebhookMessageEditOptions): Promise<Message<BooleanCache<Cached>>>;
public fetchReply(message?: Snowflake | '@original'): Promise<Message<BooleanCache<Cached>>>;
public followUp(options: string | MessagePayload | InteractionReplyOptions): Promise<Message<BooleanCache<Cached>>>;
public send(options: string | MessagePayload | InteractionReplyOptions): Promise<Message<BooleanCache<Cached>>>;
public reply(options: InteractionReplyOptions & { fetchReply: true }): Promise<Message<BooleanCache<Cached>>>;
public reply(
options: string | MessagePayload | InteractionReplyOptions,
): Promise<InteractionResponse<BooleanCache<Cached>>>;
public replyEmbed(
embed: JSONEncodable<APIEmbed> | APIEmbed,
options: Omit<InteractionReplyOptions & { fetchReply: true }, 'embeds'>,
): Promise<Message<BooleanCache<Cached>>>;
public replyEmbed(
embed: JSONEncodable<APIEmbed> | APIEmbed,
options?: Omit<InteractionReplyOptions, 'embeds'>,
): Promise<InteractionResponse<BooleanCache<Cached>>>;
public embed(
embed: JSONEncodable<APIEmbed> | APIEmbed,
options?: Omit<InteractionReplyOptions, 'embeds'>,
options?: Omit<InteractionReplyOptions, 'embeds'> | boolean,
): Promise<Message<BooleanCache<Cached>>>;
public update(options: InteractionUpdateOptions & { fetchReply: true }): Promise<Message<BooleanCache<Cached>>>;
public update(
Expand Down Expand Up @@ -2474,29 +2450,20 @@ export class ModalSubmitInteraction<Cached extends CacheType = CacheType> extend
public ephemeral: boolean | null;
public message: Message<BooleanCache<Cached>> | null;
public replied: boolean;
public updated: boolean;
public readonly webhook: InteractionWebhook;
public reply(options: InteractionReplyOptions & { fetchReply: true }): Promise<Message<BooleanCache<Cached>>>;
public reply(
options: string | MessagePayload | InteractionReplyOptions,
): Promise<InteractionResponse<BooleanCache<Cached>>>;
public replyEmbed(
embed: JSONEncodable<APIEmbed> | APIEmbed,
options: Omit<InteractionReplyOptions & { fetchReply: true }, 'embeds'>,
): Promise<Message>;
public replyEmbed(
embed: JSONEncodable<APIEmbed> | APIEmbed,
options?: Omit<InteractionReplyOptions, 'embeds'>,
): Promise<InteractionResponse<BooleanCache<Cached>>>;
public embed(
embed: JSONEncodable<APIEmbed> | APIEmbed,
options?: Omit<InteractionReplyOptions, 'embeds'>,
options?: Omit<InteractionReplyOptions, 'embeds'> | boolean,
): Promise<Message<BooleanCache<Cached>>>;
public deleteReply(message?: MessageResolvable | '@original'): Promise<void>;
public delete(): Promise<void>;
public editReply(
options: string | MessagePayload | InteractionEditReplyOptions,
): Promise<Message<BooleanCache<Cached>>>;
public edit(options: string | MessagePayload | WebhookMessageEditOptions): Promise<Message<BooleanCache<Cached>>>;
public deferReply(
options: InteractionDeferReplyOptions & { fetchReply: true },
): Promise<Message<BooleanCache<Cached>>>;
Expand Down
Loading