From 2e940e635dc901abf8b685ea5f570cea8adb3cdd Mon Sep 17 00:00:00 2001 From: SpaceEEC Date: Fri, 20 Nov 2020 16:20:47 +0100 Subject: [PATCH] fix(GuildMemberUpdate): cache incoming members & use partials if enabled (#4986) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com> --- src/client/actions/ActionsManager.js | 1 + src/client/actions/GuildMemberUpdate.js | 44 +++++++++++++++++++ .../websocket/handlers/GUILD_MEMBER_UPDATE.js | 28 +----------- 3 files changed, 47 insertions(+), 26 deletions(-) create mode 100644 src/client/actions/GuildMemberUpdate.js diff --git a/src/client/actions/ActionsManager.js b/src/client/actions/ActionsManager.js index a5193c932f16..4055795aa92c 100644 --- a/src/client/actions/ActionsManager.js +++ b/src/client/actions/ActionsManager.js @@ -20,6 +20,7 @@ class ActionsManager { this.register(require('./InviteCreate')); this.register(require('./InviteDelete')); this.register(require('./GuildMemberRemove')); + this.register(require('./GuildMemberUpdate')); this.register(require('./GuildBanRemove')); this.register(require('./GuildRoleCreate')); this.register(require('./GuildRoleDelete')); diff --git a/src/client/actions/GuildMemberUpdate.js b/src/client/actions/GuildMemberUpdate.js new file mode 100644 index 000000000000..53ca4818b552 --- /dev/null +++ b/src/client/actions/GuildMemberUpdate.js @@ -0,0 +1,44 @@ +'use strict'; + +const Action = require('./Action'); +const { Status, Events } = require('../../util/Constants'); + +class GuildMemberUpdateAction extends Action { + handle(data, shard) { + const { client } = this; + if (data.user.username) { + const user = client.users.cache.get(data.user.id); + if (!user) { + client.users.add(data.user); + } else if (!user.equals(data.user)) { + client.actions.UserUpdate.handle(data.user); + } + } + + const guild = client.guilds.cache.get(data.guild_id); + if (guild) { + const member = this.getMember({ user: data.user }, guild); + if (member) { + const old = member._update(data); + /** + * Emitted whenever a guild member changes - i.e. new role, removed role, nickname. + * Also emitted when the user's details (e.g. username) change. + * @event Client#guildMemberUpdate + * @param {GuildMember} oldMember The member before the update + * @param {GuildMember} newMember The member after the update + */ + if (shard.status === Status.READY) client.emit(Events.GUILD_MEMBER_UPDATE, old, member); + } else { + const newMember = guild.members.add(data); + /** + * Emitted whenever a member becomes available in a large guild. + * @event Client#guildMemberAvailable + * @param {GuildMember} member The member that became available + */ + this.client.emit(Events.GUILD_MEMBER_AVAILABLE, newMember); + } + } + } +} + +module.exports = GuildMemberUpdateAction; diff --git a/src/client/websocket/handlers/GUILD_MEMBER_UPDATE.js b/src/client/websocket/handlers/GUILD_MEMBER_UPDATE.js index 4c8d259c2963..cafc6bd59a39 100644 --- a/src/client/websocket/handlers/GUILD_MEMBER_UPDATE.js +++ b/src/client/websocket/handlers/GUILD_MEMBER_UPDATE.js @@ -1,29 +1,5 @@ 'use strict'; -const { Status, Events } = require('../../../util/Constants'); - -module.exports = (client, { d: data }, shard) => { - let user = client.users.cache.get(data.user.id); - if (!user && data.user.username) user = client.users.add(data.user); - if (user && data.user && data.user.username) { - if (!user.equals(data.user)) client.actions.UserUpdate.handle(data.user); - } - - const guild = client.guilds.cache.get(data.guild_id); - if (guild) { - const member = guild.members.cache.get(data.user.id); - if (member) { - const old = member._update(data); - if (shard.status === Status.READY) { - /** - * Emitted whenever a guild member changes - i.e. new role, removed role, nickname. - * Also emitted when the user's details (e.g. username) change. - * @event Client#guildMemberUpdate - * @param {GuildMember} oldMember The member before the update - * @param {GuildMember} newMember The member after the update - */ - client.emit(Events.GUILD_MEMBER_UPDATE, old, member); - } - } - } +module.exports = (client, packet, shard) => { + client.actions.GuildMemberUpdate.handle(packet.d, shard); };