feat: interactions (#5448)

Co-authored-by: izexi <43889168+izexi@users.noreply.github.com>
Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
Co-authored-by: Advaith <advaithj1@gmail.com>
Co-authored-by: Shiaupiau <stu43005@gmail.com>
Co-authored-by: monbrey <rsm999@uowmail.edu.au>
Co-authored-by: Tiemen <ThaTiemsz@users.noreply.github.com>
Co-authored-by: Carter <carter@elhnet.net>
This commit is contained in:
Jan
2021-05-07 17:22:33 +02:00
committed by GitHub
parent af00ec8970
commit f7643f7bbe
24 changed files with 1340 additions and 35 deletions

View File

@@ -0,0 +1,23 @@
'use strict';
const { Events, InteractionTypes } = require('../../../util/Constants');
let Structures;
module.exports = (client, { d: data }) => {
if (data.type === InteractionTypes.APPLICATION_COMMAND) {
if (!Structures) Structures = require('../../../util/Structures');
const CommandInteraction = Structures.get('CommandInteraction');
const interaction = new CommandInteraction(client, data);
/**
* Emitted when an interaction is created.
* @event Client#interaction
* @param {Interaction} interaction The interaction which was created
*/
client.emit(Events.INTERACTION_CREATE, interaction);
return;
}
client.emit(Events.DEBUG, `[INTERACTION] Received interaction with unknown type: ${data.type}`);
};

View File

@@ -110,6 +110,8 @@ const Messages = {
FETCH_GROUP_DM_CHANNEL: "Bots don't have access to Group DM Channels and cannot fetch them",
MEMBER_FETCH_NONCE_LENGTH: 'Nonce length must not exceed 32 characters.',
INTERACTION_ALREADY_REPLIED: 'This interaction has already been deferred or replied to.',
};
for (const [name, message] of Object.entries(Messages)) register(name, message);

View File

@@ -33,8 +33,10 @@ module.exports = {
version: require('../package.json').version,
// Managers
ApplicationCommandManager: require('./managers/ApplicationCommandManager'),
BaseGuildEmojiManager: require('./managers/BaseGuildEmojiManager'),
ChannelManager: require('./managers/ChannelManager'),
GuildApplicationCommandManager: require('./managers/GuildApplicationCommandManager'),
GuildChannelManager: require('./managers/GuildChannelManager'),
GuildEmojiManager: require('./managers/GuildEmojiManager'),
GuildEmojiRoleManager: require('./managers/GuildEmojiRoleManager'),
@@ -58,6 +60,7 @@ module.exports = {
// Structures
Application: require('./structures/interfaces/Application'),
ApplicationCommand: require('./structures/ApplicationCommand'),
Base: require('./structures/Base'),
Activity: require('./structures/Presence').Activity,
APIMessage: require('./structures/APIMessage'),
@@ -71,6 +74,7 @@ module.exports = {
return require('./structures/ClientUser');
},
Collector: require('./structures/interfaces/Collector'),
CommandInteraction: require('./structures/CommandInteraction'),
DMChannel: require('./structures/DMChannel'),
Emoji: require('./structures/Emoji'),
Guild: require('./structures/Guild'),
@@ -82,6 +86,7 @@ module.exports = {
GuildTemplate: require('./structures/GuildTemplate'),
Integration: require('./structures/Integration'),
IntegrationApplication: require('./structures/IntegrationApplication'),
Interaction: require('./structures/Interaction'),
Invite: require('./structures/Invite'),
Message: require('./structures/Message'),
MessageAttachment: require('./structures/MessageAttachment'),

View File

@@ -0,0 +1,301 @@
'use strict';
const BaseManager = require('./BaseManager');
const { TypeError } = require('../errors');
const ApplicationCommand = require('../structures/ApplicationCommand');
const Collection = require('../util/Collection');
const { ApplicationCommandPermissionTypes } = require('../util/Constants');
/**
* Manages API methods for application commands and stores their cache.
* @extends {BaseManager}
*/
class ApplicationCommandManager extends BaseManager {
constructor(client, iterable) {
super(client, iterable, ApplicationCommand);
}
/**
* The cache of this manager
* @type {Collection<Snowflake, ApplicationCommand>}
* @name ApplicationCommandManager#cache
*/
add(data, cache) {
return super.add(data, cache, { extras: [this.guild] });
}
/**
* The APIRouter path to the commands
* @type {Object}
* @readonly
* @private
*/
get commandPath() {
let path = this.client.api.applications(this.client.application.id);
if (this.guild) path = path.guilds(this.guild.id);
return path.commands;
}
/**
* Data that resolves to give an ApplicationCommand object. This can be:
* * An ApplicationCommand object
* * A Snowflake
* @typedef {ApplicationCommand|Snowflake} ApplicationCommandResolvable
*/
/**
* Obtains one or multiple application commands from Discord, or the cache if it's already available.
* @param {Snowflake} [id] ID of the application command
* @param {boolean} [cache=true] Whether to cache the new application commands if they weren't already
* @param {boolean} [force=false] Whether to skip the cache check and request the API
* @returns {Promise<ApplicationCommand|Collection<Snowflake, ApplicationCommand>>}
* @example
* // Fetch a single command
* client.application.commands.fetch('123456789012345678')
* .then(command => console.log(`Fetched command ${command.name}`))
* .catch(console.error);
* @example
* // Fetch all commands
* guild.commands.fetch()
* .then(commands => console.log(`Fetched ${commands.size} commands`))
* .catch(console.error);
*/
async fetch(id, cache = true, force = false) {
if (id) {
if (!force) {
const existing = this.cache.get(id);
if (existing) return existing;
}
const command = await this.commandPath(id).get();
return this.add(command, cache);
}
const data = await this.commandPath.get();
return data.reduce((coll, command) => coll.set(command.id, this.add(command, cache)), new Collection());
}
/**
* Creates an application command.
* @param {ApplicationCommandData} command The command
* @returns {Promise<ApplicationCommand>}
* @example
* // Create a new command
* client.application.commands.create({
* name: 'test',
* description: 'A test command',
* })
* .then(console.log)
* .catch(console.error);
*/
async create(command) {
const data = await this.commandPath.post({
data: this.constructor.transformCommand(command),
});
return this.add(data);
}
/**
* Sets all the commands for this application or guild.
* @param {ApplicationCommandData[]} commands The commands
* @returns {Promise<Collection<Snowflake, ApplicationCommand>>}
* @example
* // Set all commands to just this one
* client.application.commands.set([
* {
* name: 'test',
* description: 'A test command',
* },
* ])
* .then(console.log)
* .catch(console.error);
* @example
* // Remove all commands
* guild.commands.set([])
* .then(console.log)
* .catch(console.error);
*/
async set(commands) {
const data = await this.commandPath.put({
data: commands.map(c => this.constructor.transformCommand(c)),
});
return data.reduce((coll, command) => coll.set(command.id, this.add(command)), new Collection());
}
/**
* Edits an application command.
* @param {ApplicationCommandResolvable} command The command to edit
* @param {ApplicationCommandData} data The data to update the command with
* @returns {Promise<ApplicationCommand>}
* @example
* // Edit an existing command
* client.application.commands.edit('123456789012345678', {
* description: 'New description',
* })
* .then(console.log)
* .catch(console.error);
*/
async edit(command, data) {
const id = this.resolveID(command);
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
const patched = await this.commandPath(id).patch({ data: this.constructor.transformCommand(data) });
return this.add(patched);
}
/**
* Deletes an application command.
* @param {ApplicationCommandResolvable} command The command to delete
* @returns {Promise<?ApplicationCommand>}
* @example
* // Delete a command
* guild.commands.delete('123456789012345678')
* .then(console.log)
* .catch(console.error);
*/
async delete(command) {
const id = this.resolveID(command);
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
await this.commandPath(id).delete();
const cached = this.cache.get(id);
this.cache.delete(id);
return cached ?? null;
}
/**
* Fetches the permissions for one or multiple commands.
* @param {ApplicationCommandResolvable} [command] The command to get the permissions from
* @returns {Promise<ApplicationCommandPermissions[]|Collection<Snowflake, ApplicationCommandPermissions[]>>}
* @example
* // Fetch permissions for one command
* guild.commands.fetchPermissions('123456789012345678')
* .then(perms => console.log(`Fetched permissions for ${perms.length} users`))
* .catch(console.error);
* @example
* // Fetch permissions for all commands
* client.application.commands.fetchPermissions()
* .then(perms => console.log(`Fetched permissions for ${perms.size} commands`))
* .catch(console.error);
*/
async fetchPermissions(command) {
if (command) {
const id = this.resolveID(command);
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
const data = await this.commandPath(id).permissions.get();
return data.permissions.map(perm => this.constructor.transformPermissions(perm, true));
}
const data = await this.commandPath.permissions.get();
return data.reduce(
(coll, perm) =>
coll.set(
perm.id,
perm.permissions.map(p => this.constructor.transformPermissions(p, true)),
),
new Collection(),
);
}
/**
* Data used for overwriting the permissions for all application commands in a guild.
* @typedef {object} GuildApplicationCommandPermissionData
* @prop {Snowflake} command The ID of the command
* @prop {ApplicationCommandPermissionData[]} permissions The permissions for this command
*/
/**
* Sets the permissions for a command.
* @param {ApplicationCommandResolvable|GuildApplicationCommandPermissionData[]} command The command to edit the
* permissions for, or an array of guild application command permissions to set the permissions of all commands
* @param {ApplicationCommandPermissionData[]} permissions The new permissions for the command
* @returns {Promise<ApplicationCommandPermissions[]|Collection<Snowflake, ApplicationCommandPermissions[]>>}
* @example
* // Set the permissions for one command
* client.commands.setPermissions('123456789012345678', [
* {
* id: '876543210987654321',
* type: 'USER',
* permission: false,
* },
* ])
* .then(console.log)
* .catch(console.error);
* @example
* // Set the permissions for all commands
* guild.commands.setPermissions([
* {
* id: '123456789012345678',
* permissions: [{
* id: '876543210987654321',
* type: 'USER',
* permission: false,
* }],
* },
* ])
* .then(console.log)
* .catch(console.error);
*/
async setPermissions(command, permissions) {
const id = this.resolveID(command);
if (id) {
const data = await this.commandPath(id).permissions.put({
data: { permissions: permissions.map(perm => this.constructor.transformPermissions(perm)) },
});
return data.permissions.map(perm => this.constructor.transformPermissions(perm, true));
}
const data = await this.commandPath.permissions.put({
data: command.map(perm => ({
id: perm.id,
permissions: perm.permissions.map(p => this.constructor.transformPermissions(p)),
})),
});
return data.reduce(
(coll, perm) =>
coll.set(
perm.id,
perm.permissions.map(p => this.constructor.transformPermissions(p, true)),
),
new Collection(),
);
}
/**
* Transforms an {@link ApplicationCommandData} object into something that can be used with the API.
* @param {ApplicationCommandData} command The command to transform
* @returns {Object}
* @private
*/
static transformCommand(command) {
return {
name: command.name,
description: command.description,
options: command.options?.map(o => ApplicationCommand.transformOption(o)),
default_permission: command.defaultPermission,
};
}
/**
* Transforms an {@link ApplicationCommandPermissionData} object into something that can be used with the API.
* @param {ApplicationCommandPermissionData} permissions The permissions to transform
* @param {boolean} [received] Whether these permissions have been received from Discord
* @returns {Object}
* @private
*/
static transformPermissions(permissions, received) {
return {
id: permissions.id,
permission: permissions.permission,
type:
typeof permissions.type === 'number' && !received
? permissions.type
: ApplicationCommandPermissionTypes[permissions.type],
};
}
}
module.exports = ApplicationCommandManager;

View File

@@ -0,0 +1,21 @@
'use strict';
const ApplicationCommandManager = require('./ApplicationCommandManager');
/**
* An extension for guild-specific application commands.
* @extends {ApplicationCommandManager}
*/
class GuildApplicationCommandManager extends ApplicationCommandManager {
constructor(guild, iterable) {
super(guild.client, iterable);
/**
* The guild that this manager belongs to
* @type {Guild}
*/
this.guild = guild;
}
}
module.exports = GuildApplicationCommandManager;

View File

@@ -18,9 +18,10 @@ class RESTManager {
this.globalReset = null;
this.globalDelay = null;
if (client.options.restSweepInterval > 0) {
client.setInterval(() => {
const interval = client.setInterval(() => {
this.handlers.sweep(handler => handler._inactive);
}, client.options.restSweepInterval * 1000);
interval.unref();
}
}

View File

@@ -73,6 +73,16 @@ class APIMessage {
return this.target instanceof Message;
}
/**
* Whether or not the target is an interaction
* @type {boolean}
* @readonly
*/
get isInteraction() {
const Interaction = require('./Interaction');
return this.target instanceof Interaction;
}
/**
* Makes the content of this message.
* @returns {?(string|string[])}
@@ -129,7 +139,7 @@ class APIMessage {
}
const embedLikes = [];
if (this.isWebhook) {
if (this.isInteraction || this.isWebhook) {
if (this.options.embeds) {
embedLikes.push(...this.options.embeds);
}
@@ -149,6 +159,8 @@ class APIMessage {
if (this.isMessage) {
// eslint-disable-next-line eqeqeq
flags = this.options.flags != null ? new MessageFlags(this.options.flags).bitfield : this.target.flags.bitfield;
} else if (this.isInteraction && this.options.ephemeral) {
flags = MessageFlags.FLAGS.EPHEMERAL;
}
let allowedMentions =
@@ -196,7 +208,7 @@ class APIMessage {
if (this.files) return this;
const embedLikes = [];
if (this.isWebhook) {
if (this.isInteraction || this.isWebhook) {
if (this.options.embeds) {
embedLikes.push(...this.options.embeds);
}
@@ -348,10 +360,11 @@ class APIMessage {
* @returns {MessageOptions|WebhookMessageOptions}
*/
static create(target, content, options, extra = {}) {
const Interaction = require('./Interaction');
const Webhook = require('./Webhook');
const WebhookClient = require('../client/WebhookClient');
const isWebhook = target instanceof Webhook || target instanceof WebhookClient;
const isWebhook = target instanceof Interaction || target instanceof Webhook || target instanceof WebhookClient;
const transformed = this.transformOptions(content, options, extra, isWebhook);
return new this(target, transformed);
}

View File

@@ -0,0 +1,218 @@
'use strict';
const Base = require('./Base');
const { ApplicationCommandOptionTypes } = require('../util/Constants');
const SnowflakeUtil = require('../util/SnowflakeUtil');
/**
* Represents an application command.
* @extends {Base}
*/
class ApplicationCommand extends Base {
constructor(client, data, guild) {
super(client);
/**
* The ID of this command
* @type {Snowflake}
*/
this.id = data.id;
/**
* The guild this command is part of
* @type {?Guild}
*/
this.guild = guild ?? null;
this._patch(data);
}
_patch(data) {
/**
* The name of this command
* @type {string}
*/
this.name = data.name;
/**
* The description of this command
* @type {string}
*/
this.description = data.description;
/**
* The options of this command
* @type {ApplicationCommandOption[]}
*/
this.options = data.options?.map(o => this.constructor.transformOption(o, true)) ?? [];
/**
* Whether the command is enabled by default when the app is added to a guild
* @type {boolean}
*/
this.defaultPermission = data.default_permission;
}
/**
* The timestamp the command was created at
* @type {number}
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
}
/**
* The time the command was created at
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}
/**
* The manager that this command belongs to
* @type {ApplicationCommandManager}
* @readonly
*/
get manager() {
return (this.guild ?? this.client.application).commands;
}
/**
* Data for creating or editing an application command.
* @typedef {Object} ApplicationCommandData
* @property {string} name The name of the command
* @property {string} description The description of the command
* @property {ApplicationCommandOptionData[]} [options] Options for the command
* @property {boolean} [defaultPermission] Whether the command is enabled by default when the app is added to a guild
*/
/**
* An option for an application command or subcommand.
* @typedef {Object} ApplicationCommandOptionData
* @property {ApplicationCommandOptionType|number} type The type of the option
* @property {string} name The name of the option
* @property {string} description The description of the option
* @property {boolean} [required] Whether the option is required
* @property {ApplicationCommandOptionChoice[]} [choices] The choices of the option for the user to pick from
* @property {ApplicationCommandOptionData[]} [options] Additional options if this option is a subcommand (group)
*/
/**
* Edits this application command.
* @param {ApplicationCommandData} data The data to update the command with
* @returns {Promise<ApplicationCommand>}
* @example
* // Edit the description of this command
* command.edit({
* description: 'New description',
* })
* .then(console.log)
* .catch(console.error);
*/
edit(data) {
return this.manager.edit(this, data);
}
/**
* Deletes this command.
* @returns {Promise<ApplicationCommand>}
* @example
* // Delete this command
* command.delete()
* .then(console.log)
* .catch(console.error);
*/
delete() {
return this.manager.delete(this);
}
/**
* The object returned when fetching permissions for an application command.
* @typedef {object} ApplicationCommandPermissionData
* @property {Snowflake} id The ID of the role or user
* @property {ApplicationCommandPermissionType|number} type Whether this permission if for a role or a user
* @property {boolean} permission Whether the role or user has the permission to use this command
*/
/**
* The object returned when fetching permissions for an application command.
* @typedef {object} ApplicationCommandPermissions
* @property {Snowflake} id The ID of the role or user
* @property {ApplicationCommandPermissionType} type Whether this permission if for a role or a user
* @property {boolean} permission Whether the role or user has the permission to use this command
*/
/**
* Fetches the permissions for this command.
* @returns {Promise<ApplicationCommandPermissions[]>}
* @example
* // Fetch permissions for this command
* command.fetchPermissions()
* .then(perms => console.log(`Fetched permissions for ${perms.length} users`))
* .catch(console.error);
*/
fetchPermissions() {
return this.manager.fetchPermissions(this);
}
/**
* Sets the permissions for this command.
* @param {ApplicationCommandPermissionData[]} permissions The new permissions for the command
* @returns {Promise<ApplicationCommandPermissions[]>}
* @example
* // Set the permissions for this command
* command.setPermissions([
* {
* id: '876543210987654321',
* type: 'USER',
* permission: false,
* },
* ])
* .then(console.log)
* .catch(console.error);
*/
setPermissions(permissions) {
return this.manager.setPermissions(this, permissions);
}
/**
* An option for an application command or subcommand.
* @typedef {Object} ApplicationCommandOption
* @property {ApplicationCommandOptionType} type The type of the option
* @property {string} name The name of the option
* @property {string} description The description of the option
* @property {boolean} [required] Whether the option is required
* @property {ApplicationCommandOptionChoice[]} [choices] The choices of the option for the user to pick from
* @property {ApplicationCommandOption[]} [options] Additional options if this option is a subcommand (group)
*/
/**
* A choice for an application command option.
* @typedef {Object} ApplicationCommandOptionChoice
* @property {string} name The name of the choice
* @property {string|number} value The value of the choice
*/
/**
* Transforms an {@link ApplicationCommandOptionData} object into something that can be used with the API.
* @param {ApplicationCommandOptionData} option The option to transform
* @param {boolean} [received] Whether this option has been received from Discord
* @returns {Object}
* @private
*/
static transformOption(option, received) {
return {
type: typeof option.type === 'number' && !received ? option.type : ApplicationCommandOptionTypes[option.type],
name: option.name,
description: option.description,
required: option.required,
choices: option.choices,
options: option.options?.map(o => this.transformOption(o)),
};
}
}
module.exports = ApplicationCommand;

View File

@@ -2,6 +2,7 @@
const Team = require('./Team');
const Application = require('./interfaces/Application');
const ApplicationCommandManager = require('../managers/ApplicationCommandManager');
const ApplicationFlags = require('../util/ApplicationFlags');
/**
@@ -9,6 +10,16 @@ const ApplicationFlags = require('../util/ApplicationFlags');
* @extends {Application}
*/
class ClientApplication extends Application {
constructor(client, data) {
super(client, data);
/**
* The application command manager for this application
* @type {ApplicationCommandManager}
*/
this.commands = new ApplicationCommandManager(this.client);
}
_patch(data) {
super._patch(data);

View File

@@ -0,0 +1,222 @@
'use strict';
const APIMessage = require('./APIMessage');
const Interaction = require('./Interaction');
const WebhookClient = require('../client/WebhookClient');
const { Error } = require('../errors');
const { ApplicationCommandOptionTypes, InteractionResponseTypes } = require('../util/Constants');
const MessageFlags = require('../util/MessageFlags');
/**
* Represents a command interaction.
* @extends {Interaction}
*/
class CommandInteraction extends Interaction {
constructor(client, data) {
super(client, data);
/**
* The ID of the invoked application command
* @type {Snowflake}
*/
this.commandID = data.data.id;
/**
* The name of the invoked application command
* @type {string}
*/
this.commandName = data.data.name;
/**
* Whether the reply to this interaction has been deferred
* @type {boolean}
*/
this.deferred = false;
/**
* The options passed to the command.
* @type {CommandInteractionOption[]}
*/
this.options = data.data.options?.map(o => this.transformOption(o, data.data.resolved)) ?? [];
/**
* Whether this interaction has already been replied to
* @type {boolean}
*/
this.replied = false;
/**
* An associated webhook client, can be used to create deferred replies
* @type {WebhookClient}
*/
this.webhook = new WebhookClient(this.applicationID, this.token, this.client.options);
}
/**
* The invoked application command, if it was fetched before
* @type {?ApplicationCommand}
*/
get command() {
const id = this.commandID;
return this.guild?.commands.cache.get(id) ?? this.client.application.commands.cache.get(id) ?? null;
}
/**
* Defers the reply to this interaction.
* @param {boolean} [ephemeral] Whether the reply should be ephemeral
* @returns {Promise<void>}
* @example
* // Defer the reply to this interaction
* interaction.defer()
* .then(console.log)
* .catch(console.error)
* @example
* // Defer to send an ephemeral reply later
* interaction.defer(true)
* .then(console.log)
* .catch(console.error);
*/
async defer(ephemeral) {
if (this.deferred || this.replied) throw new Error('INTERACTION_ALREADY_REPLIED');
await this.client.api.interactions(this.id, this.token).callback.post({
data: {
type: InteractionResponseTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE,
data: {
flags: ephemeral ? MessageFlags.FLAGS.EPHEMERAL : undefined,
},
},
});
this.deferred = true;
}
/**
* Options for a reply to an interaction.
* @typedef {WebhookMessageOptions} InteractionReplyOptions
* @property {boolean} [ephemeral] Whether the reply should be ephemeral
*/
/**
* Creates a reply to this interaction.
* @param {string|APIMessage|MessageAdditions} content The content for the reply
* @param {InteractionReplyOptions} [options] Additional options for the reply
* @returns {Promise<void>}
* @example
* // Reply to the interaction with an embed
* const embed = new MessageEmbed().setDescription('Pong!');
*
* interaction.reply(embed)
* .then(console.log)
* .catch(console.error);
* @example
* // Create an ephemeral reply
* interaction.reply('Pong!', { ephemeral: true })
* .then(console.log)
* .catch(console.error);
*/
async reply(content, options) {
if (this.deferred || this.replied) throw new Error('INTERACTION_ALREADY_REPLIED');
const apiMessage = content instanceof APIMessage ? content : APIMessage.create(this, content, options);
const { data, files } = await apiMessage.resolveData().resolveFiles();
await this.client.api.interactions(this.id, this.token).callback.post({
data: {
type: InteractionResponseTypes.CHANNEL_MESSAGE_WITH_SOURCE,
data,
},
files,
});
this.replied = true;
}
/**
* Fetches the initial reply to this interaction.
* @see Webhook#fetchMessage
* @returns {Promise<Message|Object>}
* @example
* // Fetch the reply to this interaction
* interaction.fetchReply()
* .then(reply => console.log(`Replied with ${reply.content}`))
* .catch(console.error);
*/
async fetchReply() {
const raw = await this.webhook.fetchMessage('@original');
return this.channel?.messages.add(raw) ?? raw;
}
/**
* Edits the initial reply to this interaction.
* @see Webhook#editMessage
* @param {string|APIMessage|MessageEmbed|MessageEmbed[]} content The new content for the message
* @param {WebhookEditMessageOptions} [options] The options to provide
* @returns {Promise<Message|Object>}
* @example
* // Edit the reply to this interaction
* interaction.editReply('New content')
* .then(console.log)
* .catch(console.error);
*/
async editReply(content, options) {
const raw = await this.webhook.editMessage('@original', content, options);
return this.channel?.messages.add(raw) ?? raw;
}
/**
* Deletes the initial reply to this interaction.
* @see Webhook#deleteMessage
* @returns {Promise<void>}
* @example
* // Delete the reply to this interaction
* interaction.deleteReply()
* .then(console.log)
* .catch(console.error);
*/
async deleteReply() {
await this.webhook.deleteMessage('@original');
}
/**
* Represents an option of a received command interaction.
* @typedef {Object} CommandInteractionOption
* @property {string} name The name of the option
* @property {ApplicationCommandOptionType} type The type of the option
* @property {string|number|boolean} [value] The value of the option
* @property {CommandInteractionOption[]} [options] Additional options if this option is a subcommand (group)
* @property {User} [user] The resolved user
* @property {GuildMember|Object} [member] The resolved member
* @property {GuildChannel|Object} [channel] The resolved channel
* @property {Role|Object} [role] The resolved role
*/
/**
* Transforms an option received from the API.
* @param {Object} option The received option
* @param {Object} resolved The resolved interaction data
* @returns {CommandInteractionOption}
* @private
*/
transformOption(option, resolved) {
const result = {
name: option.name,
type: ApplicationCommandOptionTypes[option.type],
};
if ('value' in option) result.value = option.value;
if ('options' in option) result.options = option.options.map(o => this.transformOption(o, resolved));
const user = resolved?.users?.[option.value];
if (user) result.user = this.client.users.add(user);
const member = resolved?.members?.[option.value];
if (member) result.member = this.guild?.members.add({ user, ...member }) ?? member;
const channel = resolved?.channels?.[option.value];
if (channel) result.channel = this.client.channels.add(channel, this.guild) ?? channel;
const role = resolved?.roles?.[option.value];
if (role) result.role = this.guild?.roles.add(role) ?? role;
return result;
}
}
module.exports = CommandInteraction;

View File

@@ -9,6 +9,7 @@ const Invite = require('./Invite');
const VoiceRegion = require('./VoiceRegion');
const Webhook = require('./Webhook');
const { Error, TypeError } = require('../errors');
const GuildApplicationCommandManager = require('../managers/GuildApplicationCommandManager');
const GuildChannelManager = require('../managers/GuildChannelManager');
const GuildEmojiManager = require('../managers/GuildEmojiManager');
const GuildMemberManager = require('../managers/GuildMemberManager');
@@ -42,6 +43,12 @@ class Guild extends Base {
constructor(client, data) {
super(client);
/**
* A manager of the application commands belonging to this guild
* @type {GuildApplicationCommandManager}
*/
this.commands = new GuildApplicationCommandManager(this);
/**
* A manager of the members belonging to this guild
* @type {GuildMemberManager}

View File

@@ -0,0 +1,117 @@
'use strict';
const Base = require('./Base');
const { InteractionTypes } = require('../util/Constants');
const SnowflakeUtil = require('../util/SnowflakeUtil');
/**
* Represents an interaction.
* @extends {Base}
*/
class Interaction extends Base {
constructor(client, data) {
super(client);
/**
* The type of this interaction
* @type {InteractionType}
*/
this.type = InteractionTypes[data.type];
/**
* The ID of this interaction
* @type {Snowflake}
*/
this.id = data.id;
/**
* The token of this interaction
* @type {string}
* @name Interaction#token
* @readonly
*/
Object.defineProperty(this, 'token', { value: data.token });
/**
* The ID of the application
* @type {Snowflake}
*/
this.applicationID = data.application_id;
/**
* The ID of the channel this interaction was sent in
* @type {?Snowflake}
*/
this.channelID = data.channel_id ?? null;
/**
* The ID of the guild this interaction was sent in
* @type {?Snowflake}
*/
this.guildID = data.guild_id ?? null;
/**
* The user which sent this interaction
* @type {User}
*/
this.user = this.client.users.add(data.user ?? data.member.user);
/**
* If this interaction was sent in a guild, the member which sent it
* @type {?GuildMember|Object}
*/
this.member = data.member ? this.guild?.members.add(data.member) ?? data.member : null;
/**
* The version
* @type {number}
*/
this.version = data.version;
}
/**
* The timestamp the interaction was created at
* @type {number}
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
}
/**
* The time the interaction was created at
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}
/**
* The channel this interaction was sent in
* @type {?Channel}
* @readonly
*/
get channel() {
return this.client.channels.cache.get(this.channelID) ?? null;
}
/**
* The guild this interaction was sent in
* @type {?Guild}
* @readonly
*/
get guild() {
return this.client.guilds.cache.get(this.guildID) ?? null;
}
/**
* Indicates whether this interaction is a command interaction.
* @returns {boolean}
*/
isCommand() {
return InteractionTypes[this.type] === InteractionTypes.APPLICATION_COMMAND;
}
}
module.exports = Interaction;

View File

@@ -11,7 +11,7 @@ const Sticker = require('./Sticker');
const { Error, TypeError } = require('../errors');
const ReactionManager = require('../managers/ReactionManager');
const Collection = require('../util/Collection');
const { MessageTypes, SystemMessageTypes } = require('../util/Constants');
const { InteractionTypes, MessageTypes, SystemMessageTypes } = require('../util/Constants');
const MessageFlags = require('../util/MessageFlags');
const Permissions = require('../util/Permissions');
const SnowflakeUtil = require('../util/SnowflakeUtil');
@@ -232,6 +232,30 @@ class Message extends Base {
if (data.referenced_message) {
this.channel.messages.add(data.referenced_message);
}
/**
* Partial data of the interaction that a message is a reply to
* @typedef {object} MessageInteraction
* @property {Snowflake} id The ID of the interaction
* @property {InteractionType} type The type of the interaction
* @property {string} commandName The name of the interaction's application command
* @property {User} user The user that invoked the interaction
*/
if (data.interaction) {
/**
* Partial data of the interaction that this message is a reply to
* @type {?MessageInteraction}
*/
this.interaction = {
id: data.interaction.id,
type: InteractionTypes[data.interaction.type],
commandName: data.interaction.name,
user: this.client.users.add(data.interaction.user),
};
} else if (!this.interaction) {
this.interaction = null;
}
}
/**

View File

@@ -238,7 +238,7 @@ class Webhook {
/**
* Gets a message that was sent by this webhook.
* @param {Snowflake} message The ID of the message to fetch
* @param {Snowflake|'@original'} message The ID of the message to fetch
* @param {boolean} [cache=true] Whether to cache the message
* @returns {Promise<Message|Object>} Returns the raw message data if the webhook was instantiated as a
* {@link WebhookClient} or if the channel is uncached, otherwise a {@link Message} will be returned
@@ -250,7 +250,7 @@ class Webhook {
/**
* Edits a message that was sent by this webhook.
* @param {MessageResolvable} message The message to edit
* @param {MessageResolvable|'@original'} message The message to edit
* @param {StringResolvable|APIMessage} [content] The new content for the message
* @param {WebhookEditMessageOptions|MessageEmbed|MessageEmbed[]} [options] The options to provide
* @returns {Promise<Message|Object>} Returns the raw message data if the webhook was instantiated as a
@@ -287,7 +287,7 @@ class Webhook {
/**
* Delete a message that was sent by this webhook.
* @param {MessageResolvable} message The message to delete
* @param {MessageResolvable|'@original'} message The message to delete
* @returns {Promise<void>}
*/
async deleteMessage(message) {

View File

@@ -271,6 +271,7 @@ exports.Events = {
TYPING_START: 'typingStart',
TYPING_STOP: 'typingStop',
WEBHOOKS_UPDATE: 'webhookUpdate',
INTERACTION_CREATE: 'interaction',
ERROR: 'error',
WARN: 'warn',
DEBUG: 'debug',
@@ -343,6 +344,7 @@ exports.PartialTypes = keyMirror(['USER', 'CHANNEL', 'GUILD_MEMBER', 'MESSAGE',
* * VOICE_STATE_UPDATE
* * VOICE_SERVER_UPDATE
* * WEBHOOKS_UPDATE
* * INTERACTION_CREATE
* @typedef {string} WSEventType
*/
exports.WSEvents = keyMirror([
@@ -382,6 +384,7 @@ exports.WSEvents = keyMirror([
'VOICE_STATE_UPDATE',
'VOICE_SERVER_UPDATE',
'WEBHOOKS_UPDATE',
'INTERACTION_CREATE',
]);
/**
@@ -434,6 +437,7 @@ exports.InviteScopes = [
* * GUILD_DISCOVERY_GRACE_PERIOD_INITIAL_WARNING
* * GUILD_DISCOVERY_GRACE_PERIOD_FINAL_WARNING
* * REPLY
* * APPLICATION_COMMAND
* @typedef {string} MessageType
*/
exports.MessageTypes = [
@@ -457,15 +461,19 @@ exports.MessageTypes = [
'GUILD_DISCOVERY_GRACE_PERIOD_FINAL_WARNING',
null,
'REPLY',
'APPLICATION_COMMAND',
];
/**
* The types of messages that are `System`. The available types are `MessageTypes` excluding:
* * DEFAULT
* * REPLY
* * APPLICATION_COMMAND
* @typedef {string} SystemMessageType
*/
exports.SystemMessageTypes = exports.MessageTypes.filter(type => type && type !== 'DEFAULT' && type !== 'REPLY');
exports.SystemMessageTypes = exports.MessageTypes.filter(
type => type && !['DEFAULT', 'REPLY', 'APPLICATION_COMMAND'].includes(type),
);
/**
* <info>Bots cannot set a `CUSTOM_STATUS`, it is only for custom statuses received from users</info>
@@ -742,6 +750,64 @@ exports.StickerFormatTypes = createEnum([null, 'PNG', 'APNG', 'LOTTIE']);
*/
exports.OverwriteTypes = createEnum(['role', 'member']);
/**
* The type of an {@link ApplicationCommandOption} object:
* * SUB_COMMAND
* * SUB_COMMAND_GROUP
* * STRING
* * INTEGER
* * BOOLEAN
* * USER
* * CHANNEL
* * ROLE
* * MENTIONABLE
* @typedef {string} ApplicationCommandOptionType
*/
exports.ApplicationCommandOptionTypes = createEnum([
null,
'SUB_COMMAND',
'SUB_COMMAND_GROUP',
'STRING',
'INTEGER',
'BOOLEAN',
'USER',
'CHANNEL',
'ROLE',
'MENTIONABLE',
]);
/**
* The type of an {@link ApplicationCommandPermissions} object:
* * ROLE
* * USER
* @typedef {string} ApplicationCommandPermissionType
*/
exports.ApplicationCommandPermissionTypes = createEnum([null, 'ROLE', 'USER']);
/**
* The type of an {@link Interaction} object:
* * PING
* * APPLICATION_COMMAND
* @typedef {string} InteractionType
*/
exports.InteractionTypes = createEnum([null, 'PING', 'APPLICATION_COMMAND']);
/**
* The type of an interaction response:
* * PONG
* * CHANNEL_MESSAGE_WITH_SOURCE
* * DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
* @typedef {string} InteractionResponseType
*/
exports.InteractionResponseTypes = createEnum([
null,
'PONG',
null,
null,
'CHANNEL_MESSAGE_WITH_SOURCE',
'DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE',
]);
function keyMirror(arr) {
let tmp = Object.create(null);
for (const value of arr) tmp[value] = value;

View File

@@ -28,6 +28,8 @@ class MessageFlags extends BitField {}
* * `SUPPRESS_EMBEDS`
* * `SOURCE_MESSAGE_DELETED`
* * `URGENT`
* * `EPHEMERAL`
* * `LOADING`
* @type {Object}
* @see {@link https://discord.com/developers/docs/resources/channel#message-object-message-flags}
*/
@@ -37,6 +39,8 @@ MessageFlags.FLAGS = {
SUPPRESS_EMBEDS: 1 << 2,
SOURCE_MESSAGE_DELETED: 1 << 3,
URGENT: 1 << 4,
EPHEMERAL: 1 << 6,
LOADING: 1 << 7,
};
module.exports = MessageFlags;

View File

@@ -78,6 +78,7 @@ class Permissions extends BitField {
* * `MANAGE_ROLES`
* * `MANAGE_WEBHOOKS`
* * `MANAGE_EMOJIS`
* * `USE_APPLICATION_COMMANDS`
* * `REQUEST_TO_SPEAK`
* @type {Object<string, bigint>}
* @see {@link https://discord.com/developers/docs/topics/permissions}
@@ -114,6 +115,7 @@ Permissions.FLAGS = {
MANAGE_ROLES: 1n << 28n,
MANAGE_WEBHOOKS: 1n << 29n,
MANAGE_EMOJIS: 1n << 30n,
USE_APPLICATION_COMMANDS: 1n << 31n,
REQUEST_TO_SPEAK: 1n << 32n,
};

View File

@@ -19,6 +19,7 @@
* * **`VoiceState`**
* * **`Role`**
* * **`User`**
* * **`CommandInteraction`**
* @typedef {string} ExtendableStructure
*/
@@ -109,6 +110,7 @@ const structures = {
VoiceState: require('../structures/VoiceState'),
Role: require('../structures/Role'),
User: require('../structures/User'),
CommandInteraction: require('../structures/CommandInteraction'),
};
module.exports = Structures;