From 17a1a19f42a9fe2e5332bc37862f30eb442ece93 Mon Sep 17 00:00:00 2001 From: March 7th <71698422+aiko-chan-ai@users.noreply.github.com> Date: Wed, 15 Jun 2022 18:13:25 +0700 Subject: [PATCH] refactor(TextBasedChannel): sendSlash Using REST API (without opcode 24, fix `command outdate` error) --- src/structures/Message.js | 47 ++++++---------- src/structures/interfaces/TextBasedChannel.js | 56 ++++++++----------- typings/index.d.ts | 4 +- 3 files changed, 43 insertions(+), 64 deletions(-) diff --git a/src/structures/Message.js b/src/structures/Message.js index 08daf38..f0b28c3 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -1076,10 +1076,9 @@ class Message extends Base { * Send context Menu v2 * @param {Snowflake} botId Bot id * @param {string} commandName Command name in Context Menu - * @param {boolean} [search=true] Search for command * @returns {Promise} */ - async contextMenu(botId, commandName, search = true) { + async contextMenu(botId, commandName) { if (!botId) throw new Error('Bot ID is required'); const user = await this.client.users.fetch(botId).catch(() => {}); if (!user || !user.bot || !user.applications) { @@ -1089,39 +1088,29 @@ class Message extends Base { throw new Error('Command name is required'); } // https://discord.com/api/v9/channels/817671035813888030/application-commands/search?type=3&application_id=817229550684471297 - let contextCMD = user.applications.cache.find(c => c.name == commandName && c.type === 'MESSAGE'); - if (!contextCMD && !search) { + let contextCMD; + const data = await this.client.api.channels[this.channelId]['application-commands'].search.get({ + query: { + type: 3, // MESSAGE, + // include_applications: false, + // query: commandName, + limit: 25, + // Shet + application_id: botId, + }, + }); + for (const command of data.application_commands) { + user.applications._add(command, true); + } + contextCMD = user.applications.cache.find(c => c.name == commandName && c.type === 'MESSAGE'); + if (!contextCMD) { throw new Error( 'INTERACTION_SEND_FAILURE', - `Command ${commandName} is not found (without search)\nList command avalible: ${user.applications.cache + `Command ${commandName} is not found (with search)\nList command avalible: ${user.applications.cache .filter(a => a.type == 'MESSAGE') .map(a => a.name) .join(', ')}`, ); - } else { - const data = await this.client.api.channels[this.channelId]['application-commands'].search.get({ - query: { - type: 3, // MESSAGE, - // include_applications: false, - // query: commandName, - limit: 25, - // Shet - application_id: botId, - }, - }); - for (const command of data.application_commands) { - user.applications._add(command, true); - } - contextCMD = user.applications.cache.find(c => c.name == commandName && c.type === 'MESSAGE'); - if (!contextCMD) { - throw new Error( - 'INTERACTION_SEND_FAILURE', - `Command ${commandName} is not found (with search)\nList command avalible: ${user.applications.cache - .filter(a => a.type == 'MESSAGE') - .map(a => a.name) - .join(', ')}`, - ); - } } return contextCMD.sendContextMenu(this, true); } diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index 085ee86..d4cdca4 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -393,11 +393,10 @@ class TextBasedChannel { * Send Slash to this channel * @param {Snowflake} botId Bot Id (Supports application ID - not bot) * @param {string} commandName Command name - * @param {boolean} [search=true] Whether to search for the command * @param {...?string} args Command arguments * @returns {Promise} */ - async sendSlash(botId, commandName, search = true, ...args) { + async sendSlash(botId, commandName, ...args) { if (!botId) throw new Error('Bot ID is required'); // ? maybe ... const user = await this.client.users.fetch(botId).catch(() => {}); @@ -405,42 +404,33 @@ class TextBasedChannel { throw new Error('botId is not a bot or does not have an application slash command'); } if (!commandName || typeof commandName !== 'string') throw new Error('Command name is required'); - let commandTarget = user.applications.cache.find(c => c.name === commandName && c.type === 'CHAT_INPUT'); - if (!commandTarget && !search) { + // Using API to search (without opcode ~ehehe) + let commandTarget; + // https://discord.com/api/v9/channels/id/application-commands/search?type=1&query=aiko&limit=7&include_applications=false&application_id=id + const data = await this.client.api.channels[this.id]['application-commands'].search.get({ + query: { + type: 1, // CHAT_INPUT, + include_applications: false, + query: commandName, + limit: 25, + // Shet + // application_id: botId, + }, + }); + for (const command of data.application_commands) { + if (user.id == command.application_id) { + commandTarget = user.applications._add(command, true); + } + } + // Remove + // commandTarget = user.applications.cache.find(c => c.name === commandName && c.type === 'CHAT_INPUT'); + if (!commandTarget) { throw new Error( 'INTERACTION_SEND_FAILURE', - `SlashCommand ${commandName} is not found (Without search)\nDebug:\n+ botId: ${botId}\n+ args: ${args.join( + `SlashCommand ${commandName} is not found (With search)\nDebug:\n+ botId: ${botId}\n+ args: ${args.join( ' | ', )}`, ); - } else { - // Using API to search (without opcode ~ehehe) - // https://discord.com/api/v9/channels/id/application-commands/search?type=1&query=aiko&limit=7&include_applications=false&application_id=id - const data = await this.client.api.channels[this.id]['application-commands'].search.get({ - query: { - type: 1, // CHAT_INPUT, - include_applications: false, - query: commandName, - limit: 25, - // Shet - // application_id: botId, - }, - }); - for (const command of data.application_commands) { - if (user.id == command.application_id) { - commandTarget = user.applications._add(command, true); - } - } - // Remove - // commandTarget = user.applications.cache.find(c => c.name === commandName && c.type === 'CHAT_INPUT'); - if (!commandTarget) { - throw new Error( - 'INTERACTION_SEND_FAILURE', - `SlashCommand ${commandName} is not found (With search)\nDebug:\n+ botId: ${botId}\n+ args: ${args.join( - ' | ', - )}`, - ); - } } return commandTarget.sendSlashCommand( new Message(this.client, { diff --git a/typings/index.d.ts b/typings/index.d.ts index 12b36f2..249733a 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1672,7 +1672,7 @@ export class Message extends Base { public clickButton(buttonID: string): Promise; public selectMenu(menuID: string, options: string[]): Promise; public selectMenu(options: string[]): Promise; - public contextMenu(botID: Snowflake, commandName: string, search?: boolean): Promise; + public contextMenu(botID: Snowflake, commandName: string): Promise; } export class MessageActionRow< @@ -3719,7 +3719,7 @@ export interface TextBasedChannelFields extends PartialTextBasedChannelFields { setNSFW(nsfw?: boolean, reason?: string): Promise; fetchWebhooks(): Promise>; sendTyping(): Promise; - sendSlash(botId: Snowflake, commandName: string, search?: boolean, ...args: any): Promise; + sendSlash(botId: Snowflake, commandName: string, ...args: any): Promise; } export function PartialWebhookMixin(Base?: Constructable): Constructable;