refactor: clickButton and selectMenu

This commit is contained in:
March 7th 2022-12-04 01:13:19 +07:00
parent b191df3e99
commit 720848131d
3 changed files with 26 additions and 30 deletions

View File

@ -1062,7 +1062,7 @@ class Message extends Base {
/** /**
* @typedef {Object} MessageButtonLocation * @typedef {Object} MessageButtonLocation
* @property {number} row Index of the row * @property {number} row Index of the row
* @property {number} column Index of the column * @property {number} col Index of the column
*/ */
/** /**
@ -1077,7 +1077,7 @@ class Message extends Base {
* // Click with button ID * // Click with button ID
* await message.clickButton('button-id'); * await message.clickButton('button-id');
* // Click with button location * // Click with button location
* await message.clickButton({ row: 0, column: 0 }); * await message.clickButton({ row: 0, col: 0 });
* // Click with class MessageButton * // Click with class MessageButton
* const button = message.components[0].components[0]; * const button = message.components[0].components[0];
* await message.clickButton(button); * await message.clickButton(button);
@ -1094,42 +1094,38 @@ class Message extends Base {
button = button.customId; button = button.customId;
} }
if (typeof button === 'object') { if (typeof button === 'object') {
if (!button.row || !button.column) throw new TypeError('INVALID_BUTTON_LOCATION'); if (!('row' in button) || !('col' in button)) throw new TypeError('INVALID_BUTTON_LOCATION');
button = this.components[button.row]?.components[button.column]?.customId; button = this.components[button.row]?.components[button.col]?.customId;
} }
button = this.components.flatMap(row => row.components).find(b => b.customId === button && b.type === 'BUTTON'); button = this.components.flatMap(row => row.components).find(b => b.customId === button && b.type === 'BUTTON');
return button ? button.click(this) : Promise.reject(new TypeError('BUTTON_NOT_FOUND')); return button ? button.click(this) : Promise.reject(new TypeError('BUTTON_NOT_FOUND'));
} }
/** /**
* Select specific menu or First Menu * Select specific menu or First Menu
* @param {string|Array<string>} menuID Select Menu specific id or auto select first Menu * @param {string|number|Array<string>} menuID Select Menu specific id or row / auto select first Menu
* @param {Array<string>} options Menu Options * @param {Array<any>} options Menu Options
* @returns {Promise<InteractionResponse>} * @returns {Promise<InteractionResponse>}
*/ */
selectMenu(menuID, options = []) { selectMenu(menuID, options = []) {
if (!this.components[0]) throw new TypeError('MESSAGE_NO_COMPONENTS'); if (!this.components[0]) throw new TypeError('MESSAGE_NO_COMPONENTS');
const menuAll = []; if (/[0-4]/.test(menuID)) {
for (const row of this.components) { menuID = this.components[menuID]?.components[0];
for (const component of row.components) {
if (
['STRING_SELECT', 'USER_SELECT', 'ROLE_SELECT', 'MENTIONABLE_SELECT', 'CHANNEL_SELECT'].includes(
component.type,
)
) {
menuAll.push(component);
}
}
}
if (menuAll.length == 0) throw new TypeError('MENU_NOT_FOUND');
if (menuAll.length == 1) {
return menuAll[0].select(this, Array.isArray(menuID) ? menuID : options);
} else { } else {
if (typeof menuID !== 'string') throw new TypeError('MENU_ID_NOT_STRING'); const menuAll = this.components
const menuCorrect = menuAll.find(menu => menu.customId == menuID); .flatMap(row => row.components)
if (!menuCorrect) throw new TypeError('MENU_NOT_FOUND'); .filter(b =>
return menuCorrect.select(this, Array.isArray(menuID) ? menuID : options); ['STRING_SELECT', 'USER_SELECT', 'ROLE_SELECT', 'MENTIONABLE_SELECT', 'CHANNEL_SELECT'].includes(b.type),
);
if (menuAll.length == 0) throw new TypeError('MENU_NOT_FOUND');
if (menuID) {
menuID = menuAll.find(b => b.customId === menuID);
} else {
menuID = menuAll[0];
} }
} }
if (!menuID.type.includes('_SELECT')) throw new TypeError('MENU_NOT_FOUND');
return menuID.select(this, Array.isArray(menuID) ? menuID : options);
}
// //
/** /**
* Send context Menu v2 * Send context Menu v2

View File

@ -280,7 +280,7 @@ class MessageSelectMenu extends BaseMessageComponent {
/** /**
* Mesage select menu * Mesage select menu
* @param {Message} message The message this select menu is for * @param {Message} message The message this select menu is for
* @param {Array<string>} values The values of the select menu * @param {Array<any>} values The values of the select menu
* @returns {Promise<InteractionResponse>} * @returns {Promise<InteractionResponse>}
*/ */
async select(message, values = []) { async select(message, values = []) {

8
typings/index.d.ts vendored
View File

@ -1975,8 +1975,8 @@ export class Message<Cached extends boolean = boolean> extends Base {
public markUnread(): Promise<boolean>; public markUnread(): Promise<boolean>;
public markRead(): Promise<boolean>; public markRead(): Promise<boolean>;
public clickButton(button?: MessageButton | MessageButtonLocation | string): Promise<InteractionResponse>; public clickButton(button?: MessageButton | MessageButtonLocation | string): Promise<InteractionResponse>;
public selectMenu(menuID: string, options: string[]): Promise<InteractionResponse>; public selectMenu(menuID: string, options: any[]): Promise<InteractionResponse>;
public selectMenu(options: string[]): Promise<InteractionResponse>; public selectMenu(options: any[]): Promise<InteractionResponse>;
public contextMenu(botID: Snowflake, commandName: string): Promise<InteractionResponse>; public contextMenu(botID: Snowflake, commandName: string): Promise<InteractionResponse>;
} }
@ -2286,7 +2286,7 @@ export class MessageSelectMenu extends BaseMessageComponent {
...options: MessageSelectOptionData[] | MessageSelectOptionData[][] ...options: MessageSelectOptionData[] | MessageSelectOptionData[][]
): this; ): this;
public toJSON(): APISelectMenuComponent; public toJSON(): APISelectMenuComponent;
public select(message: Message, values?: string[]): Promise<InteractionResponse>; public select(message: Message, values?: any[]): Promise<InteractionResponse>;
} }
// Todo // Todo
@ -6627,7 +6627,7 @@ export interface StartThreadOptions {
export interface MessageButtonLocation { export interface MessageButtonLocation {
row: number; row: number;
colunm: number; col: number;
} }
export type Status = number; export type Status = number;