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

@@ -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);
}