From 869153c3fdf155783e7c0ecebd3627b087c3a026 Mon Sep 17 00:00:00 2001 From: Almeida Date: Sun, 12 Mar 2023 20:24:22 +0000 Subject: [PATCH] refactor: compare with `undefined` directly (#9191) * refactor: compare with `undefined` directly * fix: lint --- packages/collection/src/collection.ts | 34 +++++++++---------- packages/discord.js/src/client/Client.js | 6 ++-- .../src/managers/GuildBanManager.js | 4 +-- .../src/managers/GuildChannelManager.js | 4 +-- .../discord.js/src/managers/GuildManager.js | 11 +++--- .../src/managers/GuildMemberManager.js | 4 +-- .../managers/GuildScheduledEventManager.js | 6 ++-- .../discord.js/src/managers/RoleManager.js | 7 ++-- .../discord.js/src/managers/ThreadManager.js | 2 +- .../src/structures/ApplicationCommand.js | 2 +- .../src/structures/ClientPresence.js | 2 +- .../discord.js/src/structures/ClientUser.js | 2 +- .../CommandInteractionOptionResolver.js | 2 +- .../discord.js/src/structures/DMChannel.js | 2 +- packages/discord.js/src/structures/Guild.js | 4 +-- .../discord.js/src/structures/GuildChannel.js | 4 +-- .../discord.js/src/structures/InviteGuild.js | 3 +- .../src/structures/MessagePayload.js | 15 ++++---- .../discord.js/src/structures/VoiceState.js | 2 +- packages/discord.js/src/util/BitField.js | 2 +- packages/docgen/src/types/param.ts | 2 +- packages/docgen/src/types/typedef.ts | 4 +-- packages/formatters/src/formatters.ts | 8 ++--- packages/voice/src/DataStore.ts | 2 +- packages/voice/src/VoiceConnection.ts | 4 +-- packages/voice/src/audio/AudioResource.ts | 2 +- packages/voice/src/networking/Networking.ts | 2 +- .../voice/src/networking/VoiceWebSocket.ts | 2 +- .../voice/src/receive/AudioReceiveStream.ts | 2 +- 29 files changed, 69 insertions(+), 77 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index 425fcd0fa..8b29f9032 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -84,7 +84,7 @@ export class Collection extends Map { public first(): V | undefined; public first(amount: number): V[]; public first(amount?: number): V | V[] | undefined { - if (typeof amount === 'undefined') return this.values().next().value; + if (amount === undefined) return this.values().next().value; if (amount < 0) return this.last(amount * -1); amount = Math.min(this.size, amount); const iter = this.values(); @@ -101,7 +101,7 @@ export class Collection extends Map { public firstKey(): K | undefined; public firstKey(amount: number): K[]; public firstKey(amount?: number): K | K[] | undefined { - if (typeof amount === 'undefined') return this.keys().next().value; + if (amount === undefined) return this.keys().next().value; if (amount < 0) return this.lastKey(amount * -1); amount = Math.min(this.size, amount); const iter = this.keys(); @@ -119,7 +119,7 @@ export class Collection extends Map { public last(amount: number): V[]; public last(amount?: number): V | V[] | undefined { const arr = [...this.values()]; - if (typeof amount === 'undefined') return arr[arr.length - 1]; + if (amount === undefined) return arr[arr.length - 1]; if (amount < 0) return this.first(amount * -1); if (!amount) return []; return arr.slice(-amount); @@ -136,7 +136,7 @@ export class Collection extends Map { public lastKey(amount: number): K[]; public lastKey(amount?: number): K | K[] | undefined { const arr = [...this.keys()]; - if (typeof amount === 'undefined') return arr[arr.length - 1]; + if (amount === undefined) return arr[arr.length - 1]; if (amount < 0) return this.firstKey(amount * -1); if (!amount) return []; return arr.slice(-amount); @@ -178,7 +178,7 @@ export class Collection extends Map { public random(amount: number): V[]; public random(amount?: number): V | V[] | undefined { const arr = [...this.values()]; - if (typeof amount === 'undefined') return arr[Math.floor(Math.random() * arr.length)]; + if (amount === undefined) return arr[Math.floor(Math.random() * arr.length)]; if (!arr.length || !amount) return []; return Array.from( { length: Math.min(amount, arr.length) }, @@ -196,7 +196,7 @@ export class Collection extends Map { public randomKey(amount: number): K[]; public randomKey(amount?: number): K | K[] | undefined { const arr = [...this.keys()]; - if (typeof amount === 'undefined') return arr[Math.floor(Math.random() * arr.length)]; + if (amount === undefined) return arr[Math.floor(Math.random() * arr.length)]; if (!arr.length || !amount) return []; return Array.from( { length: Math.min(amount, arr.length) }, @@ -238,7 +238,7 @@ export class Collection extends Map { public find(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): V | undefined; public find(fn: (value: V, key: K, collection: this) => unknown, thisArg?: unknown): V | undefined { if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); - if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); + if (thisArg !== undefined) fn = fn.bind(thisArg); for (const [key, val] of this) { if (fn(val, key, this)) return val; } @@ -267,7 +267,7 @@ export class Collection extends Map { public findKey(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): K | undefined; public findKey(fn: (value: V, key: K, collection: this) => unknown, thisArg?: unknown): K | undefined { if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); - if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); + if (thisArg !== undefined) fn = fn.bind(thisArg); for (const [key, val] of this) { if (fn(val, key, this)) return key; } @@ -286,7 +286,7 @@ export class Collection extends Map { public sweep(fn: (this: T, value: V, key: K, collection: this) => unknown, thisArg: T): number; public sweep(fn: (value: V, key: K, collection: this) => unknown, thisArg?: unknown): number { if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); - if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); + if (thisArg !== undefined) fn = fn.bind(thisArg); const previousSize = this.size; for (const [key, val] of this) { if (fn(val, key, this)) this.delete(key); @@ -321,7 +321,7 @@ export class Collection extends Map { public filter(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): Collection; public filter(fn: (value: V, key: K, collection: this) => unknown, thisArg?: unknown): Collection { if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); - if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); + if (thisArg !== undefined) fn = fn.bind(thisArg); const results = new this.constructor[Symbol.species](); for (const [key, val] of this) { if (fn(val, key, this)) results.set(key, val); @@ -365,7 +365,7 @@ export class Collection extends Map { thisArg?: unknown, ): [Collection, Collection] { if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); - if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); + if (thisArg !== undefined) fn = fn.bind(thisArg); const results: [Collection, Collection] = [ new this.constructor[Symbol.species](), new this.constructor[Symbol.species](), @@ -418,7 +418,7 @@ export class Collection extends Map { public map(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): T[]; public map(fn: (value: V, key: K, collection: this) => T, thisArg?: unknown): T[] { if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); - if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); + if (thisArg !== undefined) fn = fn.bind(thisArg); const iter = this.entries(); return Array.from({ length: this.size }, (): T => { const [key, value] = iter.next().value; @@ -441,7 +441,7 @@ export class Collection extends Map { public mapValues(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): Collection; public mapValues(fn: (value: V, key: K, collection: this) => T, thisArg?: unknown): Collection { if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); - if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); + if (thisArg !== undefined) fn = fn.bind(thisArg); const coll = new this.constructor[Symbol.species](); for (const [key, val] of this) coll.set(key, fn(val, key, this)); return coll; @@ -462,7 +462,7 @@ export class Collection extends Map { public some(fn: (this: T, value: V, key: K, collection: this) => unknown, thisArg: T): boolean; public some(fn: (value: V, key: K, collection: this) => unknown, thisArg?: unknown): boolean { if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); - if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); + if (thisArg !== undefined) fn = fn.bind(thisArg); for (const [key, val] of this) { if (fn(val, key, this)) return true; } @@ -495,7 +495,7 @@ export class Collection extends Map { public every(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): boolean; public every(fn: (value: V, key: K, collection: this) => unknown, thisArg?: unknown): boolean { if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); - if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); + if (thisArg !== undefined) fn = fn.bind(thisArg); for (const [key, val] of this) { if (!fn(val, key, this)) return false; } @@ -519,7 +519,7 @@ export class Collection extends Map { if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); let accumulator!: T; - if (typeof initialValue !== 'undefined') { + if (initialValue !== undefined) { accumulator = initialValue; for (const [key, val] of this) accumulator = fn(accumulator, val, key, this); return accumulator; @@ -585,7 +585,7 @@ export class Collection extends Map { public tap(fn: (this: T, collection: this) => void, thisArg: T): this; public tap(fn: (collection: this) => void, thisArg?: unknown): this { if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); - if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); + if (thisArg !== undefined) fn = fn.bind(thisArg); fn(this); return this; } diff --git a/packages/discord.js/src/client/Client.js b/packages/discord.js/src/client/Client.js index f9a91f6dd..2263e969c 100644 --- a/packages/discord.js/src/client/Client.js +++ b/packages/discord.js/src/client/Client.js @@ -307,7 +307,7 @@ class Client extends BaseClient { * .catch(console.error); */ async fetchWebhook(id, token) { - const data = await this.rest.get(Routes.webhook(id, token), { auth: typeof token === 'undefined' }); + const data = await this.rest.get(Routes.webhook(id, token), { auth: token === undefined }); return new Webhook(this, { token, ...data }); } @@ -411,7 +411,7 @@ class Client extends BaseClient { if (!this.application) throw new DiscordjsError(ErrorCodes.ClientNotReady, 'generate an invite link'); const { scopes } = options; - if (typeof scopes === 'undefined') { + if (scopes === undefined) { throw new DiscordjsTypeError(ErrorCodes.InvalidMissingScopes); } if (!Array.isArray(scopes)) { @@ -485,7 +485,7 @@ class Client extends BaseClient { * @private */ _validateOptions(options = this.options) { - if (typeof options.intents === 'undefined') { + if (options.intents === undefined) { throw new DiscordjsTypeError(ErrorCodes.ClientMissingIntents); } else { options.intents = new IntentsBitField(options.intents).freeze(); diff --git a/packages/discord.js/src/managers/GuildBanManager.js b/packages/discord.js/src/managers/GuildBanManager.js index a07fd7e6e..d3c8a0033 100644 --- a/packages/discord.js/src/managers/GuildBanManager.js +++ b/packages/discord.js/src/managers/GuildBanManager.js @@ -103,7 +103,7 @@ class GuildBanManager extends CachedManager { const resolvedUser = this.client.users.resolveId(user ?? options); if (resolvedUser) return this._fetchSingle({ user: resolvedUser, cache, force }); - if (!before && !after && !limit && typeof cache === 'undefined') { + if (!before && !after && !limit && cache === undefined) { return Promise.reject(new DiscordjsError(ErrorCodes.FetchBanResolveId)); } @@ -156,7 +156,7 @@ class GuildBanManager extends CachedManager { const id = this.client.users.resolveId(user); if (!id) throw new DiscordjsError(ErrorCodes.BanResolveId, true); - if (typeof options.deleteMessageDays !== 'undefined' && !deprecationEmittedForDeleteMessageDays) { + if (options.deleteMessageDays !== undefined && !deprecationEmittedForDeleteMessageDays) { process.emitWarning( // eslint-disable-next-line max-len 'The deleteMessageDays option for GuildBanManager#create() is deprecated. Use the deleteMessageSeconds option instead.', diff --git a/packages/discord.js/src/managers/GuildChannelManager.js b/packages/discord.js/src/managers/GuildChannelManager.js index 6c27eea8c..a5fa1602e 100644 --- a/packages/discord.js/src/managers/GuildChannelManager.js +++ b/packages/discord.js/src/managers/GuildChannelManager.js @@ -275,7 +275,7 @@ class GuildChannelManager extends CachedManager { const parent = options.parent && this.client.channels.resolveId(options.parent); - if (typeof options.position !== 'undefined') { + if (options.position !== undefined) { await this.setPosition(channel, options.position, { position: options.position, reason: options.reason }); } @@ -440,7 +440,7 @@ class GuildChannelManager extends CachedManager { id: this.client.channels.resolveId(r.channel), position: r.position, lock_permissions: r.lockPermissions, - parent_id: typeof r.parent !== 'undefined' ? this.resolveId(r.parent) : undefined, + parent_id: r.parent !== undefined ? this.resolveId(r.parent) : undefined, })); await this.client.rest.patch(Routes.guildChannels(this.guild.id), { body: channelPositions }); diff --git a/packages/discord.js/src/managers/GuildManager.js b/packages/discord.js/src/managers/GuildManager.js index 9db8d2d10..1d2d4ba96 100644 --- a/packages/discord.js/src/managers/GuildManager.js +++ b/packages/discord.js/src/managers/GuildManager.js @@ -185,8 +185,7 @@ class GuildManager extends CachedManager { roles: roles.map(({ color, permissions, ...options }) => ({ ...options, color: color && resolveColor(color), - permissions: - typeof permissions === 'undefined' ? undefined : PermissionsBitField.resolve(permissions).toString(), + permissions: permissions === undefined ? undefined : PermissionsBitField.resolve(permissions).toString(), })), channels: channels.map( ({ @@ -205,8 +204,8 @@ class GuildManager extends CachedManager { video_quality_mode: videoQualityMode, permission_overwrites: permissionOverwrites?.map(({ allow, deny, ...permissionOverwriteOptions }) => ({ ...permissionOverwriteOptions, - allow: typeof allow === 'undefined' ? undefined : PermissionsBitField.resolve(allow).toString(), - deny: typeof deny === 'undefined' ? undefined : PermissionsBitField.resolve(deny).toString(), + allow: allow === undefined ? undefined : PermissionsBitField.resolve(allow).toString(), + deny: deny === undefined ? undefined : PermissionsBitField.resolve(deny).toString(), })), rate_limit_per_user: rateLimitPerUser, }), @@ -215,9 +214,7 @@ class GuildManager extends CachedManager { afk_timeout: afkTimeout, system_channel_id: systemChannelId, system_channel_flags: - typeof systemChannelFlags === 'undefined' - ? undefined - : SystemChannelFlagsBitField.resolve(systemChannelFlags), + systemChannelFlags === undefined ? undefined : SystemChannelFlagsBitField.resolve(systemChannelFlags), }, }); diff --git a/packages/discord.js/src/managers/GuildMemberManager.js b/packages/discord.js/src/managers/GuildMemberManager.js index beefd383d..a4cf09b5d 100644 --- a/packages/discord.js/src/managers/GuildMemberManager.js +++ b/packages/discord.js/src/managers/GuildMemberManager.js @@ -358,7 +358,7 @@ class GuildMemberManager extends CachedManager { } options.roles &&= options.roles.map(role => (role instanceof Role ? role.id : role)); - if (typeof options.communicationDisabledUntil !== 'undefined') { + if (options.communicationDisabledUntil !== undefined) { options.communication_disabled_until = // eslint-disable-next-line eqeqeq options.communicationDisabledUntil != null @@ -366,7 +366,7 @@ class GuildMemberManager extends CachedManager { : options.communicationDisabledUntil; } - if (typeof options.flags !== 'undefined') { + if (options.flags !== undefined) { options.flags = GuildMemberFlagsBitField.resolve(options.flags); } diff --git a/packages/discord.js/src/managers/GuildScheduledEventManager.js b/packages/discord.js/src/managers/GuildScheduledEventManager.js index 0867c1519..9071b60a7 100644 --- a/packages/discord.js/src/managers/GuildScheduledEventManager.js +++ b/packages/discord.js/src/managers/GuildScheduledEventManager.js @@ -85,12 +85,12 @@ class GuildScheduledEventManager extends CachedManager { let entity_metadata, channel_id; if (entityType === GuildScheduledEventEntityType.External) { - channel_id = typeof channel === 'undefined' ? channel : null; + channel_id = channel === undefined ? channel : null; entity_metadata = { location: entityMetadata?.location }; } else { channel_id = this.guild.channels.resolveId(channel); if (!channel_id) throw new DiscordjsError(ErrorCodes.GuildVoiceChannelResolve); - entity_metadata = typeof entityMetadata === 'undefined' ? entityMetadata : null; + entity_metadata = entityMetadata === undefined ? entityMetadata : null; } const data = await this.client.rest.post(Routes.guildScheduledEvents(this.guild.id), { @@ -214,7 +214,7 @@ class GuildScheduledEventManager extends CachedManager { const data = await this.client.rest.patch(Routes.guildScheduledEvent(this.guild.id, guildScheduledEventId), { body: { - channel_id: typeof channel === 'undefined' ? channel : this.guild.channels.resolveId(channel), + channel_id: channel === undefined ? channel : this.guild.channels.resolveId(channel), name, privacy_level: privacyLevel, scheduled_start_time: scheduledStartTime ? new Date(scheduledStartTime).toISOString() : undefined, diff --git a/packages/discord.js/src/managers/RoleManager.js b/packages/discord.js/src/managers/RoleManager.js index 4aad14697..87b9e5656 100644 --- a/packages/discord.js/src/managers/RoleManager.js +++ b/packages/discord.js/src/managers/RoleManager.js @@ -137,7 +137,7 @@ class RoleManager extends CachedManager { async create(options = {}) { let { name, color, hoist, permissions, position, mentionable, reason, icon, unicodeEmoji } = options; color &&= resolveColor(color); - if (typeof permissions !== 'undefined') permissions = new PermissionsBitField(permissions); + if (permissions !== undefined) permissions = new PermissionsBitField(permissions); if (icon) { const guildEmojiURL = this.guild.emojis.resolve(icon)?.url; icon = guildEmojiURL ? await DataResolver.resolveImage(guildEmojiURL) : await DataResolver.resolveImage(icon); @@ -198,10 +198,9 @@ class RoleManager extends CachedManager { const body = { name: options.name, - color: typeof options.color === 'undefined' ? undefined : resolveColor(options.color), + color: options.color === undefined ? undefined : resolveColor(options.color), hoist: options.hoist, - permissions: - typeof options.permissions === 'undefined' ? undefined : new PermissionsBitField(options.permissions), + permissions: options.permissions === undefined ? undefined : new PermissionsBitField(options.permissions), mentionable: options.mentionable, icon, unicode_emoji: options.unicodeEmoji, diff --git a/packages/discord.js/src/managers/ThreadManager.js b/packages/discord.js/src/managers/ThreadManager.js index 2ae78cc00..b3ab61230 100644 --- a/packages/discord.js/src/managers/ThreadManager.js +++ b/packages/discord.js/src/managers/ThreadManager.js @@ -147,7 +147,7 @@ class ThreadManager extends CachedManager { let timestamp; let id; const query = makeURLSearchParams({ limit }); - if (typeof before !== 'undefined') { + if (before !== undefined) { if (before instanceof ThreadChannel || /^\d{17,19}$/.test(String(before))) { id = this.resolveId(before); timestamp = this.resolve(before)?.archivedAt?.toISOString(); diff --git a/packages/discord.js/src/structures/ApplicationCommand.js b/packages/discord.js/src/structures/ApplicationCommand.js index e2337e0d2..949a08a68 100644 --- a/packages/discord.js/src/structures/ApplicationCommand.js +++ b/packages/discord.js/src/structures/ApplicationCommand.js @@ -389,7 +389,7 @@ class ApplicationCommand extends Base { // TODO: remove ?? 0 on each when nullable (command.options?.length ?? 0) !== (this.options?.length ?? 0) || defaultMemberPermissions !== (this.defaultMemberPermissions?.bitfield ?? null) || - (typeof dmPermission !== 'undefined' && dmPermission !== this.dmPermission) || + (dmPermission !== undefined && dmPermission !== this.dmPermission) || !isEqual(command.nameLocalizations ?? command.name_localizations ?? {}, this.nameLocalizations ?? {}) || !isEqual( command.descriptionLocalizations ?? command.description_localizations ?? {}, diff --git a/packages/discord.js/src/structures/ClientPresence.js b/packages/discord.js/src/structures/ClientPresence.js index e94238f97..5b448d786 100644 --- a/packages/discord.js/src/structures/ClientPresence.js +++ b/packages/discord.js/src/structures/ClientPresence.js @@ -21,7 +21,7 @@ class ClientPresence extends Presence { set(presence) { const packet = this._parse(presence); this._patch(packet); - if (typeof presence.shardId === 'undefined') { + if (presence.shardId === undefined) { this.client.ws.broadcast({ op: GatewayOpcodes.PresenceUpdate, d: packet }); } else if (Array.isArray(presence.shardId)) { for (const shardId of presence.shardId) { diff --git a/packages/discord.js/src/structures/ClientUser.js b/packages/discord.js/src/structures/ClientUser.js index ef82a79f5..8e854f16c 100644 --- a/packages/discord.js/src/structures/ClientUser.js +++ b/packages/discord.js/src/structures/ClientUser.js @@ -55,7 +55,7 @@ class ClientUser extends User { * @returns {Promise} */ async edit(options) { - if (typeof options.avatar !== 'undefined') options.avatar = await DataResolver.resolveImage(options.avatar); + if (options.avatar !== undefined) options.avatar = await DataResolver.resolveImage(options.avatar); const newData = await this.client.rest.patch(Routes.user(), { body: options }); this.client.token = newData.token; this.client.rest.setToken(newData.token); diff --git a/packages/discord.js/src/structures/CommandInteractionOptionResolver.js b/packages/discord.js/src/structures/CommandInteractionOptionResolver.js index 1e85412e2..621dbf44e 100644 --- a/packages/discord.js/src/structures/CommandInteractionOptionResolver.js +++ b/packages/discord.js/src/structures/CommandInteractionOptionResolver.js @@ -97,7 +97,7 @@ class CommandInteractionOptionResolver { return null; } else if (!allowedTypes.includes(option.type)) { throw new DiscordjsTypeError(ErrorCodes.CommandInteractionOptionType, name, option.type, allowedTypes.join(', ')); - } else if (required && properties.every(prop => option[prop] === null || typeof option[prop] === 'undefined')) { + } else if (required && properties.every(prop => option[prop] === null || option[prop] === undefined)) { throw new DiscordjsTypeError(ErrorCodes.CommandInteractionOptionEmpty, name, option.type); } return option; diff --git a/packages/discord.js/src/structures/DMChannel.js b/packages/discord.js/src/structures/DMChannel.js index b5452ca09..8c13f7550 100644 --- a/packages/discord.js/src/structures/DMChannel.js +++ b/packages/discord.js/src/structures/DMChannel.js @@ -68,7 +68,7 @@ class DMChannel extends BaseChannel { * @readonly */ get partial() { - return typeof this.lastMessageId === 'undefined'; + return this.lastMessageId === undefined; } /** diff --git a/packages/discord.js/src/structures/Guild.js b/packages/discord.js/src/structures/Guild.js index 594163d94..4799d5db3 100644 --- a/packages/discord.js/src/structures/Guild.js +++ b/packages/discord.js/src/structures/Guild.js @@ -820,9 +820,7 @@ class Guild extends AnonymousGuild { banner: banner && (await DataResolver.resolveImage(banner)), system_channel_id: systemChannel && this.client.channels.resolveId(systemChannel), system_channel_flags: - typeof systemChannelFlags === 'undefined' - ? undefined - : SystemChannelFlagsBitField.resolve(systemChannelFlags), + systemChannelFlags === undefined ? undefined : SystemChannelFlagsBitField.resolve(systemChannelFlags), rules_channel_id: rulesChannel && this.client.channels.resolveId(rulesChannel), public_updates_channel_id: publicUpdatesChannel && this.client.channels.resolveId(publicUpdatesChannel), preferred_locale: preferredLocale, diff --git a/packages/discord.js/src/structures/GuildChannel.js b/packages/discord.js/src/structures/GuildChannel.js index b5177d289..ab9e5c90b 100644 --- a/packages/discord.js/src/structures/GuildChannel.js +++ b/packages/discord.js/src/structures/GuildChannel.js @@ -131,8 +131,8 @@ class GuildChannel extends BaseChannel { // Compare overwrites return ( - typeof channelVal !== 'undefined' && - typeof parentVal !== 'undefined' && + channelVal !== undefined && + parentVal !== undefined && channelVal.deny.bitfield === parentVal.deny.bitfield && channelVal.allow.bitfield === parentVal.allow.bitfield ); diff --git a/packages/discord.js/src/structures/InviteGuild.js b/packages/discord.js/src/structures/InviteGuild.js index ab1aed492..8efd9805e 100644 --- a/packages/discord.js/src/structures/InviteGuild.js +++ b/packages/discord.js/src/structures/InviteGuild.js @@ -15,8 +15,7 @@ class InviteGuild extends AnonymousGuild { * The welcome screen for this invite guild * @type {?WelcomeScreen} */ - this.welcomeScreen = - typeof data.welcome_screen !== 'undefined' ? new WelcomeScreen(this, data.welcome_screen) : null; + this.welcomeScreen = data.welcome_screen !== undefined ? new WelcomeScreen(this, data.welcome_screen) : null; } } diff --git a/packages/discord.js/src/structures/MessagePayload.js b/packages/discord.js/src/structures/MessagePayload.js index 57a3a7139..6bcac122a 100644 --- a/packages/discord.js/src/structures/MessagePayload.js +++ b/packages/discord.js/src/structures/MessagePayload.js @@ -107,7 +107,7 @@ class MessagePayload { let content; if (this.options.content === null) { content = ''; - } else if (typeof this.options.content !== 'undefined') { + } else if (this.options.content !== undefined) { content = verifyString(this.options.content, DiscordjsRangeError, ErrorCodes.MessageContentType, true); } @@ -127,7 +127,7 @@ class MessagePayload { const tts = Boolean(this.options.tts); let nonce; - if (typeof this.options.nonce !== 'undefined') { + if (this.options.nonce !== undefined) { nonce = this.options.nonce; if (typeof nonce === 'number' ? !Number.isInteger(nonce) : typeof nonce !== 'string') { throw new DiscordjsRangeError(ErrorCodes.MessageNonceType); @@ -147,8 +147,8 @@ class MessagePayload { let flags; if ( - typeof this.options.flags !== 'undefined' || - (this.isMessage && typeof this.options.reply === 'undefined') || + this.options.flags !== undefined || + (this.isMessage && this.options.reply === undefined) || this.isMessageManager ) { flags = @@ -163,11 +163,11 @@ class MessagePayload { } let allowedMentions = - typeof this.options.allowedMentions === 'undefined' + this.options.allowedMentions === undefined ? this.target.client.options.allowedMentions : this.options.allowedMentions; - if (typeof allowedMentions?.repliedUser !== 'undefined') { + if (allowedMentions?.repliedUser !== undefined) { allowedMentions = { ...allowedMentions, replied_user: allowedMentions.repliedUser }; delete allowedMentions.repliedUser; } @@ -204,8 +204,7 @@ class MessagePayload { components, username, avatar_url: avatarURL, - allowed_mentions: - typeof content === 'undefined' && typeof message_reference === 'undefined' ? undefined : allowedMentions, + allowed_mentions: content === undefined && message_reference === undefined ? undefined : allowedMentions, flags, message_reference, attachments: this.options.attachments, diff --git a/packages/discord.js/src/structures/VoiceState.js b/packages/discord.js/src/structures/VoiceState.js index 6a0cb56bb..ae510f2cf 100644 --- a/packages/discord.js/src/structures/VoiceState.js +++ b/packages/discord.js/src/structures/VoiceState.js @@ -224,7 +224,7 @@ class VoiceState extends Base { const target = this.client.user.id === this.id ? '@me' : this.id; - if (target !== '@me' && typeof options.requestToSpeak !== 'undefined') { + if (target !== '@me' && options.requestToSpeak !== undefined) { throw new DiscordjsError(ErrorCodes.VoiceStateNotOwn); } diff --git a/packages/discord.js/src/util/BitField.js b/packages/discord.js/src/util/BitField.js index 76e7aefb7..1edd66329 100644 --- a/packages/discord.js/src/util/BitField.js +++ b/packages/discord.js/src/util/BitField.js @@ -164,7 +164,7 @@ class BitField { 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 (typeof bit === 'string') { - if (typeof this.Flags[bit] !== 'undefined') return this.Flags[bit]; + if (this.Flags[bit] !== undefined) return this.Flags[bit]; if (!isNaN(bit)) return typeof DefaultBit === 'bigint' ? BigInt(bit) : Number(bit); } throw new DiscordjsRangeError(ErrorCodes.BitFieldInvalid, bit); diff --git a/packages/docgen/src/types/param.ts b/packages/docgen/src/types/param.ts index 8c64148cc..9994bbb36 100644 --- a/packages/docgen/src/types/param.ts +++ b/packages/docgen/src/types/param.ts @@ -13,7 +13,7 @@ export class DocumentedParam extends DocumentedItem name: data.name, // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing, no-param-reassign description: data.comment?.summary?.reduce((prev, curr) => (prev += curr.text), '').trim() || undefined, - optional: data.flags.isOptional || typeof data.defaultValue !== 'undefined', + optional: data.flags.isOptional || data.defaultValue !== undefined, default: (data.defaultValue === '...' ? undefined : data.defaultValue) ?? (data.comment?.blockTags diff --git a/packages/docgen/src/types/typedef.ts b/packages/docgen/src/types/typedef.ts index e41f9871e..4c6e75e9d 100644 --- a/packages/docgen/src/types/typedef.ts +++ b/packages/docgen/src/types/typedef.ts @@ -86,7 +86,7 @@ export class DocumentedTypeDef extends DocumentedItem (prev += curr.text), '').trim() || undefined, - optional: child.flags.isOptional || typeof child.defaultValue !== 'undefined', + optional: child.flags.isOptional || child.defaultValue !== undefined, default: (child.defaultValue === '...' ? undefined : child.defaultValue) ?? (child.comment?.blockTags @@ -131,7 +131,7 @@ export class DocumentedTypeDef extends DocumentedItem (prev += curr.text), '').trim() || undefined, - optional: param.flags.isOptional || typeof param.defaultValue !== 'undefined', + optional: param.flags.isOptional || param.defaultValue !== undefined, default: (param.defaultValue === '...' ? undefined : param.defaultValue) ?? (param.comment?.blockTags diff --git a/packages/formatters/src/formatters.ts b/packages/formatters/src/formatters.ts index cd830764d..8b253a977 100644 --- a/packages/formatters/src/formatters.ts +++ b/packages/formatters/src/formatters.ts @@ -16,7 +16,7 @@ export function codeBlock(content: C): `\`\`\`\n${C}\n\`\`\``; */ export function codeBlock(language: L, content: C): `\`\`\`${L}\n${C}\n\`\`\``; export function codeBlock(language: string, content?: string): string { - return typeof content === 'undefined' ? `\`\`\`\n${language}\n\`\`\`` : `\`\`\`${language}\n${content}\n\`\`\``; + return content === undefined ? `\`\`\`\n${language}\n\`\`\`` : `\`\`\`${language}\n${content}\n\`\`\``; } /** @@ -238,11 +238,11 @@ export function chatInputApplicationCommandMention< subcommandName?: S, commandId?: I, ): `` | `` | `` { - if (typeof commandId !== 'undefined') { + if (commandId !== undefined) { return ``; } - if (typeof subcommandName !== 'undefined') { + if (subcommandName !== undefined) { return ``; } @@ -336,7 +336,7 @@ export function messageLink( // string inputs can only be used with FFmpeg if (typeof input === 'string') { inputType = StreamType.Arbitrary; - } else if (typeof inputType === 'undefined') { + } else if (inputType === undefined) { const analysis = inferStreamType(input); inputType = analysis.streamType; needsInlineVolume = needsInlineVolume && !analysis.hasVolume; diff --git a/packages/voice/src/networking/Networking.ts b/packages/voice/src/networking/Networking.ts index 7ed779b1d..50b0fe5c5 100644 --- a/packages/voice/src/networking/Networking.ts +++ b/packages/voice/src/networking/Networking.ts @@ -498,7 +498,7 @@ export class Networking extends EventEmitter { public dispatchAudio() { const state = this.state; if (state.code !== NetworkingStatusCode.Ready) return false; - if (typeof state.preparedPacket !== 'undefined') { + if (state.preparedPacket !== undefined) { this.playAudioPacket(state.preparedPacket); state.preparedPacket = undefined; return true; diff --git a/packages/voice/src/networking/VoiceWebSocket.ts b/packages/voice/src/networking/VoiceWebSocket.ts index 16ee47ff4..6696acab5 100644 --- a/packages/voice/src/networking/VoiceWebSocket.ts +++ b/packages/voice/src/networking/VoiceWebSocket.ts @@ -161,7 +161,7 @@ export class VoiceWebSocket extends EventEmitter { * @param ms - The interval in milliseconds. If negative, the interval will be unset */ public setHeartbeatInterval(ms: number) { - if (typeof this.heartbeatInterval !== 'undefined') clearInterval(this.heartbeatInterval); + if (this.heartbeatInterval !== undefined) clearInterval(this.heartbeatInterval); if (ms > 0) { this.heartbeatInterval = setInterval(() => { if (this.lastHeartbeatSend !== 0 && this.missedHeartbeats >= 3) { diff --git a/packages/voice/src/receive/AudioReceiveStream.ts b/packages/voice/src/receive/AudioReceiveStream.ts index 105eaa8db..119903844 100644 --- a/packages/voice/src/receive/AudioReceiveStream.ts +++ b/packages/voice/src/receive/AudioReceiveStream.ts @@ -69,7 +69,7 @@ export class AudioReceiveStream extends Readable { buffer && (this.end.behavior === EndBehaviorType.AfterInactivity || (this.end.behavior === EndBehaviorType.AfterSilence && - (buffer.compare(SILENCE_FRAME) !== 0 || typeof this.endTimeout === 'undefined'))) + (buffer.compare(SILENCE_FRAME) !== 0 || this.endTimeout === undefined))) ) { this.renewEndTimeout(this.end); }