From fff887b2f43d19164dbf2878b00abead90a0703f Mon Sep 17 00:00:00 2001 From: monbrey Date: Fri, 13 Aug 2021 00:04:40 +1000 Subject: [PATCH] feat(CommandInteractionResolvedData): access to "raw" resolved data (#6384) Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com> --- src/structures/BaseCommandInteraction.js | 59 +++++++++++++++++++ src/structures/CommandInteraction.js | 1 + .../CommandInteractionOptionResolver.js | 9 ++- src/structures/ContextMenuInteraction.js | 6 +- typings/index.d.ts | 10 ++++ 5 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/structures/BaseCommandInteraction.js b/src/structures/BaseCommandInteraction.js index 0eb331fb91eb..2070c3460c92 100644 --- a/src/structures/BaseCommandInteraction.js +++ b/src/structures/BaseCommandInteraction.js @@ -1,5 +1,6 @@ 'use strict'; +const { Collection } = require('@discordjs/collection'); const Interaction = require('./Interaction'); const InteractionWebhook = require('./InteractionWebhook'); const InteractionResponses = require('./interfaces/InteractionResponses'); @@ -74,6 +75,64 @@ class BaseCommandInteraction extends Interaction { return this.guild?.commands.cache.get(id) ?? this.client.application.commands.cache.get(id) ?? null; } + /** + * Represents the resolved data of a received command interaction. + * @typedef {Object} CommandInteractionResolvedData + * @property {Collection} [users] The resolved users + * @property {Collection} [members] The resolved guild members + * @property {Collection} [roles] The resolved roles + * @property {Collection} [channels] The resolved channels + * @property {Collection} [messages] The resolved messages + */ + + /** + * Transforms the resolved received from the API. + * @param {APIInteractionDataResolved} resolved The received resolved objects + * @returns {CommandInteractionResolvedData} + * @private + */ + transformResolved({ members, users, channels, roles, messages }) { + const result = {}; + + if (members) { + result.members = new Collection(); + for (const [id, member] of Object.entries(members)) { + const user = users[id]; + result.members.set(id, this.guild?.members._add({ user, ...member }) ?? member); + } + } + + if (users) { + result.users = new Collection(); + for (const user of Object.values(users)) { + result.users.set(user.id, this.client.users._add(user)); + } + } + + if (roles) { + result.roles = new Collection(); + for (const role of Object.values(roles)) { + result.roles.set(role.id, this.guild?.roles._add(role) ?? role); + } + } + + if (channels) { + result.channels = new Collection(); + for (const channel of Object.values(channels)) { + result.channels.set(channel.id, this.client.channels._add(channel, this.guild) ?? channel); + } + } + + if (messages) { + result.messages = new Collection(); + for (const message of Object.values(messages)) { + result.messages.set(message.id, this.channel?.messages?._add(message) ?? message); + } + } + + return result; + } + /** * Represents an option of a received command interaction. * @typedef {Object} CommandInteractionOption diff --git a/src/structures/CommandInteraction.js b/src/structures/CommandInteraction.js index 10693e65f9e8..4c8bd5d7535a 100644 --- a/src/structures/CommandInteraction.js +++ b/src/structures/CommandInteraction.js @@ -18,6 +18,7 @@ class CommandInteraction extends BaseCommandInteraction { this.options = new CommandInteractionOptionResolver( this.client, data.data.options?.map(option => this.transformOption(option, data.data.resolved)) ?? [], + this.transformResolved(data.data.resolved ?? {}), ); } } diff --git a/src/structures/CommandInteractionOptionResolver.js b/src/structures/CommandInteractionOptionResolver.js index 011f1cbd2628..a5c01e6a8a12 100644 --- a/src/structures/CommandInteractionOptionResolver.js +++ b/src/structures/CommandInteractionOptionResolver.js @@ -6,7 +6,7 @@ const { TypeError } = require('../errors'); * A resolver for command interaction options. */ class CommandInteractionOptionResolver { - constructor(client, options) { + constructor(client, options, resolved) { /** * The client that instantiated this. * @name CommandInteractionOptionResolver#client @@ -55,6 +55,13 @@ class CommandInteractionOptionResolver { * @readonly */ Object.defineProperty(this, 'data', { value: Object.freeze([...options]) }); + + /** + * The interaction resolved data + * @name CommandInteractionOptionResolver#resolved + * @type {Readonly} + */ + Object.defineProperty(this, 'resolved', { value: Object.freeze(resolved) }); } /** diff --git a/src/structures/ContextMenuInteraction.js b/src/structures/ContextMenuInteraction.js index 8754eda1cb81..f4f6ef47d274 100644 --- a/src/structures/ContextMenuInteraction.js +++ b/src/structures/ContextMenuInteraction.js @@ -15,7 +15,11 @@ class ContextMenuInteraction extends BaseCommandInteraction { * The target of the interaction, parsed into options * @type {CommandInteractionOptionResolver} */ - this.options = new CommandInteractionOptionResolver(this.client, this.resolveContextMenuOptions(data.data)); + this.options = new CommandInteractionOptionResolver( + this.client, + this.resolveContextMenuOptions(data.data), + this.transformResolved(data.data.resolved), + ); /** * The id of the target of the interaction diff --git a/typings/index.d.ts b/typings/index.d.ts index a5ca9b5aa335..b253db23960d 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -270,6 +270,7 @@ export abstract class BaseCommandInteraction extends Interaction { option: APIApplicationCommandOption, resolved: APIApplicationCommandInteractionData['resolved'], ): CommandInteractionOption; + private transformResolved(resolved: APIApplicationCommandInteractionData['resolved']): CommandInteractionResolvedData; } export abstract class BaseGuild extends Base { @@ -534,6 +535,7 @@ export class CommandInteractionOptionResolver { public constructor(client: Client, options: CommandInteractionOption[]); public readonly client: Client; public readonly data: readonly CommandInteractionOption[]; + public readonly resolved: Readonly; private _group: string | null; private _hoistedOptions: CommandInteractionOption[]; private _subcommand: string | null; @@ -3281,6 +3283,14 @@ export interface CommandInteractionOption { message?: Message | APIMessage; } +export interface CommandInteractionResolvedData { + users?: Collection; + members?: Collection; + roles?: Collection; + channels?: Collection; + messages?: Collection; +} + export interface ConstantsClientApplicationAssetTypes { SMALL: 1; BIG: 2;