Skip to content

Commit

Permalink
feat(CommandInteractionResolvedData): access to "raw" resolved data (#…
Browse files Browse the repository at this point in the history
…6384)

Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
  • Loading branch information
monbrey and NotSugden committed Aug 12, 2021
1 parent d9456a1 commit fff887b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
59 changes: 59 additions & 0 deletions 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');
Expand Down Expand Up @@ -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<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.
* @typedef {Object} CommandInteractionOption
Expand Down
1 change: 1 addition & 0 deletions src/structures/CommandInteraction.js
Expand Up @@ -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 ?? {}),
);
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/structures/CommandInteractionOptionResolver.js
Expand Up @@ -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
Expand Down Expand Up @@ -55,6 +55,13 @@ class CommandInteractionOptionResolver {
* @readonly
*/
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) });
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/structures/ContextMenuInteraction.js
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions typings/index.d.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<CommandInteractionResolvedData>;
private _group: string | null;
private _hoistedOptions: CommandInteractionOption[];
private _subcommand: string | null;
Expand Down Expand Up @@ -3281,6 +3283,14 @@ export interface CommandInteractionOption {
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 {
SMALL: 1;
BIG: 2;
Expand Down

0 comments on commit fff887b

Please sign in to comment.