From fe1f9911c58d7379528c0f4d7180bca754e6fd0d Mon Sep 17 00:00:00 2001 From: March 7th <71698422+aiko-chan-ai@users.noreply.github.com> Date: Sun, 11 Sep 2022 09:39:57 +0700 Subject: [PATCH] fix: sendSlash search failure (>25 bot) --- src/errors/Messages.js | 1 + src/structures/User.js | 4 +- src/structures/interfaces/TextBasedChannel.js | 37 +++++++------------ typings/index.d.ts | 2 +- 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/errors/Messages.js b/src/errors/Messages.js index 56335f4..84df648 100644 --- a/src/errors/Messages.js +++ b/src/errors/Messages.js @@ -52,6 +52,7 @@ const Messages = { EMBED_PROVIDER_NAME: 'MessageEmbed provider name must be a string.', INVALID_COMMAND_NAME: allCMD => `Could not parse subGroupCommand and subCommand due to too long: ${allCMD.join(' ')}`, INVALID_RANGE_QUERY_MEMBER: 'Invalid range query member. (0} @@ -418,7 +418,7 @@ class TextBasedChannel { * // CommandName is Group Command / Sub Command * channel.sendSlash('123456789012345678', 'embed title', 'description', 'author', '#00ff00') */ - async sendSlash(botId, commandString, ...args) { + async sendSlash(bot, commandString, ...args) { args = args.flat(2); const cmd = commandString.trim().split(' '); // Validate CommandName @@ -430,7 +430,8 @@ class TextBasedChannel { } validateName(sub[i]); } - if (!botId) throw new Error('Bot ID is required'); + if (!bot) throw new Error('MUST_SPECIFY_BOT'); + const botId = this.client.users.resolveId(bot); // ? maybe ... const user = await this.client.users.fetch(botId).catch(() => {}); if (!user || !user.bot || !user.application) { @@ -439,41 +440,29 @@ class TextBasedChannel { if (user._partial) await user.getProfile(); if (!commandName || typeof commandName !== 'string') throw new Error('Command name is required'); // 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 + // https://discord.com/api/v9/channels/id/application-commands/search?type=1&application_id=161660517914509312 const query = { type: 1, // Slash commands - include_applications: false, + application_id: user.application?.id ?? user.id, }; - if (this.client.channels.cache.get(this.id)?.type == 'DM') { - query.application_id = user.application.id; - } else { - query.limit = 25; - query.query = commandName; - } const data = await this.client.api.channels[this.id]['application-commands'].search.get({ query, }); for (const command of data.application_commands) { if (user.id == command.application_id || user.application.id == command.application_id) { - const c = user.application?.commands?._add(command, true); - if (command.name == commandName) commandTarget = c; - } else { - const tempUser = this.client.users.cache.get(command.application_id); - if (tempUser && tempUser.bot && tempUser.application) { - tempUser.application?.commands?._add(command, true); - } + user.application?.commands?._add(command, true); } } // Remove - commandTarget = - commandTarget || user.application?.commands?.cache.find(c => c.name === commandName && c.type === 'CHAT_INPUT'); + const commandTarget = user.application?.commands?.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( - ' | ', - )}`, + `SlashCommand ${commandName} is not found (With search)\nDebug:\n+ botId: ${botId} (ApplicationId: ${ + user.application?.id + })\n+ args: ${args.join(' | ') || null}`, ); } return commandTarget.sendSlashCommand( diff --git a/typings/index.d.ts b/typings/index.d.ts index 908524d..141405f 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -4061,7 +4061,7 @@ export interface TextBasedChannelFields extends PartialTextBasedChannelFields { setNSFW(nsfw?: boolean, reason?: string): Promise; fetchWebhooks(): Promise>; sendTyping(): Promise; - sendSlash(botId: Snowflake, commandName: string, ...args: any): Promise; + sendSlash(bot: UserResolvable, commandName: string, ...args: any): Promise; } export function PartialWebhookMixin(Base?: Constructable): Constructable;