mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
* refactor: change `xID` to `xId` * Update src/managers/MessageManager.js Co-authored-by: Noel <buechler.noel@outlook.com> Co-authored-by: Noel <buechler.noel@outlook.com>
156 lines
3.5 KiB
JavaScript
156 lines
3.5 KiB
JavaScript
'use strict';
|
|
|
|
const Base = require('./Base');
|
|
const { InteractionTypes, MessageComponentTypes } = require('../util/Constants');
|
|
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
|
|
|
/**
|
|
* Represents an interaction.
|
|
* @extends {Base}
|
|
*/
|
|
class Interaction extends Base {
|
|
constructor(client, data) {
|
|
super(client);
|
|
|
|
/**
|
|
* The interaction's type
|
|
* @type {InteractionType}
|
|
*/
|
|
this.type = InteractionTypes[data.type];
|
|
|
|
/**
|
|
* 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 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 received from a guild.
|
|
* @returns {boolean}
|
|
*/
|
|
inGuild() {
|
|
return Boolean(this.guildId && this.member);
|
|
}
|
|
|
|
/**
|
|
* Indicates whether this interaction is a {@link CommandInteraction}.
|
|
* @returns {boolean}
|
|
*/
|
|
isCommand() {
|
|
return InteractionTypes[this.type] === InteractionTypes.APPLICATION_COMMAND;
|
|
}
|
|
|
|
/**
|
|
* Indicates whether this interaction is a {@link MessageComponentInteraction}.
|
|
* @returns {boolean}
|
|
*/
|
|
isMessageComponent() {
|
|
return InteractionTypes[this.type] === InteractionTypes.MESSAGE_COMPONENT;
|
|
}
|
|
|
|
/**
|
|
* Indicates whether this interaction is a {@link ButtonInteraction}.
|
|
* @returns {boolean}
|
|
*/
|
|
isButton() {
|
|
return (
|
|
InteractionTypes[this.type] === InteractionTypes.MESSAGE_COMPONENT &&
|
|
MessageComponentTypes[this.componentType] === MessageComponentTypes.BUTTON
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Indicates whether this interaction is a {@link SelectMenuInteraction}.
|
|
* @returns {boolean}
|
|
*/
|
|
isSelectMenu() {
|
|
return (
|
|
InteractionTypes[this.type] === InteractionTypes.MESSAGE_COMPONENT &&
|
|
MessageComponentTypes[this.componentType] === MessageComponentTypes.SELECT_MENU
|
|
);
|
|
}
|
|
}
|
|
|
|
module.exports = Interaction;
|