Skip to content

Commit

Permalink
chore: export function again
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite committed Nov 12, 2023
1 parent 9bf989c commit 3195db1
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const CommandInteraction = require('./CommandInteraction');
const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver');
const { transformResolved } = require('../util/transformResolved');
const { transformResolved } = require('../util/Util');

/**
* Represents a command interaction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { lazy } = require('@discordjs/util');
const { ApplicationCommandOptionType } = require('discord-api-types/v10');
const CommandInteraction = require('./CommandInteraction');
const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver');
const { transformResolved } = require('../util/transformResolved');
const { transformResolved } = require('../util/Util');

const getMessage = lazy(() => require('./Message').Message);

Expand Down
3 changes: 1 addition & 2 deletions packages/discord.js/src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ const { createComponent } = require('../util/Components');
const { NonSystemMessageTypes, MaxBulkDeletableMessageAge, DeletableMessageTypes } = require('../util/Constants');
const MessageFlagsBitField = require('../util/MessageFlagsBitField');
const PermissionsBitField = require('../util/PermissionsBitField');
const { cleanContent, resolvePartialEmoji } = require('../util/Util');
const { transformResolved } = require('../util/transformResolved');
const { cleanContent, resolvePartialEmoji, transformResolved } = require('../util/Util');

/**
* Represents a message on Discord.
Expand Down
71 changes: 71 additions & 0 deletions packages/discord.js/src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { ChannelType, RouteBases, Routes } = require('discord-api-types/v10');
const { fetch } = require('undici');
const Colors = require('./Colors');
const { DiscordjsError, DiscordjsRangeError, DiscordjsTypeError, ErrorCodes } = require('../errors');
const Attachment = require('../structures/Attachment');
const isObject = d => typeof d === 'object' && d !== null;

/**
Expand Down Expand Up @@ -409,6 +410,75 @@ function parseWebhookURL(url) {
};
}

/**
* Supportive data for interaction resolved data.
* @typedef {Object} SupportingInteractionResolvedData
* @property {Client} client The client
* @property {Guild} [guild] A guild
* @property {BaseChannel} [channel] A channel
* @private
*/

/**
* Transforms the resolved data received from the API.
* @param {SupportingInteractionResolvedData} supportingData Data to support the transformation
* @param {APIInteractionDataResolved} [data] The received resolved objects
* @returns {CommandInteractionResolvedData}
* @private
*/
function transformResolved(
{ client, guild, channel },
{ members, users, channels, roles, messages, attachments } = {},
) {
const result = {};

if (members) {
result.members = new Collection();
for (const [id, member] of Object.entries(members)) {
const user = users[id];
result.members.set(id, guild?.members._add({ user, ...member }) ?? member);
}
}

if (users) {
result.users = new Collection();
for (const user of Object.values(users)) {
result.users.set(user.id, client.users._add(user));
}
}

if (roles) {
result.roles = new Collection();
for (const role of Object.values(roles)) {
result.roles.set(role.id, guild?.roles._add(role) ?? role);
}
}

if (channels) {
result.channels = new Collection();
for (const apiChannel of Object.values(channels)) {
result.channels.set(apiChannel.id, client.channels._add(apiChannel, guild) ?? apiChannel);
}
}

if (messages) {
result.messages = new Collection();
for (const message of Object.values(messages)) {
result.messages.set(message.id, channel?.messages?._add(message) ?? message);
}
}

if (attachments) {
result.attachments = new Collection();
for (const attachment of Object.values(attachments)) {
const patched = new Attachment(attachment);
result.attachments.set(attachment.id, patched);
}
}

return result;
}

module.exports = {
flatten,
fetchRecommendedShardCount,
Expand All @@ -426,6 +496,7 @@ module.exports = {
cleanContent,
cleanCodeBlockContent,
parseWebhookURL,
transformResolved,
};

// Fixes Circular
Expand Down
75 changes: 0 additions & 75 deletions packages/discord.js/src/util/transformResolved.js

This file was deleted.

10 changes: 10 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3221,6 +3221,10 @@ export function setPosition<T extends Channel | Role>(
reason?: string,
): Promise<{ id: Snowflake; position: number }[]>;
export function parseWebhookURL(url: string): WebhookClientDataIdWithToken | null;
export function transformResolved<Cached extends CacheType>(
supportingData: SupportingInteractionResolvedData,
data?: APIApplicationCommandInteractionData['resolved'],
): CommandInteractionResolvedData<Cached>;

export interface MappedComponentBuilderTypes {
[ComponentType.Button]: ButtonBuilder;
Expand Down Expand Up @@ -6361,6 +6365,12 @@ export interface StageInstanceEditOptions {
privacyLevel?: StageInstancePrivacyLevel;
}

export interface SupportingInteractionResolvedData {
client: Client;
guild?: Guild;
channel?: Channel;
}

export type SweeperKey = keyof SweeperDefinitions;

export type CollectionSweepFilter<K, V> = (value: V, key: K, collection: Collection<K, V>) => boolean;
Expand Down

0 comments on commit 3195db1

Please sign in to comment.