feat: message.clickButton support Button class

This commit is contained in:
March 7th 2022-09-26 17:38:17 +07:00
parent d514d2ed60
commit 8fe25ff21a
2 changed files with 16 additions and 22 deletions

View File

@ -8,6 +8,7 @@ const BaseMessageComponent = require('./BaseMessageComponent');
const ClientApplication = require('./ClientApplication'); const ClientApplication = require('./ClientApplication');
const InteractionCollector = require('./InteractionCollector'); const InteractionCollector = require('./InteractionCollector');
const MessageAttachment = require('./MessageAttachment'); const MessageAttachment = require('./MessageAttachment');
const MessageButton = require('./MessageButton');
const Embed = require('./MessageEmbed'); const Embed = require('./MessageEmbed');
const Mentions = require('./MessageMentions'); const Mentions = require('./MessageMentions');
const MessagePayload = require('./MessagePayload'); const MessagePayload = require('./MessagePayload');
@ -1029,33 +1030,26 @@ class Message extends Base {
} }
/** /**
* Click specific button [Suggestion: Dux#2925] * Click specific button
* @param {string} buttonID Button ID * @param {MessageButton|string} button Button ID
* @returns {Promise<InteractionResponseBody>} * @returns {Promise<InteractionResponseBody>}
*/ */
async clickButton(buttonID) { clickButton(button) {
if (typeof buttonID !== 'string') { let buttonID;
if (button instanceof MessageButton) button = button.customId;
if (typeof button === 'string') buttonID = button;
if (!buttonID) {
throw new TypeError('BUTTON_ID_NOT_STRING'); throw new TypeError('BUTTON_ID_NOT_STRING');
} }
if (!this.components[0]) throw new TypeError('MESSAGE_NO_COMPONENTS'); if (!this.components[0]) throw new TypeError('MESSAGE_NO_COMPONENTS');
let button; for (const components of this.components) {
await Promise.all( for (const interactionComponent of components.components) {
this.components.map(async row => {
await Promise.all(
row.components.map(interactionComponent => {
if (interactionComponent.type == 'BUTTON' && interactionComponent.customId == buttonID) { if (interactionComponent.type == 'BUTTON' && interactionComponent.customId == buttonID) {
button = interactionComponent; return interactionComponent.click(this);
}
}
} }
return true;
}),
);
}),
);
if (!button) {
throw new TypeError('BUTTON_NOT_FOUND'); throw new TypeError('BUTTON_NOT_FOUND');
} else {
return button.click(this);
}
} }
/** /**
* Select specific menu or First Menu * Select specific menu or First Menu

2
typings/index.d.ts vendored
View File

@ -1929,7 +1929,7 @@ export class Message<Cached extends boolean = boolean> extends Base {
// Added // Added
public markUnread(): Promise<boolean>; public markUnread(): Promise<boolean>;
public markRead(): Promise<boolean>; public markRead(): Promise<boolean>;
public clickButton(buttonID: string): Promise<InteractionResponseBody>; public clickButton(button: MessageButton | string): Promise<InteractionResponseBody>;
public selectMenu(menuID: string, options: string[]): Promise<InteractionResponseBody>; public selectMenu(menuID: string, options: string[]): Promise<InteractionResponseBody>;
public selectMenu(options: string[]): Promise<InteractionResponseBody>; public selectMenu(options: string[]): Promise<InteractionResponseBody>;
public contextMenu(botID: Snowflake, commandName: string): Promise<InteractionResponseBody>; public contextMenu(botID: Snowflake, commandName: string): Promise<InteractionResponseBody>;