refactor(Message)!: update clickButton method to use CustomID and remove coordinate support

This commit is contained in:
Elysia
2025-07-09 18:57:09 +07:00
parent 64a47093a8
commit c2fad20ed7
3 changed files with 17 additions and 44 deletions

View File

@@ -163,7 +163,9 @@ class BaseMessageComponent {
} }
static extractInteractiveComponents(component) { static extractInteractiveComponents(component) {
switch (component.type) { let type = component.type;
if (typeof type === 'string') type = MessageComponentTypes[type];
switch (type) {
case MessageComponentTypes.ACTION_ROW: case MessageComponentTypes.ACTION_ROW:
return component.components; return component.components;
case MessageComponentTypes.SECTION: case MessageComponentTypes.SECTION:

View File

@@ -1086,49 +1086,20 @@ class Message extends Base {
} }
/** /**
* Click specific button with X and Y * Click a specified button in the message based on the button's CustomID
* @typedef {Object} MessageButtonLocation * @param {string} buttonid customId of the button to click
* @property {number} X Index of the row *
* @property {number} Y Index of the column * To be compatible with Components V2, the following methods have been removed in this version:
*/ * - Clicking by coordinates (using ActionRow)
* - Clicking the first button in the message
/** *
* Click specific button or automatically click first button if no button is specified. * Currently, only clicking by CustomID is supported.
* @param {MessageButtonLocation|string|undefined} button button
* @returns {Promise<Message|Modal>} * @returns {Promise<Message|Modal>}
* @example
* // Demo msg
* Some content
* ――――――――――――――――――――――――――――――――> X from 0
* │ [button1] [button2] [button3]
* │ [button4] [button5] [button6]
* ↓
* Y from 0
* // Click button6 with X and Y
* [0,0] [1,0] [2,0]
* [0,1] [1,1] [2,1]
* // Code
* message.clickButton({
* X: 2, Y: 1,
* });
* // Click button with customId (Ex button 5)
* message.clickButton('button5');
* // Click button 1
* message.clickButton();
*/ */
clickButton(button) { clickButton(buttonid) {
if (typeof button == 'undefined') { const button = this.resolveComponent(buttonid);
button = this.components if (!button || button.type !== 'BUTTON') throw new TypeError('BUTTON_NOT_FOUND');
.flatMap(row => row.components) if (button.disabled) throw new TypeError('BUTTON_CANNOT_CLICK');
.find(b => b.type === 'BUTTON' && b.customId && !b.disabled);
} else if (typeof button == 'string') {
button = this.components.flatMap(row => row.components).find(b => b.type === 'BUTTON' && b.customId == button);
} else {
button = this.components[button.Y]?.components[button.X];
}
if (!button) throw new TypeError('BUTTON_NOT_FOUND');
button = button.toJSON();
if (!button.custom_id || button.disabled) throw new TypeError('BUTTON_CANNOT_CLICK');
const nonce = SnowflakeUtil.generate(); const nonce = SnowflakeUtil.generate();
const data = { const data = {
type: InteractionTypes.MESSAGE_COMPONENT, type: InteractionTypes.MESSAGE_COMPONENT,
@@ -1141,7 +1112,7 @@ class Message extends Base {
message_flags: this.flags.bitfield, message_flags: this.flags.bitfield,
data: { data: {
component_type: MessageComponentTypes.BUTTON, component_type: MessageComponentTypes.BUTTON,
custom_id: button.custom_id, custom_id: button.customId,
}, },
}; };
this.client.api.interactions.post({ this.client.api.interactions.post({

2
typings/index.d.ts vendored
View File

@@ -2225,7 +2225,7 @@ export class Message<Cached extends boolean = boolean> extends Base {
public inGuild(): this is Message<true> & this; public inGuild(): this is Message<true> & this;
public readonly isMessage: true; public readonly isMessage: true;
public clickButton(button?: { X: number; Y: number } | string): Promise<Message | Modal>; public clickButton(button: string): Promise<Message | Modal>;
public selectMenu( public selectMenu(
menu: 0 | 1 | 2 | 3 | 4 | string, menu: 0 | 1 | 2 | 3 | 4 | string,
vales: (UserResolvable | RoleResolvable | ChannelResolvable | string)[], vales: (UserResolvable | RoleResolvable | ChannelResolvable | string)[],