mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 01:23:31 +01:00
refactor: ES2021 features (#6540)
Co-authored-by: Antonio Román <kyradiscord@gmail.com> Co-authored-by: Voltrex <mohammadkeyvanzade94@gmail.com>
This commit is contained in:
@@ -133,14 +133,14 @@ class Channel extends Base {
|
||||
}
|
||||
|
||||
static create(client, data, guild, { allowUnknownGuild, fromInteraction } = {}) {
|
||||
if (!CategoryChannel) CategoryChannel = require('./CategoryChannel');
|
||||
if (!DMChannel) DMChannel = require('./DMChannel');
|
||||
if (!NewsChannel) NewsChannel = require('./NewsChannel');
|
||||
if (!StageChannel) StageChannel = require('./StageChannel');
|
||||
if (!StoreChannel) StoreChannel = require('./StoreChannel');
|
||||
if (!TextChannel) TextChannel = require('./TextChannel');
|
||||
if (!ThreadChannel) ThreadChannel = require('./ThreadChannel');
|
||||
if (!VoiceChannel) VoiceChannel = require('./VoiceChannel');
|
||||
CategoryChannel ??= require('./CategoryChannel');
|
||||
DMChannel ??= require('./DMChannel');
|
||||
NewsChannel ??= require('./NewsChannel');
|
||||
StageChannel ??= require('./StageChannel');
|
||||
StoreChannel ??= require('./StoreChannel');
|
||||
TextChannel ??= require('./TextChannel');
|
||||
ThreadChannel ??= require('./ThreadChannel');
|
||||
VoiceChannel ??= require('./VoiceChannel');
|
||||
|
||||
let channel;
|
||||
if (!data.guild_id && !guild) {
|
||||
@@ -151,7 +151,7 @@ class Channel extends Base {
|
||||
channel = new PartialGroupDMChannel(client, data);
|
||||
}
|
||||
} else {
|
||||
if (!guild) guild = client.guilds.cache.get(data.guild_id);
|
||||
guild ??= client.guilds.cache.get(data.guild_id);
|
||||
|
||||
if (guild || allowUnknownGuild) {
|
||||
switch (data.type) {
|
||||
|
||||
@@ -53,7 +53,7 @@ class ClientPresence extends Presence {
|
||||
if (activities?.length) {
|
||||
for (const [i, activity] of activities.entries()) {
|
||||
if (typeof activity.name !== 'string') throw new TypeError('INVALID_TYPE', `activities[${i}].name`, 'string');
|
||||
if (!activity.type) activity.type = 0;
|
||||
activity.type ??= 0;
|
||||
|
||||
data.activities.push({
|
||||
type: typeof activity.type === 'number' ? activity.type : ActivityTypes[activity.type],
|
||||
|
||||
@@ -25,8 +25,8 @@ class ClientUser extends User {
|
||||
* @type {?boolean}
|
||||
*/
|
||||
this.mfaEnabled = typeof data.mfa_enabled === 'boolean' ? data.mfa_enabled : null;
|
||||
} else if (typeof this.mfaEnabled === 'undefined') {
|
||||
this.mfaEnabled = null;
|
||||
} else {
|
||||
this.mfaEnabled ??= null;
|
||||
}
|
||||
|
||||
if (data.token) this.client.token = data.token;
|
||||
|
||||
@@ -279,8 +279,8 @@ class Guild extends AnonymousGuild {
|
||||
* @type {?number}
|
||||
*/
|
||||
this.maximumMembers = data.max_members;
|
||||
} else if (typeof this.maximumMembers === 'undefined') {
|
||||
this.maximumMembers = null;
|
||||
} else {
|
||||
this.maximumMembers ??= null;
|
||||
}
|
||||
|
||||
if (typeof data.max_presences !== 'undefined') {
|
||||
@@ -289,9 +289,9 @@ class Guild extends AnonymousGuild {
|
||||
* <info>You will need to fetch the guild using {@link Guild#fetch} if you want to receive this parameter</info>
|
||||
* @type {?number}
|
||||
*/
|
||||
this.maximumPresences = data.max_presences ?? 25000;
|
||||
} else if (typeof this.maximumPresences === 'undefined') {
|
||||
this.maximumPresences = null;
|
||||
this.maximumPresences = data.max_presences ?? 25_000;
|
||||
} else {
|
||||
this.maximumPresences ??= null;
|
||||
}
|
||||
|
||||
if (typeof data.approximate_member_count !== 'undefined') {
|
||||
@@ -301,8 +301,8 @@ class Guild extends AnonymousGuild {
|
||||
* @type {?number}
|
||||
*/
|
||||
this.approximateMemberCount = data.approximate_member_count;
|
||||
} else if (typeof this.approximateMemberCount === 'undefined') {
|
||||
this.approximateMemberCount = null;
|
||||
} else {
|
||||
this.approximateMemberCount ??= null;
|
||||
}
|
||||
|
||||
if (typeof data.approximate_presence_count !== 'undefined') {
|
||||
@@ -312,8 +312,8 @@ class Guild extends AnonymousGuild {
|
||||
* @type {?number}
|
||||
*/
|
||||
this.approximatePresenceCount = data.approximate_presence_count;
|
||||
} else if (typeof this.approximatePresenceCount === 'undefined') {
|
||||
this.approximatePresenceCount = null;
|
||||
} else {
|
||||
this.approximatePresenceCount ??= null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -541,18 +541,18 @@ class Guild extends AnonymousGuild {
|
||||
*/
|
||||
get maximumBitrate() {
|
||||
if (this.features.includes('VIP_REGIONS')) {
|
||||
return 384000;
|
||||
return 384_000;
|
||||
}
|
||||
|
||||
switch (PremiumTiers[this.premiumTier]) {
|
||||
case PremiumTiers.TIER_1:
|
||||
return 128000;
|
||||
return 128_000;
|
||||
case PremiumTiers.TIER_2:
|
||||
return 256000;
|
||||
return 256_000;
|
||||
case PremiumTiers.TIER_3:
|
||||
return 384000;
|
||||
return 384_000;
|
||||
default:
|
||||
return 96000;
|
||||
return 96_000;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -172,7 +172,7 @@ class GuildChannel extends Channel {
|
||||
if (!verified) member = this.guild.members.resolve(member);
|
||||
if (!member) return [];
|
||||
|
||||
if (!roles) roles = member.roles.cache;
|
||||
roles ??= member.roles.cache;
|
||||
const roleOverwrites = [];
|
||||
let memberOverwrites;
|
||||
let everyoneOverwrites;
|
||||
|
||||
@@ -129,7 +129,7 @@ class GuildTemplate extends Base {
|
||||
client.incrementMaxListeners();
|
||||
client.on(Events.GUILD_CREATE, handleGuild);
|
||||
|
||||
const timeout = setTimeout(() => resolveGuild(client.guilds._add(data)), 10000).unref();
|
||||
const timeout = setTimeout(() => resolveGuild(client.guilds._add(data)), 10_000).unref();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -116,8 +116,8 @@ class Integration extends Base {
|
||||
*/
|
||||
this.application = new IntegrationApplication(this.client, data.application);
|
||||
}
|
||||
} else if (!this.application) {
|
||||
this.application = null;
|
||||
} else {
|
||||
this.application ??= null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -162,7 +162,7 @@ class Invite extends Base {
|
||||
get expiresTimestamp() {
|
||||
return (
|
||||
this._expiresTimestamp ??
|
||||
(this.createdTimestamp && this.maxAge ? this.createdTimestamp + this.maxAge * 1000 : null)
|
||||
(this.createdTimestamp && this.maxAge ? this.createdTimestamp + this.maxAge * 1_000 : null)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -92,8 +92,8 @@ class Message extends Base {
|
||||
* @type {?User}
|
||||
*/
|
||||
this.author = this.client.users._add(data.author, !data.webhook_id);
|
||||
} else if (!this.author) {
|
||||
this.author = null;
|
||||
} else {
|
||||
this.author ??= null;
|
||||
}
|
||||
|
||||
if ('pinned' in data) {
|
||||
@@ -102,8 +102,8 @@ class Message extends Base {
|
||||
* @type {?boolean}
|
||||
*/
|
||||
this.pinned = Boolean(data.pinned);
|
||||
} else if (typeof this.pinned !== 'boolean') {
|
||||
this.pinned = null;
|
||||
} else {
|
||||
this.pinned ??= null;
|
||||
}
|
||||
|
||||
if ('tts' in data) {
|
||||
@@ -112,8 +112,8 @@ class Message extends Base {
|
||||
* @type {?boolean}
|
||||
*/
|
||||
this.tts = data.tts;
|
||||
} else if (typeof this.tts !== 'boolean') {
|
||||
this.tts = null;
|
||||
} else {
|
||||
this.tts ??= null;
|
||||
}
|
||||
|
||||
if (!partial) {
|
||||
@@ -334,8 +334,8 @@ class Message extends Base {
|
||||
commandName: data.interaction.name,
|
||||
user: this.client.users._add(data.interaction.user),
|
||||
};
|
||||
} else if (!this.interaction) {
|
||||
this.interaction = null;
|
||||
} else {
|
||||
this.interaction ??= null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -447,7 +447,7 @@ class Message extends Base {
|
||||
* @example
|
||||
* // Create a reaction collector
|
||||
* const filter = (reaction, user) => reaction.emoji.name === '👌' && user.id === 'someId';
|
||||
* const collector = message.createReactionCollector({ filter, time: 15000 });
|
||||
* const collector = message.createReactionCollector({ filter, time: 15_000 });
|
||||
* collector.on('collect', r => console.log(`Collected ${r.emoji.name}`));
|
||||
* collector.on('end', collected => console.log(`Collected ${collected.size} items`));
|
||||
*/
|
||||
@@ -469,7 +469,7 @@ class Message extends Base {
|
||||
* @example
|
||||
* // Create a reaction collector
|
||||
* const filter = (reaction, user) => reaction.emoji.name === '👌' && user.id === 'someId'
|
||||
* message.awaitReactions({ filter, time: 15000 })
|
||||
* message.awaitReactions({ filter, time: 15_000 })
|
||||
* .then(collected => console.log(`Collected ${collected.size} reactions`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
@@ -498,7 +498,7 @@ class Message extends Base {
|
||||
* @example
|
||||
* // Create a message component interaction collector
|
||||
* const filter = (interaction) => interaction.customId === 'button' && interaction.user.id === 'someId';
|
||||
* const collector = message.createMessageComponentCollector({ filter, time: 15000 });
|
||||
* const collector = message.createMessageComponentCollector({ filter, time: 15_000 });
|
||||
* collector.on('collect', i => console.log(`Collected ${i.customId}`));
|
||||
* collector.on('end', collected => console.log(`Collected ${collected.size} items`));
|
||||
*/
|
||||
@@ -526,7 +526,7 @@ class Message extends Base {
|
||||
* @example
|
||||
* // Collect a message component interaction
|
||||
* const filter = (interaction) => interaction.customId === 'button' && interaction.user.id === 'someId';
|
||||
* message.awaitMessageComponent({ filter, time: 15000 })
|
||||
* message.awaitMessageComponent({ filter, time: 15_000 })
|
||||
* .then(interaction => console.log(`${interaction.customId} was clicked!`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
|
||||
@@ -120,7 +120,7 @@ class MessageReaction {
|
||||
if (this.partial) return;
|
||||
this.users.cache.set(user.id, user);
|
||||
if (!this.me || user.id !== this.message.client.user.id || this.count === 0) this.count++;
|
||||
if (!this.me) this.me = user.id === this.message.client.user.id;
|
||||
this.me ??= user.id === this.message.client.user.id;
|
||||
}
|
||||
|
||||
_remove(user) {
|
||||
|
||||
@@ -67,8 +67,8 @@ class ThreadChannel extends Channel {
|
||||
* @type {?Snowflake}
|
||||
*/
|
||||
this.parentId = data.parent_id;
|
||||
} else if (!this.parentId) {
|
||||
this.parentId = null;
|
||||
} else {
|
||||
this.parentId ??= null;
|
||||
}
|
||||
|
||||
if ('thread_metadata' in data) {
|
||||
@@ -105,18 +105,10 @@ class ThreadChannel extends Channel {
|
||||
*/
|
||||
this.archiveTimestamp = new Date(data.thread_metadata.archive_timestamp).getTime();
|
||||
} else {
|
||||
if (!this.locked) {
|
||||
this.locked = null;
|
||||
}
|
||||
if (!this.archived) {
|
||||
this.archived = null;
|
||||
}
|
||||
if (!this.autoArchiveDuration) {
|
||||
this.autoArchiveDuration = null;
|
||||
}
|
||||
if (!this.archiveTimestamp) {
|
||||
this.archiveTimestamp = null;
|
||||
}
|
||||
this.locked ??= null;
|
||||
this.archived ??= null;
|
||||
this.autoArchiveDuration ??= null;
|
||||
this.archiveTimestamp ??= null;
|
||||
this.invitable ??= null;
|
||||
}
|
||||
|
||||
@@ -126,8 +118,8 @@ class ThreadChannel extends Channel {
|
||||
* @type {?Snowflake}
|
||||
*/
|
||||
this.ownerId = data.owner_id;
|
||||
} else if (!this.ownerId) {
|
||||
this.ownerId = null;
|
||||
} else {
|
||||
this.ownerId ??= null;
|
||||
}
|
||||
|
||||
if ('last_message_id' in data) {
|
||||
@@ -136,8 +128,8 @@ class ThreadChannel extends Channel {
|
||||
* @type {?Snowflake}
|
||||
*/
|
||||
this.lastMessageId = data.last_message_id;
|
||||
} else if (!this.lastMessageId) {
|
||||
this.lastMessageId = null;
|
||||
} else {
|
||||
this.lastMessageId ??= null;
|
||||
}
|
||||
|
||||
if ('last_pin_timestamp' in data) {
|
||||
@@ -146,8 +138,8 @@ class ThreadChannel extends Channel {
|
||||
* @type {?number}
|
||||
*/
|
||||
this.lastPinTimestamp = data.last_pin_timestamp ? new Date(data.last_pin_timestamp).getTime() : null;
|
||||
} else if (!this.lastPinTimestamp) {
|
||||
this.lastPinTimestamp = null;
|
||||
} else {
|
||||
this.lastPinTimestamp ??= null;
|
||||
}
|
||||
|
||||
if ('rate_limit_per_user' in data || !partial) {
|
||||
@@ -156,8 +148,8 @@ class ThreadChannel extends Channel {
|
||||
* @type {?number}
|
||||
*/
|
||||
this.rateLimitPerUser = data.rate_limit_per_user ?? 0;
|
||||
} else if (!this.rateLimitPerUser) {
|
||||
this.rateLimitPerUser = null;
|
||||
} else {
|
||||
this.rateLimitPerUser ??= null;
|
||||
}
|
||||
|
||||
if ('message_count' in data) {
|
||||
@@ -168,8 +160,8 @@ class ThreadChannel extends Channel {
|
||||
* @type {?number}
|
||||
*/
|
||||
this.messageCount = data.message_count;
|
||||
} else if (!this.messageCount) {
|
||||
this.messageCount = null;
|
||||
} else {
|
||||
this.messageCount ??= null;
|
||||
}
|
||||
|
||||
if ('member_count' in data) {
|
||||
@@ -180,8 +172,8 @@ class ThreadChannel extends Channel {
|
||||
* @type {?number}
|
||||
*/
|
||||
this.memberCount = data.member_count;
|
||||
} else if (!this.memberCount) {
|
||||
this.memberCount = null;
|
||||
} else {
|
||||
this.memberCount ??= null;
|
||||
}
|
||||
|
||||
if (data.member && this.client.user) this.members._add({ user_id: this.client.user.id, ...data.member });
|
||||
|
||||
@@ -35,7 +35,7 @@ class Typing extends Base {
|
||||
* The UNIX timestamp in milliseconds the user started typing at
|
||||
* @type {number}
|
||||
*/
|
||||
this.startedTimestamp = data.timestamp * 1000;
|
||||
this.startedTimestamp = data.timestamp * 1_000;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -93,8 +93,8 @@ class User extends Base {
|
||||
* @type {?number}
|
||||
*/
|
||||
this.accentColor = data.accent_color;
|
||||
} else if (typeof this.accentColor === 'undefined') {
|
||||
this.accentColor = null;
|
||||
} else {
|
||||
this.accentColor ??= null;
|
||||
}
|
||||
|
||||
if ('system' in data) {
|
||||
|
||||
@@ -53,7 +53,7 @@ class VoiceChannel extends BaseGuildVoiceChannel {
|
||||
* @returns {Promise<VoiceChannel>}
|
||||
* @example
|
||||
* // Set the bitrate of a voice channel
|
||||
* voiceChannel.setBitrate(48000)
|
||||
* voiceChannel.setBitrate(48_000)
|
||||
* .then(vc => console.log(`Set bitrate to ${vc.bitrate}bps for ${vc.name}`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
|
||||
@@ -189,7 +189,7 @@ class Webhook {
|
||||
* 'color': '#F0F',
|
||||
* 'footer_icon': 'http://snek.s3.amazonaws.com/topSnek.png',
|
||||
* 'footer': 'Powered by sneks',
|
||||
* 'ts': Date.now() / 1000
|
||||
* 'ts': Date.now() / 1_000
|
||||
* }]
|
||||
* }).catch(console.error);
|
||||
* @see {@link https://api.slack.com/messaging/webhooks}
|
||||
|
||||
@@ -198,7 +198,7 @@ class TextBasedChannel {
|
||||
* @example
|
||||
* // Create a message collector
|
||||
* const filter = m => m.content.includes('discord');
|
||||
* const collector = channel.createMessageCollector({ filter, time: 15000 });
|
||||
* const collector = channel.createMessageCollector({ filter, time: 15_000 });
|
||||
* collector.on('collect', m => console.log(`Collected ${m.content}`));
|
||||
* collector.on('end', collected => console.log(`Collected ${collected.size} items`));
|
||||
*/
|
||||
@@ -221,7 +221,7 @@ class TextBasedChannel {
|
||||
* // Await !vote messages
|
||||
* const filter = m => m.content.startsWith('!vote');
|
||||
* // Errors: ['time'] treats ending because of the time limit as an error
|
||||
* channel.awaitMessages({ filter, max: 4, time: 60000, errors: ['time'] })
|
||||
* channel.awaitMessages({ filter, max: 4, time: 60_000, errors: ['time'] })
|
||||
* .then(collected => console.log(collected.size))
|
||||
* .catch(collected => console.log(`After a minute, only ${collected.size} out of 4 voted.`));
|
||||
*/
|
||||
@@ -245,7 +245,7 @@ class TextBasedChannel {
|
||||
* @example
|
||||
* // Create a button interaction collector
|
||||
* const filter = (interaction) => interaction.customId === 'button' && interaction.user.id === 'someId';
|
||||
* const collector = channel.createMessageComponentCollector({ filter, time: 15000 });
|
||||
* const collector = channel.createMessageComponentCollector({ filter, time: 15_000 });
|
||||
* collector.on('collect', i => console.log(`Collected ${i.customId}`));
|
||||
* collector.on('end', collected => console.log(`Collected ${collected.size} items`));
|
||||
*/
|
||||
@@ -265,7 +265,7 @@ class TextBasedChannel {
|
||||
* @example
|
||||
* // Collect a message component interaction
|
||||
* const filter = (interaction) => interaction.customId === 'button' && interaction.user.id === 'someId';
|
||||
* channel.awaitMessageComponent({ filter, time: 15000 })
|
||||
* channel.awaitMessageComponent({ filter, time: 15_000 })
|
||||
* .then(interaction => console.log(`${interaction.customId} was clicked!`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
@@ -297,7 +297,7 @@ class TextBasedChannel {
|
||||
if (Array.isArray(messages) || messages instanceof Collection) {
|
||||
let messageIds = messages instanceof Collection ? [...messages.keys()] : messages.map(m => m.id ?? m);
|
||||
if (filterOld) {
|
||||
messageIds = messageIds.filter(id => Date.now() - SnowflakeUtil.deconstruct(id).timestamp < 1209600000);
|
||||
messageIds = messageIds.filter(id => Date.now() - SnowflakeUtil.deconstruct(id).timestamp < 1_209_600_000);
|
||||
}
|
||||
if (messageIds.length === 0) return new Collection();
|
||||
if (messageIds.length === 1) {
|
||||
|
||||
Reference in New Issue
Block a user