diff --git a/src/client/actions/ActionsManager.js b/src/client/actions/ActionsManager.js index d5cf61ed2..8341e7453 100644 --- a/src/client/actions/ActionsManager.js +++ b/src/client/actions/ActionsManager.js @@ -27,6 +27,7 @@ class ActionsManager { this.register(require('./GuildEmojiCreate')); this.register(require('./GuildEmojiDelete')); this.register(require('./GuildEmojiUpdate')); + this.register(require('./GuildEmojisUpdate')); this.register(require('./GuildRolesPositionUpdate')); this.register(require('./GuildChannelsPositionUpdate')); } diff --git a/src/client/actions/GuildEmojisUpdate.js b/src/client/actions/GuildEmojisUpdate.js new file mode 100644 index 000000000..8656a34cd --- /dev/null +++ b/src/client/actions/GuildEmojisUpdate.js @@ -0,0 +1,38 @@ +const Action = require('./Action'); + +function mappify(iterable) { + const map = new Map(); + for (const x of iterable) map.set(...x); + return map; +} + +class GuildEmojisUpdateAction extends Action { + handle(data) { + const guild = this.client.guilds.get(data.guild_id); + if (!guild || !guild.emojis) return; + + const deletions = mappify(guild.emojis.entries()); + + for (const emoji of data.emojis) { + // Determine type of emoji event + const cachedEmoji = guild.emojis.get(emoji.id); + if (cachedEmoji) { + deletions.delete(emoji.id); + if (!cachedEmoji.equals(emoji, true)) { + // Emoji updated + this.client.actions.GuildEmojiUpdate.handle(cachedEmoji, emoji); + } + } else { + // Emoji added + this.client.actions.GuildEmojiCreate.handle(guild, emoji); + } + } + + for (const emoji of deletions.values()) { + // Emoji deleted + this.client.actions.GuildEmojiDelete.handle(emoji); + } + } +} + +module.exports = GuildEmojisUpdateAction; diff --git a/src/client/websocket/packets/handlers/GuildEmojisUpdate.js b/src/client/websocket/packets/handlers/GuildEmojisUpdate.js index cf8522b54..9b1f59e50 100644 --- a/src/client/websocket/packets/handlers/GuildEmojisUpdate.js +++ b/src/client/websocket/packets/handlers/GuildEmojisUpdate.js @@ -1,39 +1,10 @@ const AbstractHandler = require('./AbstractHandler'); -function mappify(iterable) { - const map = new Map(); - for (const x of iterable) map.set(...x); - return map; -} - class GuildEmojisUpdate extends AbstractHandler { handle(packet) { const client = this.packetManager.client; const data = packet.d; - const guild = client.guilds.get(data.guild_id); - if (!guild || !guild.emojis) return; - - const deletions = mappify(guild.emojis.entries()); - - for (const emoji of data.emojis) { - // Determine type of emoji event - const cachedEmoji = guild.emojis.get(emoji.id); - if (cachedEmoji) { - deletions.delete(emoji.id); - if (!cachedEmoji.equals(emoji, true)) { - // Emoji updated - client.actions.GuildEmojiUpdate.handle(cachedEmoji, emoji); - } - } else { - // Emoji added - client.actions.GuildEmojiCreate.handle(guild, emoji); - } - } - - for (const emoji of deletions.values()) { - // Emoji deleted - client.actions.GuildEmojiDelete.handle(emoji); - } + client.actions.GuildEmojisUpdate.handle(data); } } diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 46ea9ea1f..eb3cd6e44 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -121,13 +121,6 @@ class Guild { */ this.applicationID = data.application_id; - /** - * A collection of emojis that are in this guild. The key is the emoji's ID, the value is the emoji. - * @type {Collection} - */ - this.emojis = new Collection(); - for (const emoji of data.emojis) this.emojis.set(emoji.id, new Emoji(this, emoji)); - /** * The time in seconds before a user is counted as "away from keyboard". * @type {?number} @@ -216,6 +209,20 @@ class Guild { } } } + + if (!this.emojis) { + /** + * A collection of emojis that are in this guild. The key is the emoji's ID, the value is the emoji. + * @type {Collection} + */ + this.emojis = new Collection(); + for (const emoji of data.emojis) this.emojis.set(emoji.id, new Emoji(this, emoji)); + } else { + this.client.actions.GuildEmojisUpdate.handle({ + guild_id: this.id, + emojis: data.emojis, + }); + } } /** diff --git a/test/random.js b/test/random.js index 0a4a2e071..5ba989038 100644 --- a/test/random.js +++ b/test/random.js @@ -18,9 +18,9 @@ client.on('userUpdate', (o, n) => { console.log(o.username, n.username); }); -client.on('guildEmojiCreate', e => console.log('create!!', e.name)); -client.on('guildEmojiDelete', e => console.log('delete!!', e.name)); -client.on('guildEmojiUpdate', (o, n) => console.log('update!!', o.name, n.name)); +client.on('emojiCreate', e => console.log('create!!', e.name)); +client.on('emojiDelete', e => console.log('delete!!', e.name)); +client.on('emojiUpdate', (o, n) => console.log('update!!', o.name, n.name)); client.on('guildMemberAdd', m => console.log(`${m.user.username} joined ${m.guild.name}`));