Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 messages
monbrey marked this conversation as resolved.
Show resolved Hide resolved
monbrey marked this conversation as resolved.
Show resolved Hide resolved
* @property {Collection<string, Channel|APIChannel>} channels The resolved channels
* @property {Collection<string, Message|APIMessage>} messages The resolved messages
monbrey marked this conversation as resolved.
Show resolved Hide resolved
*/

/**
* 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);
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
}
}

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),
monbrey marked this conversation as resolved.
Show resolved Hide resolved
monbrey marked this conversation as resolved.
Show resolved Hide 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) });
vladfrangu marked this conversation as resolved.
Show resolved Hide 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 @@ -265,6 +265,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 @@ -529,6 +530,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 @@ -3255,6 +3257,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, GuildChannel | APIInteractionDataResolvedChannel>;
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
monbrey marked this conversation as resolved.
Show resolved Hide resolved
messages?: Collection<string, Message | APIMessage>;
}

export interface ConstantsClientApplicationAssetTypes {
SMALL: 1;
BIG: 2;
Expand Down