fix: opcode 24 (Interaction search function)

Todo: Rewrite slash command ...
This commit is contained in:
March 7th
2022-06-12 19:13:12 +07:00
parent 8eaa5e0c72
commit 07d52ee41b
11 changed files with 45 additions and 54 deletions

View File

@@ -626,9 +626,11 @@ class ApplicationCommand extends Base {
// ApplicationCommandData
version: this.version,
id: this.id,
guild_id: message.guildId,
name: this.name,
type: ApplicationCommandTypes[this.type],
options: option_,
attachments: [], // Todo
},
nonce: SnowflakeUtil.generate(),
},
@@ -648,7 +650,7 @@ class ApplicationCommand extends Base {
* await command.sendContextMenu(messsage);
*/
async sendContextMenu(message, sendFromMessage = false) {
if (!(message instanceof Message && !sendFromMessage)) {
if (!sendFromMessage && !(message instanceof Message)) {
throw new TypeError('The message must be a Discord.Message');
}
if (this.type == 'CHAT_INPUT') return false;

View File

@@ -36,6 +36,7 @@ const {
Events,
} = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
const SnowflakeUtil = require('../util/SnowflakeUtil');
const SystemChannelFlags = require('../util/SystemChannelFlags');
const Util = require('../util/Util');
@@ -615,34 +616,24 @@ class Guild extends AnonymousGuild {
/**
* Options for guildSearchInteraction
* @typedef {Object} GuildSearchInteractionOptions
* @property {?number|string} [type=1] {@link ApplicationCommandTypes}
* @property {?string} [query] Command name
* @property {?number} [limit=1] Maximum number of results
* @property {string} query Command name
* @property {?number} [limit=10] Maximum number of results
* @property {?number} [offset=0] Only return entries for actions made by this user
* @property {Snowflake[]} botId Array of bot IDs to filter by
* @property {?Snowflake} [botId] BotID
* @property {?ApplicationCommandType} [type=CHAT_INPUT] Type of command
*/
/**
* Searches for guild interactions
* @param {GuildSearchInteractionOptions} options Options for the search
* @returns {Promise}
* @returns {void | Promise<ApplicationCommand>}
*/
searchInteraction(options = {}) {
let { query, type, limit, offset, botId } = Object.assign(
{ query: undefined, type: 1, limit: 1, offset: 0, botId: [] },
let { query, limit, offset, botId, type } = Object.assign(
{ query: undefined, limit: 10, offset: 0, botId: undefined, type: 'CHAT_INPUT' },
options,
);
if (typeof type === 'string') {
if (type == 'CHAT_INPUT') type = 1;
else if (type == 'USER') type = 2;
else if (type == 'MESSAGE') type = 3;
// MODAL_SUMMIT :))
}
if (type < 1 || type > 3) {
throw new RangeError('Type must be 1 (CHAT_INPUT), 2 (USER), 3 (MESSAGE)');
}
if (typeof type !== 'number') {
throw new TypeError('Type must be a number | string');
}
if (!query) throw new Error('MISSING_VALUE', 'searchInteraction', 'query');
const nonce = SnowflakeUtil.generate();
this.shard.send({
op: Opcodes.REQUEST_APPLICATION_COMMANDS,
d: {
@@ -650,18 +641,21 @@ class Guild extends AnonymousGuild {
applications: false,
limit,
offset,
type,
query: query,
command_ids: Array.isArray(botId) ? botId : undefined,
query,
nonce,
},
});
if (!botId || !type) return undefined;
return new Promise((resolve, reject) => {
const handler = application => {
const handler = applications => {
timeout.refresh();
if (applications.nonce !== nonce) return;
const cmd = applications.application_commands.find(app => app.name == query && app.application_id == botId);
if (!cmd) return;
clearTimeout(timeout);
this.client.removeListener(Events.GUILD_APPLICATION_COMMANDS_UPDATE, handler);
this.client.decrementMaxListeners();
resolve(application.slice(0, limit));
resolve(this.client.users.cache.get(botId)?.applications?.cache?.find(c => c.name === query && c.type == type));
};
const timeout = setTimeout(() => {
this.client.removeListener(Events.GUILD_APPLICATION_COMMANDS_UPDATE, handler);

View File

@@ -1,8 +1,8 @@
'use strict';
/* eslint-disable import/order */
const { Message } = require('discord.js');
const MessageCollector = require('../MessageCollector');
const { Message } = require('../Message');
const MessagePayload = require('../MessagePayload');
const SnowflakeUtil = require('../../util/SnowflakeUtil');
const { Collection } = require('@discordjs/collection');
@@ -404,20 +404,9 @@ 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');
const commandTarget = (
user.applications.cache.find(c => c.name === commandName && c.type === 'CHAT_INPUT')
? this.guild
? await this.guild.searchInteraction({
type: 'CHAT_INPUT',
query: commandName,
botId: [botId],
limit: 1,
})
: await user.applications.fetch()
: user.applications.cache
).find(application => commandName == application.name && application.type == 'CHAT_INPUT');
const commandTarget = user.applications.cache.find(c => c.name === commandName && c.type === 'CHAT_INPUT');
if (!commandTarget) {
throw new Error(`Command ${commandName} is not found`);
throw new Error(`Command ${commandName} is not found\nDebug:\n+ botId: ${botId}\n+ args: ${args.join(' | ')}`);
}
return commandTarget.sendSlashCommand(
new Message(this.client, {