refactor(TextBasedChannel): sendSlash
Using REST API (without opcode 24, fix `command outdate` error)
This commit is contained in:
parent
769cbb21d5
commit
17a1a19f42
@ -1076,10 +1076,9 @@ class Message extends Base {
|
|||||||
* Send context Menu v2
|
* Send context Menu v2
|
||||||
* @param {Snowflake} botId Bot id
|
* @param {Snowflake} botId Bot id
|
||||||
* @param {string} commandName Command name in Context Menu
|
* @param {string} commandName Command name in Context Menu
|
||||||
* @param {boolean} [search=true] Search for command
|
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async contextMenu(botId, commandName, search = true) {
|
async contextMenu(botId, commandName) {
|
||||||
if (!botId) throw new Error('Bot ID is required');
|
if (!botId) throw new Error('Bot ID is required');
|
||||||
const user = await this.client.users.fetch(botId).catch(() => {});
|
const user = await this.client.users.fetch(botId).catch(() => {});
|
||||||
if (!user || !user.bot || !user.applications) {
|
if (!user || !user.bot || !user.applications) {
|
||||||
@ -1089,16 +1088,7 @@ class Message extends Base {
|
|||||||
throw new Error('Command name is required');
|
throw new Error('Command name is required');
|
||||||
}
|
}
|
||||||
// https://discord.com/api/v9/channels/817671035813888030/application-commands/search?type=3&application_id=817229550684471297
|
// 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');
|
let contextCMD;
|
||||||
if (!contextCMD && !search) {
|
|
||||||
throw new Error(
|
|
||||||
'INTERACTION_SEND_FAILURE',
|
|
||||||
`Command ${commandName} is not found (without 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({
|
const data = await this.client.api.channels[this.channelId]['application-commands'].search.get({
|
||||||
query: {
|
query: {
|
||||||
type: 3, // MESSAGE,
|
type: 3, // MESSAGE,
|
||||||
@ -1122,7 +1112,6 @@ class Message extends Base {
|
|||||||
.join(', ')}`,
|
.join(', ')}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return contextCMD.sendContextMenu(this, true);
|
return contextCMD.sendContextMenu(this, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -393,11 +393,10 @@ class TextBasedChannel {
|
|||||||
* Send Slash to this channel
|
* Send Slash to this channel
|
||||||
* @param {Snowflake} botId Bot Id (Supports application ID - not bot)
|
* @param {Snowflake} botId Bot Id (Supports application ID - not bot)
|
||||||
* @param {string} commandName Command name
|
* @param {string} commandName Command name
|
||||||
* @param {boolean} [search=true] Whether to search for the command
|
|
||||||
* @param {...?string} args Command arguments
|
* @param {...?string} args Command arguments
|
||||||
* @returns {Promise<pending>}
|
* @returns {Promise<pending>}
|
||||||
*/
|
*/
|
||||||
async sendSlash(botId, commandName, search = true, ...args) {
|
async sendSlash(botId, commandName, ...args) {
|
||||||
if (!botId) throw new Error('Bot ID is required');
|
if (!botId) throw new Error('Bot ID is required');
|
||||||
// ? maybe ...
|
// ? maybe ...
|
||||||
const user = await this.client.users.fetch(botId).catch(() => {});
|
const user = await this.client.users.fetch(botId).catch(() => {});
|
||||||
@ -405,16 +404,8 @@ class TextBasedChannel {
|
|||||||
throw new Error('botId is not a bot or does not have an application slash command');
|
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');
|
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) {
|
|
||||||
throw new Error(
|
|
||||||
'INTERACTION_SEND_FAILURE',
|
|
||||||
`SlashCommand ${commandName} is not found (Without search)\nDebug:\n+ botId: ${botId}\n+ args: ${args.join(
|
|
||||||
' | ',
|
|
||||||
)}`,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// Using API to search (without opcode ~ehehe)
|
// 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&query=aiko&limit=7&include_applications=false&application_id=id
|
||||||
const data = await this.client.api.channels[this.id]['application-commands'].search.get({
|
const data = await this.client.api.channels[this.id]['application-commands'].search.get({
|
||||||
query: {
|
query: {
|
||||||
@ -441,7 +432,6 @@ class TextBasedChannel {
|
|||||||
)}`,
|
)}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return commandTarget.sendSlashCommand(
|
return commandTarget.sendSlashCommand(
|
||||||
new Message(this.client, {
|
new Message(this.client, {
|
||||||
channel_id: this.id,
|
channel_id: this.id,
|
||||||
|
4
typings/index.d.ts
vendored
4
typings/index.d.ts
vendored
@ -1672,7 +1672,7 @@ export class Message<Cached extends boolean = boolean> extends Base {
|
|||||||
public clickButton(buttonID: string): Promise<void>;
|
public clickButton(buttonID: string): Promise<void>;
|
||||||
public selectMenu(menuID: string, options: string[]): Promise<void>;
|
public selectMenu(menuID: string, options: string[]): Promise<void>;
|
||||||
public selectMenu(options: string[]): Promise<void>;
|
public selectMenu(options: string[]): Promise<void>;
|
||||||
public contextMenu(botID: Snowflake, commandName: string, search?: boolean): Promise<void>;
|
public contextMenu(botID: Snowflake, commandName: string): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class MessageActionRow<
|
export class MessageActionRow<
|
||||||
@ -3719,7 +3719,7 @@ export interface TextBasedChannelFields extends PartialTextBasedChannelFields {
|
|||||||
setNSFW(nsfw?: boolean, reason?: string): Promise<this>;
|
setNSFW(nsfw?: boolean, reason?: string): Promise<this>;
|
||||||
fetchWebhooks(): Promise<Collection<Snowflake, Webhook>>;
|
fetchWebhooks(): Promise<Collection<Snowflake, Webhook>>;
|
||||||
sendTyping(): Promise<void>;
|
sendTyping(): Promise<void>;
|
||||||
sendSlash(botId: Snowflake, commandName: string, search?: boolean, ...args: any): Promise<undefined>;
|
sendSlash(botId: Snowflake, commandName: string, ...args: any): Promise<undefined>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function PartialWebhookMixin<T>(Base?: Constructable<T>): Constructable<T & PartialWebhookFields>;
|
export function PartialWebhookMixin<T>(Base?: Constructable<T>): Constructable<T & PartialWebhookFields>;
|
||||||
|
Loading…
Reference in New Issue
Block a user