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

@@ -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) {