feat(CommandInteractionResolvedData): access to "raw" resolved data (#6384)

Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
This commit is contained in:
monbrey
2021-08-13 00:04:40 +10:00
committed by GitHub
parent d9456a1a76
commit fff887b2f4
5 changed files with 83 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
'use strict'; 'use strict';
const { Collection } = require('@discordjs/collection');
const Interaction = require('./Interaction'); const Interaction = require('./Interaction');
const InteractionWebhook = require('./InteractionWebhook'); const InteractionWebhook = require('./InteractionWebhook');
const InteractionResponses = require('./interfaces/InteractionResponses'); 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; 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<string, User>} [users] The resolved users
* @property {Collection<string, GuildMember|APIGuildMember>} [members] The resolved guild members
* @property {Collection<string, Role|APIRole>} [roles] The resolved roles
* @property {Collection<string, Channel|APIChannel>} [channels] The resolved channels
* @property {Collection<string, Message|APIMessage>} [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. * Represents an option of a received command interaction.
* @typedef {Object} CommandInteractionOption * @typedef {Object} CommandInteractionOption

View File

@@ -18,6 +18,7 @@ class CommandInteraction extends BaseCommandInteraction {
this.options = new CommandInteractionOptionResolver( this.options = new CommandInteractionOptionResolver(
this.client, this.client,
data.data.options?.map(option => this.transformOption(option, data.data.resolved)) ?? [], data.data.options?.map(option => this.transformOption(option, data.data.resolved)) ?? [],
this.transformResolved(data.data.resolved ?? {}),
); );
} }
} }

View File

@@ -6,7 +6,7 @@ const { TypeError } = require('../errors');
* A resolver for command interaction options. * A resolver for command interaction options.
*/ */
class CommandInteractionOptionResolver { class CommandInteractionOptionResolver {
constructor(client, options) { constructor(client, options, resolved) {
/** /**
* The client that instantiated this. * The client that instantiated this.
* @name CommandInteractionOptionResolver#client * @name CommandInteractionOptionResolver#client
@@ -55,6 +55,13 @@ class CommandInteractionOptionResolver {
* @readonly * @readonly
*/ */
Object.defineProperty(this, 'data', { value: Object.freeze([...options]) }); Object.defineProperty(this, 'data', { value: Object.freeze([...options]) });
/**
* The interaction resolved data
* @name CommandInteractionOptionResolver#resolved
* @type {Readonly<CommandInteractionResolvedData>}
*/
Object.defineProperty(this, 'resolved', { value: Object.freeze(resolved) });
} }
/** /**

View File

@@ -15,7 +15,11 @@ class ContextMenuInteraction extends BaseCommandInteraction {
* The target of the interaction, parsed into options * The target of the interaction, parsed into options
* @type {CommandInteractionOptionResolver} * @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 * The id of the target of the interaction

10
typings/index.d.ts vendored
View File

@@ -270,6 +270,7 @@ export abstract class BaseCommandInteraction extends Interaction {
option: APIApplicationCommandOption, option: APIApplicationCommandOption,
resolved: APIApplicationCommandInteractionData['resolved'], resolved: APIApplicationCommandInteractionData['resolved'],
): CommandInteractionOption; ): CommandInteractionOption;
private transformResolved(resolved: APIApplicationCommandInteractionData['resolved']): CommandInteractionResolvedData;
} }
export abstract class BaseGuild extends Base { export abstract class BaseGuild extends Base {
@@ -534,6 +535,7 @@ export class CommandInteractionOptionResolver {
public constructor(client: Client, options: CommandInteractionOption[]); public constructor(client: Client, options: CommandInteractionOption[]);
public readonly client: Client; public readonly client: Client;
public readonly data: readonly CommandInteractionOption[]; public readonly data: readonly CommandInteractionOption[];
public readonly resolved: Readonly<CommandInteractionResolvedData>;
private _group: string | null; private _group: string | null;
private _hoistedOptions: CommandInteractionOption[]; private _hoistedOptions: CommandInteractionOption[];
private _subcommand: string | null; private _subcommand: string | null;
@@ -3281,6 +3283,14 @@ export interface CommandInteractionOption {
message?: Message | APIMessage; message?: Message | APIMessage;
} }
export interface CommandInteractionResolvedData {
users?: Collection<string, User>;
members?: Collection<string, GuildMember | APIInteractionDataResolvedGuildMember>;
roles?: Collection<string, Role | APIRole>;
channels?: Collection<string, Channel | APIInteractionDataResolvedChannel>;
messages?: Collection<string, Message | APIMessage>;
}
export interface ConstantsClientApplicationAssetTypes { export interface ConstantsClientApplicationAssetTypes {
SMALL: 1; SMALL: 1;
BIG: 2; BIG: 2;