feat: Add new flags, new option for Message#selectMenu

This commit is contained in:
March 7th 2022-12-04 01:46:40 +07:00
parent 720848131d
commit 7d34a5de24
4 changed files with 34 additions and 11 deletions

View File

@ -11,6 +11,7 @@ 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');
const MessageSelectMenu = require('./MessageSelectMenu');
const ReactionCollector = require('./ReactionCollector'); const ReactionCollector = require('./ReactionCollector');
const { Sticker } = require('./Sticker'); const { Sticker } = require('./Sticker');
const { Error } = require('../errors'); const { Error } = require('../errors');
@ -1089,26 +1090,43 @@ class Message extends Base {
clickButton(button) { clickButton(button) {
if (!button) { if (!button) {
button = this.components.flatMap(row => row.components).find(b => b.type === 'BUTTON')?.customId; button = this.components.flatMap(row => row.components).find(b => b.type === 'BUTTON')?.customId;
} } else if (button instanceof MessageButton) {
if (button instanceof MessageButton) {
button = button.customId; button = button.customId;
} } else if (typeof button === 'object') {
if (typeof button === 'object') {
if (!('row' in button) || !('col' in button)) 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.col]?.customId; button = this.components[button.row]?.components[button.col]?.customId;
} }
if (!button) throw new TypeError('BUTTON_NOT_FOUND');
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|number|Array<string>} menuID Select Menu specific id or row / auto select first Menu * @param {MessageSelectMenu|string|number|Array<any>} menuID MenuId / MessageSelectMenu / Row of Menu / Array of Values (first menu)
* @param {Array<any>} options Menu Options * @param {Array<any>} options Array of Values
* @returns {Promise<InteractionResponse>} * @returns {Promise<InteractionResponse>}
* @example
* client.on('messageCreate', async message => {
* if (message.components.length) {
* // Row
* await message.selectMenu(1, [message.channel]); // row 1, type: Channel, multi: false
* // Id
* await message.selectMenu('menu-id', ['uid1', client.user, message.member]); // MenuId, type: User, multi: true
* // First Menu
* await message.selectMenu(['role-id']); // First Menu, type: role, multi: false
* // class MessageSelectMenu
* const menu = message.components[0].components[0];
* await message.selectMenu(menu, ['option1', 'option2']);
* // MessageSelectMenu (2)
* menu.select(message, ['option1', 'option2']);
* }
* });
*/ */
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');
if (/[0-4]/.test(menuID)) { if (menuID instanceof MessageSelectMenu) {
//
} else if (/[0-4]/.test(menuID)) {
menuID = this.components[menuID]?.components[0]; menuID = this.components[menuID]?.components[0];
} else { } else {
const menuAll = this.components const menuAll = this.components

View File

@ -31,6 +31,7 @@ class MessageFlags extends BitField {}
* * `HAS_THREAD` * * `HAS_THREAD`
* * `EPHEMERAL` * * `EPHEMERAL`
* * `LOADING` * * `LOADING`
* * `FAILED_TO_MENTION_SOME_ROLES_IN_THREAD`
* @type {Object} * @type {Object}
* @see {@link https://discord.com/developers/docs/resources/channel#message-object-message-flags} * @see {@link https://discord.com/developers/docs/resources/channel#message-object-message-flags}
*/ */
@ -43,6 +44,7 @@ MessageFlags.FLAGS = {
HAS_THREAD: 1 << 5, HAS_THREAD: 1 << 5,
EPHEMERAL: 1 << 6, EPHEMERAL: 1 << 6,
LOADING: 1 << 7, LOADING: 1 << 7,
FAILED_TO_MENTION_SOME_ROLES_IN_THREAD: 1 << 8,
}; };
module.exports = MessageFlags; module.exports = MessageFlags;

View File

@ -195,7 +195,7 @@ class Options extends null {
referrer_current: 'https://discord.com/channels/@me', referrer_current: 'https://discord.com/channels/@me',
referring_domain_current: 'discord.com', referring_domain_current: 'discord.com',
release_channel: 'stable', release_channel: 'stable',
client_build_number: 160771, client_build_number: 162224,
client_event_source: null, client_event_source: null,
}, },
// ! capabilities: 4093, // ! capabilities: 4093,

9
typings/index.d.ts vendored
View File

@ -1975,7 +1975,7 @@ 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: any[]): Promise<InteractionResponse>; public selectMenu(menuID: MessageSelectMenu|Snowflake|number, options: any[]): Promise<InteractionResponse>;
public selectMenu(options: any[]): Promise<InteractionResponse>; public selectMenu(options: any[]): Promise<InteractionResponse>;
public contextMenu(botID: Snowflake, commandName: string): Promise<InteractionResponse>; public contextMenu(botID: Snowflake, commandName: string): Promise<InteractionResponse>;
} }
@ -4954,7 +4954,9 @@ export type ApplicationFlagsString =
| 'VERIFICATION_PENDING_GUILD_LIMIT' | 'VERIFICATION_PENDING_GUILD_LIMIT'
| 'EMBEDDED' | 'EMBEDDED'
| 'GATEWAY_MESSAGE_CONTENT' | 'GATEWAY_MESSAGE_CONTENT'
| 'GATEWAY_MESSAGE_CONTENT_LIMITED'; | 'GATEWAY_MESSAGE_CONTENT_LIMITED'
| 'EMBEDDED_FIRST_PARTY'
| 'APPLICATION_COMMAND_BADGE';
export interface AuditLogChange { export interface AuditLogChange {
key: APIAuditLogChange['key']; key: APIAuditLogChange['key'];
@ -6229,7 +6231,8 @@ export type MessageFlagsString =
| 'URGENT' | 'URGENT'
| 'HAS_THREAD' | 'HAS_THREAD'
| 'EPHEMERAL' | 'EPHEMERAL'
| 'LOADING'; | 'LOADING'
| 'FAILED_TO_MENTION_SOME_ROLES_IN_THREAD';
export interface MessageInteraction { export interface MessageInteraction {
id: Snowflake; id: Snowflake;