mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +01:00
refactor: use proper variable names in callbacks
This commit is contained in:
@@ -369,7 +369,7 @@ class Client extends BaseClient {
|
||||
*/
|
||||
async fetchStickerPacks() {
|
||||
const data = await this.rest.get(Routes.stickerPacks());
|
||||
return new Collection(data.sticker_packs.map(p => [p.id, new StickerPack(this, p)]));
|
||||
return new Collection(data.sticker_packs.map(stickerPack => [stickerPack.id, new StickerPack(this, stickerPack)]));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -364,7 +364,7 @@ class WebSocketManager extends EventEmitter {
|
||||
*/
|
||||
checkShardsReady() {
|
||||
if (this.status === Status.Ready) return;
|
||||
if (this.shards.size !== this.totalShards || this.shards.some(s => s.status !== Status.Ready)) {
|
||||
if (this.shards.size !== this.totalShards || this.shards.some(shard => shard.status !== Status.Ready)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -140,7 +140,7 @@ class WebSocketShard extends EventEmitter {
|
||||
*/
|
||||
this.emit(WebSocketShardEvents.Ready);
|
||||
|
||||
this.expectedGuilds = new Set(packet.guilds.map(d => d.id));
|
||||
this.expectedGuilds = new Set(packet.guilds.map(guild => guild.id));
|
||||
this.status = Status.WaitingForGuilds;
|
||||
}
|
||||
|
||||
|
||||
@@ -169,9 +169,12 @@ class ApplicationCommandManager extends CachedManager {
|
||||
*/
|
||||
async set(commands, guildId) {
|
||||
const data = await this.client.rest.put(this.commandPath({ guildId }), {
|
||||
body: commands.map(c => this.constructor.transformCommand(c)),
|
||||
body: commands.map(command => this.constructor.transformCommand(command)),
|
||||
});
|
||||
return data.reduce((coll, command) => coll.set(command.id, this._add(command, true, guildId)), new Collection());
|
||||
return data.reduce(
|
||||
(collection, command) => collection.set(command.id, this._add(command, true, guildId)),
|
||||
new Collection(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -253,7 +256,7 @@ class ApplicationCommandManager extends CachedManager {
|
||||
nsfw: command.nsfw,
|
||||
description_localizations: command.descriptionLocalizations ?? command.description_localizations,
|
||||
type: command.type,
|
||||
options: command.options?.map(o => ApplicationCommand.transformOption(o)),
|
||||
options: command.options?.map(option => ApplicationCommand.transformOption(option)),
|
||||
default_member_permissions,
|
||||
dm_permission: command.dmPermission ?? command.dm_permission,
|
||||
};
|
||||
|
||||
@@ -23,7 +23,7 @@ class CategoryChannelChildManager extends DataManager {
|
||||
* @readonly
|
||||
*/
|
||||
get cache() {
|
||||
return this.guild.channels.cache.filter(c => c.parentId === this.channel.id);
|
||||
return this.guild.channels.cache.filter(channel => channel.parentId === this.channel.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -165,7 +165,7 @@ class GuildChannelManager extends CachedManager {
|
||||
reason,
|
||||
}) {
|
||||
parent &&= this.client.channels.resolveId(parent);
|
||||
permissionOverwrites &&= permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild));
|
||||
permissionOverwrites &&= permissionOverwrites.map(overwrite => PermissionOverwrites.resolve(overwrite, this.guild));
|
||||
|
||||
const data = await this.client.rest.post(Routes.guildChannels(this.guild.id), {
|
||||
body: {
|
||||
@@ -279,19 +279,21 @@ class GuildChannelManager extends CachedManager {
|
||||
await this.setPosition(channel, options.position, { position: options.position, reason: options.reason });
|
||||
}
|
||||
|
||||
let permission_overwrites = options.permissionOverwrites?.map(o => PermissionOverwrites.resolve(o, this.guild));
|
||||
let permission_overwrites = options.permissionOverwrites?.map(overwrite =>
|
||||
PermissionOverwrites.resolve(overwrite, this.guild),
|
||||
);
|
||||
|
||||
if (options.lockPermissions) {
|
||||
if (parent) {
|
||||
const newParent = this.guild.channels.resolve(parent);
|
||||
if (newParent?.type === ChannelType.GuildCategory) {
|
||||
permission_overwrites = newParent.permissionOverwrites.cache.map(o =>
|
||||
PermissionOverwrites.resolve(o, this.guild),
|
||||
permission_overwrites = newParent.permissionOverwrites.cache.map(overwrite =>
|
||||
PermissionOverwrites.resolve(overwrite, this.guild),
|
||||
);
|
||||
}
|
||||
} else if (channel.parent) {
|
||||
permission_overwrites = channel.parent.permissionOverwrites.cache.map(o =>
|
||||
PermissionOverwrites.resolve(o, this.guild),
|
||||
permission_overwrites = channel.parent.permissionOverwrites.cache.map(overwrite =>
|
||||
PermissionOverwrites.resolve(overwrite, this.guild),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -436,11 +438,11 @@ class GuildChannelManager extends CachedManager {
|
||||
* .catch(console.error);
|
||||
*/
|
||||
async setPositions(channelPositions) {
|
||||
channelPositions = channelPositions.map(r => ({
|
||||
id: this.client.channels.resolveId(r.channel),
|
||||
position: r.position,
|
||||
lock_permissions: r.lockPermissions,
|
||||
parent_id: r.parent !== undefined ? this.resolveId(r.parent) : undefined,
|
||||
channelPositions = channelPositions.map(channelPosition => ({
|
||||
id: this.client.channels.resolveId(channelPosition.channel),
|
||||
position: channelPosition.position,
|
||||
lock_permissions: channelPosition.lockPermissions,
|
||||
parent_id: channelPosition.parent !== undefined ? this.resolveId(channelPosition.parent) : undefined,
|
||||
}));
|
||||
|
||||
await this.client.rest.patch(Routes.guildChannels(this.guild.id), { body: channelPositions });
|
||||
|
||||
@@ -130,7 +130,7 @@ class GuildEmojiManager extends BaseGuildEmojiManager {
|
||||
async edit(emoji, options) {
|
||||
const id = this.resolveId(emoji);
|
||||
if (!id) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'emoji', 'EmojiResolvable', true);
|
||||
const roles = options.roles?.map(r => this.guild.roles.resolveId(r));
|
||||
const roles = options.roles?.map(role => this.guild.roles.resolveId(role));
|
||||
const newData = await this.client.rest.patch(Routes.guildEmoji(this.guild.id, id), {
|
||||
body: {
|
||||
name: options.name,
|
||||
|
||||
@@ -86,7 +86,8 @@ class MessageManager extends CachedManager {
|
||||
* @example
|
||||
* // Fetch messages and filter by a user id
|
||||
* channel.messages.fetch()
|
||||
* .then(messages => console.log(`${messages.filter(m => m.author.id === '84484653687267328').size} messages`))
|
||||
* .then(messages => console.log(`${messages.filter(message =>
|
||||
* message.author.id === '84484653687267328').size} messages`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
fetch(options) {
|
||||
|
||||
@@ -280,9 +280,9 @@ class RoleManager extends CachedManager {
|
||||
*/
|
||||
async setPositions(rolePositions) {
|
||||
// Make sure rolePositions are prepared for API
|
||||
rolePositions = rolePositions.map(o => ({
|
||||
id: this.resolveId(o.role),
|
||||
position: o.position,
|
||||
rolePositions = rolePositions.map(rolePosition => ({
|
||||
id: this.resolveId(rolePosition.role),
|
||||
position: rolePosition.position,
|
||||
}));
|
||||
|
||||
// Call the API to update role positions
|
||||
|
||||
@@ -40,7 +40,10 @@ class UserManager extends CachedManager {
|
||||
* @private
|
||||
*/
|
||||
dmChannel(userId) {
|
||||
return this.client.channels.cache.find(c => c.type === ChannelType.DM && c.recipientId === userId) ?? null;
|
||||
return (
|
||||
this.client.channels.cache.find(channel => channel.type === ChannelType.DM && channel.recipientId === userId) ??
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,7 +18,7 @@ class ActionRow extends Component {
|
||||
* @type {Component[]}
|
||||
* @readonly
|
||||
*/
|
||||
this.components = components.map(c => createComponent(c));
|
||||
this.components = components.map(component => createComponent(component));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,7 +39,7 @@ class ActionRow extends Component {
|
||||
* @returns {APIActionRowComponent}
|
||||
*/
|
||||
toJSON() {
|
||||
return { ...this.data, components: this.components.map(c => c.toJSON()) };
|
||||
return { ...this.data, components: this.components.map(component => component.toJSON()) };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ class ActionRowBuilder extends BuildersActionRow {
|
||||
constructor({ components, ...data } = {}) {
|
||||
super({
|
||||
...toSnakeCase(data),
|
||||
components: components?.map(c => createComponentBuilder(c)),
|
||||
components: components?.map(component => createComponentBuilder(component)),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class ChatInputCommandInteraction extends CommandInteraction {
|
||||
this.commandName,
|
||||
this.options._group,
|
||||
this.options._subcommand,
|
||||
...this.options._hoistedOptions.map(o => `${o.name}:${o.value}`),
|
||||
...this.options._hoistedOptions.map(option => `${option.name}:${option.value}`),
|
||||
];
|
||||
return `/${properties.filter(Boolean).join(' ')}`;
|
||||
}
|
||||
|
||||
@@ -68,11 +68,11 @@ class ClientPresence extends Presence {
|
||||
}
|
||||
} else if (!activities && (status || afk || since) && this.activities.length) {
|
||||
data.activities.push(
|
||||
...this.activities.map(a => ({
|
||||
name: a.name,
|
||||
state: a.state ?? undefined,
|
||||
type: a.type,
|
||||
url: a.url ?? undefined,
|
||||
...this.activities.map(activity => ({
|
||||
name: activity.name,
|
||||
state: activity.state ?? undefined,
|
||||
type: activity.type,
|
||||
url: activity.url ?? undefined,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1263,7 +1263,7 @@ class Guild extends AnonymousGuild {
|
||||
* @example
|
||||
* // Delete a guild
|
||||
* guild.delete()
|
||||
* .then(g => console.log(`Deleted the guild ${g}`))
|
||||
* .then(guild => console.log(`Deleted the guild ${guild}`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
async delete() {
|
||||
@@ -1365,7 +1365,9 @@ class Guild extends AnonymousGuild {
|
||||
const channelIsCategory = channel.type === ChannelType.GuildCategory;
|
||||
const types = getSortableGroupTypes(channel.type);
|
||||
return discordSort(
|
||||
this.channels.cache.filter(c => types.includes(c.type) && (channelIsCategory || c.parentId === channel.parentId)),
|
||||
this.channels.cache.filter(
|
||||
({ parentId, type }) => types.includes(type) && (channelIsCategory || parentId === channel.parentId),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,7 +279,9 @@ class GuildChannel extends BaseChannel {
|
||||
* @readonly
|
||||
*/
|
||||
get members() {
|
||||
return this.guild.members.cache.filter(m => this.permissionsFor(m).has(PermissionFlagsBits.ViewChannel, false));
|
||||
return this.guild.members.cache.filter(member =>
|
||||
this.permissionsFor(member).has(PermissionFlagsBits.ViewChannel, false),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -91,7 +91,7 @@ class GuildEmoji extends BaseGuildEmoji {
|
||||
* @example
|
||||
* // Edit an emoji
|
||||
* emoji.edit({ name: 'newemoji' })
|
||||
* .then(e => console.log(`Edited emoji ${e}`))
|
||||
* .then(emoji => console.log(`Edited emoji ${emoji}`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
edit(options) {
|
||||
|
||||
@@ -141,7 +141,7 @@ class Message extends Base {
|
||||
* in a guild for messages that do not mention the client.</info>
|
||||
* @type {Embed[]}
|
||||
*/
|
||||
this.embeds = data.embeds.map(e => new Embed(e));
|
||||
this.embeds = data.embeds.map(embed => new Embed(embed));
|
||||
} else {
|
||||
this.embeds = this.embeds?.slice() ?? [];
|
||||
}
|
||||
@@ -153,7 +153,7 @@ class Message extends Base {
|
||||
* in a guild for messages that do not mention the client.</info>
|
||||
* @type {ActionRow[]}
|
||||
*/
|
||||
this.components = data.components.map(c => createComponent(c));
|
||||
this.components = data.components.map(component => createComponent(component));
|
||||
} else {
|
||||
this.components = this.components?.slice() ?? [];
|
||||
}
|
||||
@@ -181,7 +181,7 @@ class Message extends Base {
|
||||
* @type {Collection<Snowflake, Sticker>}
|
||||
*/
|
||||
this.stickers = new Collection(
|
||||
(data.sticker_items ?? data.stickers)?.map(s => [s.id, new Sticker(this.client, s)]),
|
||||
(data.sticker_items ?? data.stickers)?.map(sticker => [sticker.id, new Sticker(this.client, sticker)]),
|
||||
);
|
||||
} else {
|
||||
this.stickers = new Collection(this.stickers);
|
||||
|
||||
@@ -167,12 +167,12 @@ class MessageMentions {
|
||||
this.crosspostedChannels = new Collection(crosspostedChannels);
|
||||
} else {
|
||||
this.crosspostedChannels = new Collection();
|
||||
for (const d of crosspostedChannels) {
|
||||
this.crosspostedChannels.set(d.id, {
|
||||
channelId: d.id,
|
||||
guildId: d.guild_id,
|
||||
type: d.type,
|
||||
name: d.name,
|
||||
for (const crosspostedChannel of crosspostedChannels) {
|
||||
this.crosspostedChannels.set(crosspostedChannel.id, {
|
||||
channelId: crosspostedChannel.id,
|
||||
guildId: crosspostedChannel.guild_id,
|
||||
type: crosspostedChannel.type,
|
||||
name: crosspostedChannel.name,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,7 +133,9 @@ class MessagePayload {
|
||||
}
|
||||
}
|
||||
|
||||
const components = this.options.components?.map(c => (isJSONEncodable(c) ? c : new ActionRowBuilder(c)).toJSON());
|
||||
const components = this.options.components?.map(component =>
|
||||
(isJSONEncodable(component) ? component : new ActionRowBuilder(component)).toJSON(),
|
||||
);
|
||||
|
||||
let username;
|
||||
let avatarURL;
|
||||
|
||||
@@ -12,7 +12,9 @@ class ModalBuilder extends BuildersModal {
|
||||
constructor({ components, ...data } = {}) {
|
||||
super({
|
||||
...toSnakeCase(data),
|
||||
components: components?.map(c => (c instanceof ComponentBuilder ? c : toSnakeCase(c))),
|
||||
components: components?.map(component =>
|
||||
component instanceof ComponentBuilder ? component : toSnakeCase(component),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ class ModalSubmitFields {
|
||||
* @type {Collection<string, ModalData>}
|
||||
*/
|
||||
this.fields = components.reduce((accumulator, next) => {
|
||||
next.components.forEach(c => accumulator.set(c.customId, c));
|
||||
next.components.forEach(component => accumulator.set(component.customId, component));
|
||||
return accumulator;
|
||||
}, new Collection());
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ class ModalSubmitInteraction extends BaseInteraction {
|
||||
* The components within the modal
|
||||
* @type {ActionRowModalData[]}
|
||||
*/
|
||||
this.components = data.data.components?.map(c => ModalSubmitInteraction.transformComponent(c));
|
||||
this.components = data.data.components?.map(component => ModalSubmitInteraction.transformComponent(component));
|
||||
|
||||
/**
|
||||
* The fields within the modal
|
||||
@@ -89,7 +89,10 @@ class ModalSubmitInteraction extends BaseInteraction {
|
||||
*/
|
||||
static transformComponent(rawComponent) {
|
||||
return rawComponent.components
|
||||
? { type: rawComponent.type, components: rawComponent.components.map(c => this.transformComponent(c)) }
|
||||
? {
|
||||
type: rawComponent.type,
|
||||
components: rawComponent.components.map(component => this.transformComponent(component)),
|
||||
}
|
||||
: {
|
||||
value: rawComponent.value,
|
||||
type: rawComponent.type,
|
||||
|
||||
@@ -96,7 +96,7 @@ class ReactionCollector extends Collector {
|
||||
|
||||
this.on('remove', (reaction, user) => {
|
||||
this.total--;
|
||||
if (!this.collected.some(r => r.users.cache.has(user.id))) this.users.delete(user.id);
|
||||
if (!this.collected.some(users => users.cache.has(user.id))) this.users.delete(user.id);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -180,7 +180,7 @@ class Role extends Base {
|
||||
get members() {
|
||||
return this.id === this.guild.id
|
||||
? this.guild.members.cache.clone()
|
||||
: this.guild.members.cache.filter(m => m._roles.includes(this.id));
|
||||
: this.guild.members.cache.filter(member => member._roles.includes(this.id));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -212,7 +212,7 @@ class Sticker extends Base {
|
||||
* @example
|
||||
* // Update the name of a sticker
|
||||
* sticker.edit({ name: 'new name' })
|
||||
* .then(s => console.log(`Updated the name of the sticker to ${s.name}`))
|
||||
* .then(sticker => console.log(`Updated the name of the sticker to ${sticker.name}`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
edit(options) {
|
||||
@@ -226,7 +226,7 @@ class Sticker extends Base {
|
||||
* @example
|
||||
* // Delete a message
|
||||
* sticker.delete()
|
||||
* .then(s => console.log(`Deleted sticker ${s.name}`))
|
||||
* .then(sticker => console.log(`Deleted sticker ${sticker.name}`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
async delete(reason) {
|
||||
|
||||
@@ -22,7 +22,9 @@ class StickerPack extends Base {
|
||||
* The stickers in the pack
|
||||
* @type {Collection<Snowflake, Sticker>}
|
||||
*/
|
||||
this.stickers = new Collection(pack.stickers.map(s => [s.id, new Sticker(client, s)]));
|
||||
this.stickers = new Collection(
|
||||
pack.stickers.map(stickerPack => [stickerPack.id, new Sticker(client, stickerPack)]),
|
||||
);
|
||||
|
||||
/**
|
||||
* The name of the sticker pack
|
||||
|
||||
@@ -181,9 +181,9 @@ class TextBasedChannel {
|
||||
* @returns {MessageCollector}
|
||||
* @example
|
||||
* // Create a message collector
|
||||
* const filter = m => m.content.includes('discord');
|
||||
* const filter = message => message.content.includes('discord');
|
||||
* const collector = channel.createMessageCollector({ filter, time: 15_000 });
|
||||
* collector.on('collect', m => console.log(`Collected ${m.content}`));
|
||||
* collector.on('collect', message => console.log(`Collected ${message.content}`));
|
||||
* collector.on('end', collected => console.log(`Collected ${collected.size} items`));
|
||||
*/
|
||||
createMessageCollector(options = {}) {
|
||||
@@ -230,7 +230,7 @@ class TextBasedChannel {
|
||||
* // Create a button interaction collector
|
||||
* const filter = (interaction) => interaction.customId === 'button' && interaction.user.id === 'someId';
|
||||
* const collector = channel.createMessageComponentCollector({ filter, time: 15_000 });
|
||||
* collector.on('collect', i => console.log(`Collected ${i.customId}`));
|
||||
* collector.on('collect', interaction => console.log(`Collected ${interaction.customId}`));
|
||||
* collector.on('end', collected => console.log(`Collected ${collected.size} items`));
|
||||
*/
|
||||
createMessageComponentCollector(options = {}) {
|
||||
@@ -279,7 +279,8 @@ class TextBasedChannel {
|
||||
*/
|
||||
async bulkDelete(messages, filterOld = false) {
|
||||
if (Array.isArray(messages) || messages instanceof Collection) {
|
||||
let messageIds = messages instanceof Collection ? [...messages.keys()] : messages.map(m => m.id ?? m);
|
||||
let messageIds =
|
||||
messages instanceof Collection ? [...messages.keys()] : messages.map(message => message.id ?? message);
|
||||
if (filterOld) {
|
||||
messageIds = messageIds.filter(
|
||||
id => Date.now() - DiscordSnowflake.timestampFrom(id) < MaxBulkDeletableMessageAge,
|
||||
|
||||
@@ -164,7 +164,11 @@ class BitField {
|
||||
const { DefaultBit } = this;
|
||||
if (typeof DefaultBit === typeof bit && bit >= DefaultBit) return bit;
|
||||
if (bit instanceof BitField) return bit.bitfield;
|
||||
if (Array.isArray(bit)) return bit.map(p => this.resolve(p)).reduce((prev, p) => prev | p, DefaultBit);
|
||||
if (Array.isArray(bit)) {
|
||||
return bit
|
||||
.map(permission => this.resolve(permission))
|
||||
.reduce((prev, permission) => prev | permission, DefaultBit);
|
||||
}
|
||||
if (typeof bit === 'string') {
|
||||
if (!isNaN(bit)) return typeof DefaultBit === 'bigint' ? BigInt(bit) : Number(bit);
|
||||
if (this.Flags[bit] !== undefined) return this.Flags[bit];
|
||||
|
||||
@@ -364,8 +364,8 @@ client.on('messageCreate', async message => {
|
||||
// https://github.com/discordjs/discord.js/issues/8545
|
||||
{
|
||||
// These should not throw any errors when comparing messages from any source.
|
||||
channel.messages.cache.filter(m => m);
|
||||
(await channel.messages.fetch()).filter(m => m.author.id === message.author.id);
|
||||
channel.messages.cache.filter(message => message);
|
||||
(await channel.messages.fetch()).filter(({ author }) => author.id === message.author.id);
|
||||
|
||||
if (channel.isDMBased()) {
|
||||
expectType<DMMessageManager>(channel.messages.channel.messages);
|
||||
@@ -2047,7 +2047,7 @@ collector.on('end', (collection, reason) => {
|
||||
}
|
||||
})();
|
||||
|
||||
expectType<Promise<number | null>>(shard.eval(c => c.readyTimestamp));
|
||||
expectType<Promise<number | null>>(shard.eval(client => client.readyTimestamp));
|
||||
|
||||
// Test audit logs
|
||||
expectType<Promise<GuildAuditLogs<AuditLogEvent.MemberKick>>>(guild.fetchAuditLogs({ type: AuditLogEvent.MemberKick }));
|
||||
|
||||
Reference in New Issue
Block a user