refactor: new node features (#5132)

Co-authored-by: Antonio Román <kyradiscord@gmail.com>
This commit is contained in:
Sugden
2021-06-30 21:40:33 +01:00
committed by GitHub
parent f108746c15
commit 1e8f01253e
68 changed files with 305 additions and 360 deletions

View File

@@ -34,14 +34,7 @@ class Client extends BaseClient {
constructor(options) {
super(Object.assign({ _tokenType: 'Bot' }, options));
// Obtain shard details from environment or if present, worker threads
let data = process.env;
try {
// Test if worker threads module is present and used
data = require('worker_threads').workerData || data;
} catch {
// Do nothing
}
const data = require('worker_threads').workerData ?? process.env;
if (this.options.shards === DefaultOptions.shards) {
if ('SHARDS' in data) {
@@ -188,7 +181,7 @@ class Client extends BaseClient {
* @readonly
*/
get readyTimestamp() {
return this.readyAt ? this.readyAt.getTime() : null;
return this.readyAt?.getTime() ?? null;
}
/**
@@ -341,7 +334,7 @@ class Client extends BaseClient {
channels++;
messages += channel.messages.cache.sweep(
message => now - (message.editedTimestamp || message.createdTimestamp) > lifetimeMs,
message => now - (message.editedTimestamp ?? message.createdTimestamp) > lifetimeMs,
);
}

View File

@@ -32,14 +32,14 @@ class GenericAction {
}
getChannel(data) {
const id = data.channel_id || data.id;
const id = data.channel_id ?? data.id;
return (
data.channel ||
data.channel ??
this.getPayload(
{
id,
guild_id: data.guild_id,
recipients: [data.author || { id: data.user_id }],
recipients: [data.author ?? { id: data.user_id }],
},
this.client.channels,
id,
@@ -49,14 +49,14 @@ class GenericAction {
}
getMessage(data, channel, cache) {
const id = data.message_id || data.id;
const id = data.message_id ?? data.id;
return (
data.message ||
data.message ??
this.getPayload(
{
id,
channel_id: channel.id,
guild_id: data.guild_id || (channel.guild ? channel.guild.id : null),
guild_id: data.guild_id ?? channel.guild?.id,
},
channel.messages,
id,
@@ -67,12 +67,12 @@ class GenericAction {
}
getReaction(data, message, user) {
const id = data.emoji.id || decodeURIComponent(data.emoji.name);
const id = data.emoji.id ?? decodeURIComponent(data.emoji.name);
return this.getPayload(
{
emoji: data.emoji,
count: message.partial ? null : 0,
me: user ? user.id === this.client.user.id : false,
me: user?.id === this.client.user.id,
},
message.reactions,
id,
@@ -86,11 +86,11 @@ class GenericAction {
getUser(data) {
const id = data.user_id;
return data.user || this.getPayload({ id }, this.client.users, id, PartialTypes.USER);
return data.user ?? this.getPayload({ id }, this.client.users, id, PartialTypes.USER);
}
getUserFromMember(data) {
if (data.guild_id && data.member && data.member.user) {
if (data.guild_id && data.member?.user) {
const guild = this.client.guilds.cache.get(data.guild_id);
if (guild) {
return guild.members.add(data.member).user;

View File

@@ -12,7 +12,7 @@ class ChannelDeleteAction extends Action {
handle(data) {
const client = this.client;
let channel = client.channels.cache.get(data.id);
const channel = client.channels.cache.get(data.id);
if (channel) {
client.channels.remove(channel.id);

View File

@@ -53,7 +53,7 @@ class GuildDeleteAction extends Action {
this.deleted.set(guild.id, guild);
this.scheduleForDeletion(guild.id);
} else {
guild = this.deleted.get(data.id) || null;
guild = this.deleted.get(data.id) ?? null;
}
return { guild };

View File

@@ -5,7 +5,7 @@ const Action = require('./Action');
class GuildEmojisUpdateAction extends Action {
handle(data) {
const guild = this.client.guilds.cache.get(data.guild_id);
if (!guild || !guild.emojis) return;
if (!guild?.emojis) return;
const deletions = new Map(guild.emojis.cache);

View File

@@ -12,7 +12,7 @@ class MessageCreateAction extends Action {
if (existing) return { message: existing };
const message = channel.messages.add(data);
const user = message.author;
let member = message.member;
const member = message.member;
channel.lastMessageID = data.id;
if (user) {
user.lastMessageID = data.id;

View File

@@ -31,8 +31,8 @@ class MessageReactionAdd extends Action {
// Verify reaction
if (message.partial && !this.client.options.partials.includes(PartialTypes.REACTION)) return false;
const existing = message.reactions.cache.get(data.emoji.id || data.emoji.name);
if (existing && existing.users.cache.has(user.id)) return { message, reaction: existing, user };
const existing = message.reactions.cache.get(data.emoji.id ?? data.emoji.name);
if (existing?.users.cache.has(user.id)) return { message, reaction: existing, user };
const reaction = message.reactions.add({
emoji: data.emoji,
count: message.partial ? null : 0,

View File

@@ -13,7 +13,7 @@ class MessageReactionRemoveEmoji extends Action {
const reaction = this.getReaction(data, message);
if (!reaction) return false;
if (!message.partial) message.reactions.cache.delete(reaction.emoji.id || reaction.emoji.name);
if (!message.partial) message.reactions.cache.delete(reaction.emoji.id ?? reaction.emoji.name);
/**
* Emitted when a bot removes an emoji reaction from a cached message.

View File

@@ -6,18 +6,17 @@ const { Events } = require('../../util/Constants');
class PresenceUpdateAction extends Action {
handle(data) {
let user = this.client.users.cache.get(data.user.id);
if (!user && data.user.username) user = this.client.users.add(data.user);
if (!user && data.user?.username) user = this.client.users.add(data.user);
if (!user) return;
if (data.user && data.user.username) {
if (data.user?.username) {
if (!user.equals(data.user)) this.client.actions.UserUpdate.handle(data.user);
}
const guild = this.client.guilds.cache.get(data.guild_id);
if (!guild) return;
let oldPresence = guild.presences.cache.get(user.id);
if (oldPresence) oldPresence = oldPresence._clone();
const oldPresence = guild.presences.cache.get(user.id)?._clone();
let member = guild.members.cache.get(user.id);
if (!member && data.status !== 'offline') {
member = guild.members.add({
@@ -28,7 +27,7 @@ class PresenceUpdateAction extends Action {
this.client.emit(Events.GUILD_MEMBER_AVAILABLE, member);
}
guild.presences.add(Object.assign(data, { guild }));
if (member && this.client.listenerCount(Events.PRESENCE_UPDATE) && !member.presence.equals(oldPresence)) {
if (this.client.listenerCount(Events.PRESENCE_UPDATE) && member && !member.presence.equals(oldPresence)) {
/**
* Emitted whenever a guild member's presence (e.g. status, activity) is changed.
* @event Client#presenceUpdate

View File

@@ -11,9 +11,8 @@ class VoiceStateUpdate extends Action {
if (guild) {
const VoiceState = Structures.get('VoiceState');
// Update the state
const oldState = guild.voiceStates.cache.has(data.user_id)
? guild.voiceStates.cache.get(data.user_id)._clone()
: new VoiceState(guild, { user_id: data.user_id });
const oldState =
guild.voiceStates.cache.get(data.user_id)?._clone() ?? new VoiceState(guild, { user_id: data.user_id });
const newState = guild.voiceStates.add(data);
@@ -21,12 +20,12 @@ class VoiceStateUpdate extends Action {
let member = guild.members.cache.get(data.user_id);
if (member && data.member) {
member._patch(data.member);
} else if (data.member && data.member.user && data.member.joined_at) {
} else if (data.member?.user && data.member.joined_at) {
member = guild.members.add(data.member);
}
// Emit event
if (member && member.user.id === client.user.id) {
if (member?.user.id === client.user.id) {
client.emit('debug', `[VOICE] received voice state update: ${JSON.stringify(data)}`);
client.voice.onVoiceStateUpdate(data);
}

View File

@@ -240,7 +240,7 @@ class WebSocketManager extends EventEmitter {
try {
await shard.connect();
} catch (error) {
if (error && error.code && UNRECOVERABLE_CLOSE_CODES.includes(error.code)) {
if (error?.code && UNRECOVERABLE_CLOSE_CODES.includes(error.code)) {
throw new Error(WSCodes[error.code]);
// Undefined if session is invalid, error event for regular closes
} else if (!error || error.code) {

View File

@@ -176,7 +176,7 @@ class WebSocketShard extends EventEmitter {
connect() {
const { gateway, client } = this.manager;
if (this.connection && this.connection.readyState === WebSocket.OPEN && this.status === Status.READY) {
if (this.connection?.readyState === WebSocket.OPEN && this.status === Status.READY) {
return Promise.resolve();
}
@@ -216,7 +216,7 @@ class WebSocketShard extends EventEmitter {
this.once(ShardEvents.INVALID_SESSION, onInvalidOrDestroyed);
this.once(ShardEvents.DESTROYED, onInvalidOrDestroyed);
if (this.connection && this.connection.readyState === WebSocket.OPEN) {
if (this.connection?.readyState === WebSocket.OPEN) {
this.debug('An open connection was found, attempting an immediate identify.');
this.identify();
return;
@@ -306,7 +306,7 @@ class WebSocketShard extends EventEmitter {
* @private
*/
onError(event) {
const error = event && event.error ? event.error : event;
const error = event?.error ?? event;
if (!error) return;
/**
@@ -345,7 +345,7 @@ class WebSocketShard extends EventEmitter {
this.debug(`[CLOSE]
Event Code: ${event.code}
Clean : ${event.wasClean}
Reason : ${event.reason || 'No reason received'}`);
Reason : ${event.reason ?? 'No reason received'}`);
this.setHeartbeatTimer(-1);
this.setHelloTimeout(-1);
@@ -648,7 +648,7 @@ class WebSocketShard extends EventEmitter {
* @private
*/
_send(data) {
if (!this.connection || this.connection.readyState !== WebSocket.OPEN) {
if (this.connection?.readyState !== WebSocket.OPEN) {
this.debug(`Tried to send packet '${JSON.stringify(data)}' but no WebSocket is available!`);
this.destroy({ closeCode: 4000 });
return;

View File

@@ -8,7 +8,7 @@ module.exports = (client, { d: data }) => {
if (channel && !Number.isNaN(time.getTime())) {
// Discord sends null for last_pin_timestamp if the last pinned message was removed
channel.lastPinTimestamp = time.getTime() || null;
channel.lastPinTimestamp = time.getTime() ?? null;
/**
* Emitted whenever the pins of a channel are updated. Due to the nature of the WebSocket event,