feat(TextChannel): add searchInteraction

This commit is contained in:
March 7th
2022-10-30 19:25:32 +07:00
parent 91ae4195a6
commit a6459f2c14
12 changed files with 75 additions and 105 deletions

View File

@@ -873,8 +873,15 @@ class ApplicationCommand extends Base {
clearTimeout(timeout);
this.client.removeListener('interactionResponse', handler);
this.client.decrementMaxListeners();
if (data.status) resolve(data.metadata);
else reject(data.metadata);
if (data.status) {
resolve(data.metadata);
} else {
reject(
new Error('INTERACTION_ERROR', {
cause: data,
}),
);
}
};
const timeout = setTimeout(() => {
this.client.removeListener('interactionResponse', handler);
@@ -958,8 +965,15 @@ class ApplicationCommand extends Base {
clearTimeout(timeout);
this.client.removeListener('interactionResponse', handler);
this.client.decrementMaxListeners();
if (data.status) resolve(data.metadata);
else reject(data.metadata);
if (data.status) {
resolve(data.metadata);
} else {
reject(
new Error('INTERACTION_ERROR', {
cause: data,
}),
);
}
};
const timeout = setTimeout(() => {
this.client.removeListener('interactionResponse', handler);

View File

@@ -185,6 +185,7 @@ class BaseGuildTextChannel extends GuildChannel {
setRateLimitPerUser() {}
setNSFW() {}
sendSlash() {}
searchInteraction() {}
}
TextBasedChannel.applyToClass(BaseGuildTextChannel, true);

View File

@@ -149,6 +149,7 @@ class DMChannel extends Channel {
createMessageComponentCollector() {}
awaitMessageComponent() {}
sendSlash() {}
searchInteraction() {}
// Doesn't work on DM channels; bulkDelete() {}
// Doesn't work on DM channels; setRateLimitPerUser() {}
// Doesn't work on DM channels; setNSFW() {}

View File

@@ -1,7 +1,6 @@
'use strict';
const process = require('node:process');
const setTimeout = require('node:timers').setTimeout;
const { Collection } = require('@discordjs/collection');
const AnonymousGuild = require('./AnonymousGuild');
const GuildAuditLogs = require('./GuildAuditLogs');
@@ -32,12 +31,8 @@ const {
Status,
MFALevels,
PremiumTiers,
Opcodes,
Events,
ApplicationCommandTypes,
} = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
const SnowflakeUtil = require('../util/SnowflakeUtil');
const SystemChannelFlags = require('../util/SystemChannelFlags');
const Util = require('../util/Util');
@@ -625,67 +620,6 @@ class Guild extends AnonymousGuild {
}
}
/**
* Options for guildSearchInteraction
* @typedef {Object} GuildSearchInteractionOptions
* @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] BotID
* @property {?ApplicationCommandType} [type=CHAT_INPUT] Type of command
*/
/**
* Searches for guild interactions
* @param {GuildSearchInteractionOptions} options Options for the search
* @deprecated
* @returns {void | Promise<ApplicationCommand>}
*/
searchInteraction(options = {}) {
let { query, limit, offset, botId, type } = Object.assign(
{ query: undefined, limit: 10, offset: 0, botId: undefined, type: 'CHAT_INPUT' },
options,
);
if (!query) throw new Error('MISSING_VALUE', 'searchInteraction', 'query');
const nonce = SnowflakeUtil.generate();
this.shard.send({
op: Opcodes.REQUEST_APPLICATION_COMMANDS,
d: {
guild_id: this.id,
applications: false,
limit,
offset,
query,
nonce,
},
});
if (!botId || !type) return undefined;
return new Promise((resolve, reject) => {
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(
this.client.users.cache
.get(botId)
?.application?.commands?.cache?.find(
c => (c.name === query && c.type == type) || c.type == ApplicationCommandTypes[type],
),
);
};
const timeout = setTimeout(() => {
this.client.removeListener(Events.GUILD_APPLICATION_COMMANDS_UPDATE, handler);
this.client.decrementMaxListeners();
reject(new Error('GUILD_APPLICATION_COMMANDS_SEARCH_TIMEOUT'));
}, 10000).unref();
this.client.incrementMaxListeners();
this.client.on(Events.GUILD_APPLICATION_COMMANDS_UPDATE, handler);
});
}
/**
* Fetches a collection of integrations to this guild.
* Resolves with a collection mapping integrations by their ids.

View File

@@ -203,8 +203,15 @@ class MessageButton extends BaseMessageComponent {
clearTimeout(timeout);
message.client.removeListener('interactionResponse', handler);
message.client.decrementMaxListeners();
if (data.status) resolve(data.metadata);
else reject(data.metadata);
if (data.status) {
resolve(data.metadata);
} else {
reject(
new Error('INTERACTION_ERROR', {
cause: data,
}),
);
}
};
const timeout = setTimeout(() => {
message.client.removeListener('interactionResponse', handler);

View File

@@ -273,8 +273,15 @@ class MessageSelectMenu extends BaseMessageComponent {
clearTimeout(timeout);
message.client.removeListener('interactionResponse', handler);
message.client.decrementMaxListeners();
if (data.status) resolve(data.metadata);
else reject(data.metadata);
if (data.status) {
resolve(data.metadata);
} else {
reject(
new Error('INTERACTION_ERROR', {
cause: data,
}),
);
}
};
const timeout = setTimeout(() => {
message.client.removeListener('interactionResponse', handler);

View File

@@ -407,6 +407,34 @@ class TextBasedChannel {
return this.edit({ nsfw }, reason);
}
/**
* Search Slash Command (return raw data)
* @param {Snowflake} applicationId Application ID
* @param {?ApplicationCommandType} type Command Type
* @returns {Object}
*/
searchInteraction(applicationId, type = 'CHAT_INPUT') {
switch (type) {
case 'USER':
case 2:
type = 2;
break;
case 'MESSAGE':
case 3:
type = 3;
break;
default:
type = 1;
break;
}
return this.client.api.channels[this.id]['application-commands'].search.get({
query: {
type,
application_id: applicationId,
},
});
}
/**
* Send Slash to this channel
* @param {UserResolvable} bot Bot user
@@ -459,15 +487,7 @@ class TextBasedChannel {
}
if (user._partial) await user.getProfile().catch(() => {});
if (!commandName || typeof commandName !== 'string') throw new Error('Command name is required');
// Using API to search (without opcode ~ehehe)
// https://discord.com/api/v9/channels/id/application-commands/search?type=1&application_id=161660517914509312
const query = {
type: 1, // Slash commands
application_id: user.application?.id ?? user.id,
};
const data = await this.client.api.channels[this.id]['application-commands'].search.get({
query,
});
const data = await this.searchInteraction(user.application?.id ?? user.id, 'CHAT_INPUT');
for (const command of data.application_commands) {
if (user.id == command.application_id || user.application.id == command.application_id) {
user.application?.commands?._add(command, true);
@@ -515,6 +535,7 @@ class TextBasedChannel {
'setRateLimitPerUser',
'setNSFW',
'sendSlash',
'searchInteraction',
);
}
for (const prop of props) {