2022-03-19 10:37:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const Base = require('./Base');
|
2022-03-24 10:55:32 +00:00
|
|
|
const { InteractionTypes, MessageComponentTypes, ApplicationCommandTypes } = require('../util/Constants');
|
|
|
|
const Permissions = require('../util/Permissions');
|
|
|
|
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
2022-03-19 10:37:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents an interaction.
|
|
|
|
* @extends {Base}
|
|
|
|
*/
|
|
|
|
class Interaction extends Base {
|
|
|
|
constructor(client, data) {
|
|
|
|
super(client);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The interaction's type
|
|
|
|
* @type {InteractionType}
|
|
|
|
*/
|
2022-03-24 10:55:32 +00:00
|
|
|
this.type = InteractionTypes[data.type];
|
2022-03-19 10:37:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The interaction's id
|
|
|
|
* @type {Snowflake}
|
|
|
|
*/
|
|
|
|
this.id = data.id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The interaction's token
|
|
|
|
* @type {string}
|
|
|
|
* @name Interaction#token
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
Object.defineProperty(this, 'token', { value: data.token });
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The application's id
|
|
|
|
* @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|APIGuildMember)}
|
|
|
|
*/
|
|
|
|
this.member = data.member ? this.guild?.members._add(data.member) ?? data.member : null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The version
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.version = data.version;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The permissions of the member, if one exists, in the channel this interaction was executed in
|
2022-03-24 10:55:32 +00:00
|
|
|
* @type {?Readonly<Permissions>}
|
2022-03-19 10:37:45 +00:00
|
|
|
*/
|
2022-03-24 10:55:32 +00:00
|
|
|
this.memberPermissions = data.member?.permissions ? new Permissions(data.member.permissions).freeze() : null;
|
2022-03-19 10:37:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The locale of the user who invoked this interaction
|
|
|
|
* @type {string}
|
2022-03-24 10:55:32 +00:00
|
|
|
* @see {@link https://discord.com/developers/docs/dispatch/field-values#predefined-field-values-accepted-locales}
|
2022-03-19 10:37:45 +00:00
|
|
|
*/
|
|
|
|
this.locale = data.locale;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The preferred locale from the guild this interaction was sent in
|
|
|
|
* @type {?string}
|
|
|
|
*/
|
|
|
|
this.guildLocale = data.guild_locale ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The timestamp the interaction was created at
|
|
|
|
* @type {number}
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
get createdTimestamp() {
|
2022-03-24 10:55:32 +00:00
|
|
|
return SnowflakeUtil.timestampFrom(this.id);
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {?TextBasedChannels}
|
|
|
|
* @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 received from a guild.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
inGuild() {
|
|
|
|
return Boolean(this.guildId && this.member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether or not this interaction is both cached and received from a guild.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
inCachedGuild() {
|
|
|
|
return Boolean(this.guild && this.member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether or not this interaction is received from an uncached guild.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
inRawGuild() {
|
|
|
|
return Boolean(this.guildId && !this.guild && this.member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-24 10:55:32 +00:00
|
|
|
* Indicates whether this interaction is a {@link BaseCommandInteraction}.
|
2022-03-19 10:37:45 +00:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2022-03-24 10:55:32 +00:00
|
|
|
isApplicationCommand() {
|
|
|
|
return InteractionTypes[this.type] === InteractionTypes.APPLICATION_COMMAND;
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-24 10:55:32 +00:00
|
|
|
* Indicates whether this interaction is a {@link CommandInteraction}.
|
2022-03-19 10:37:45 +00:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2022-03-24 10:55:32 +00:00
|
|
|
isCommand() {
|
|
|
|
return InteractionTypes[this.type] === InteractionTypes.APPLICATION_COMMAND && typeof this.targetId === 'undefined';
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-24 10:55:32 +00:00
|
|
|
* Indicates whether this interaction is a {@link ContextMenuInteraction}
|
2022-03-19 10:37:45 +00:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2022-03-24 10:55:32 +00:00
|
|
|
isContextMenu() {
|
|
|
|
return InteractionTypes[this.type] === InteractionTypes.APPLICATION_COMMAND && typeof this.targetId !== 'undefined';
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-24 10:55:32 +00:00
|
|
|
* Indicates whether this interaction is a {@link UserContextMenuInteraction}
|
2022-03-19 10:37:45 +00:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2022-03-24 10:55:32 +00:00
|
|
|
isUserContextMenu() {
|
|
|
|
return this.isContextMenu() && ApplicationCommandTypes[this.targetType] === ApplicationCommandTypes.USER;
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-24 10:55:32 +00:00
|
|
|
* Indicates whether this interaction is a {@link MessageContextMenuInteraction}
|
2022-03-19 10:37:45 +00:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2022-03-24 10:55:32 +00:00
|
|
|
isMessageContextMenu() {
|
|
|
|
return this.isContextMenu() && ApplicationCommandTypes[this.targetType] === ApplicationCommandTypes.MESSAGE;
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether this interaction is an {@link AutocompleteInteraction}
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isAutocomplete() {
|
2022-03-24 10:55:32 +00:00
|
|
|
return InteractionTypes[this.type] === InteractionTypes.APPLICATION_COMMAND_AUTOCOMPLETE;
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether this interaction is a {@link MessageComponentInteraction}.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isMessageComponent() {
|
2022-03-24 10:55:32 +00:00
|
|
|
return InteractionTypes[this.type] === InteractionTypes.MESSAGE_COMPONENT;
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether this interaction is a {@link ButtonInteraction}.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isButton() {
|
2022-03-24 10:55:32 +00:00
|
|
|
return (
|
|
|
|
InteractionTypes[this.type] === InteractionTypes.MESSAGE_COMPONENT &&
|
|
|
|
MessageComponentTypes[this.componentType] === MessageComponentTypes.BUTTON
|
|
|
|
);
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether this interaction is a {@link SelectMenuInteraction}.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isSelectMenu() {
|
2022-03-24 10:55:32 +00:00
|
|
|
return (
|
|
|
|
InteractionTypes[this.type] === InteractionTypes.MESSAGE_COMPONENT &&
|
|
|
|
MessageComponentTypes[this.componentType] === MessageComponentTypes.SELECT_MENU
|
|
|
|
);
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Interaction;
|