Files
TTtie c11ee37b06 chore: add missing guild ID hints to internal getChannel() usages (#254)
Adds guild ID hints to `getChannel()` usages inside the library that
have been left out.
2026-07-21 11:19:53 +02:00

222 lines
7.6 KiB
JavaScript

"use strict";
const Base = require("./Base");
const Entitlement = require("./Entitlement");
const {InteractionTypes} = require("../Constants");
const Member = require("./Member");
const Permission = require("./Permission");
/**
* Represents an interaction. You also probably want to look at AutocompleteInteraction, CommandInteraction, ComponentInteraction, and ModalSubmitInteraction.
* @extends Base
*/
class Interaction extends Base {
/**
* The ID of the interaction
* @member {String} Interaction#id
*/
#client;
constructor(data, client) {
super(data.id);
this.#client = client;
/**
* The ID of the interaction's application
* @type {String}
*/
this.applicationID = data.application_id;
/**
* The interaction token (Interaction tokens are valid for 15 minutes after initial response and can be used to send followup messages but you must send an initial response within 3 seconds of receiving the event. If the 3 second deadline is exceeded, the token will be invalidated.)
* @type {String}
*/
this.token = data.token;
/**
* The [interaction type](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-type)
* @type {Number}
*/
this.type = data.type;
/**
* The interaction version
* @type {Number}
*/
this.version = data.version;
/**
* Whether or not the interaction has been acknowledged
* @type {Boolean}
*/
this.acknowledged = false;
if(data.channel !== undefined) {
/**
* The channel the interaction was created in. Can be partial with only the id and type if the channel is not cached.
* @type {(PrivateChannel | TextChannel | NewsChannel)?}
*/
this.channel = this.#client.getChannel(data.channel.id, data.guild?.id) || data.channel;
}
if(data.data !== undefined) {
/**
* The data attached to the interaction. See AutocompleteInteraction, CommandInteraction, and ComponentInteraction for specific details
* @type {Object}
*/
this.data = JSON.parse(JSON.stringify(data.data));
}
if(data.guild !== undefined) {
const guild = client.guilds.get(data.guild.id);
if(guild) {
// for some reason, Discord sends the preferred guild locale under the `locale` key
data.guild.preferred_locale ??= data.guild.locale;
guild.update(data.guild);
/**
* The guild the interaction was created in. Can be partial only with the `id`, `locale`, and `features`.
* @type {Guild?}
*/
this.guild = guild;
} else {
this.guild = data.guild;
}
/**
* The ID of the guild in which the interaction was created
* @deprecated Use the `id` from {@link Interaction#guild} instead
* @type {String?}
*/
this.guildID = data.guild.id;
/**
* The selected language of the guild the command was invoked from (e.g. "en-US")
* @deprecated Use the `preferredLocale` from {@link Interaction#guild} instead
* @type {String?}
*/
this.guildLocale = data.guild.locale;
}
if(data.member !== undefined) {
data.member.id = data.member.user.id;
if(this.channel.guild) {
/**
* The member who triggered the interaction (This is only sent when the interaction is invoked within a guild)
* @type {Member?}
*/
this.member = this.channel.guild.members.update(data.member, this.channel.guild);
} else {
const guild = this.#client.guilds.get(data.guild.id);
this.member = new Member(data.member, guild, this.#client);
}
this.user = this.member.user;
}
if(data.user !== undefined) {
/**
* The user who triggered the interaction
* @type {User}
*/
this.user = this.#client.users.update(data.user, client);
}
if(data.locale !== undefined) {
/**
* The selected language of the invoking user (e.g. "en-US")
* @type {String?}
*/
this.locale = data.locale;
}
if(data.app_permissions !== undefined) {
/**
* The permissions the app has in the source context of the interaction.
* @type {Permission?}
*/
this.appPermissions = new Permission(data.app_permissions);
}
if(data.entitlements !== undefined) {
/**
* The entitlements of the user invoking the interaction
* @type {Array<Entitlement>}
*/
this.entitlements = data.entitlements.map((entitlement) => new Entitlement(entitlement, client));
}
if(data.context !== undefined) {
/**
* The context from which this interaction was triggered
* @type {Number?}
*/
this.context = data.context;
}
if(data.authorizing_integration_owners !== undefined) {
/**
* A mapping of installation contexts that the app was authorized for to respective guild/user IDs
* @type {Object<number, string>}
*/
this.authorizingIntegrationOwners = data.authorizing_integration_owners;
}
/**
* The attachment size limit of the interaction
* @type {Number}
*/
this.attachmentSizeLimit = data.attachment_size_limit;
}
update() {
this.acknowledged = true;
}
static from(data, client) {
switch(data.type) {
case InteractionTypes.PING: {
return new PingInteraction(data, client);
}
case InteractionTypes.APPLICATION_COMMAND: {
return new CommandInteraction(data, client);
}
case InteractionTypes.MESSAGE_COMPONENT: {
return new ComponentInteraction(data, client);
}
case InteractionTypes.APPLICATION_COMMAND_AUTOCOMPLETE: {
return new AutocompleteInteraction(data, client);
}
case InteractionTypes.MODAL_SUBMIT: {
return new ModalSubmitInteraction(data, client);
}
}
client.emit("warn", new Error(`Unknown interaction type: ${data.type}\n${JSON.stringify(data, null, 2)}`));
return new Interaction(data, client);
}
toJSON(props = []) {
return super.toJSON([
"acknowledged",
"applicationID",
"appPermissions",
"authorizingIntegrationOwners",
"channel",
"context",
"data",
"entitlements",
"guild",
"locale",
"token",
"type",
"version",
...props
]);
}
}
module.exports = Interaction;
// Circular import
const CommandInteraction = require("./CommandInteraction");
const ComponentInteraction = require("./ComponentInteraction");
const AutocompleteInteraction = require("./AutocompleteInteraction");
const ModalSubmitInteraction = require("./ModalSubmitInteraction.js");
const PingInteraction = require("./PingInteraction.js");