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

View File

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