Add functionality for GuildEmoji events

This commit is contained in:
Amish Shah
2016-12-26 19:21:00 +00:00
parent 2410fdf8d2
commit cd657be8be
10 changed files with 80 additions and 40 deletions

View File

@@ -95,7 +95,7 @@ class ClientDataManager {
const already = guild.emojis.has(data.id); const already = guild.emojis.has(data.id);
if (data && !already) { if (data && !already) {
let emoji = new Emoji(guild, data); let emoji = new Emoji(guild, data);
this.client.emit(Constants.Events.EMOJI_CREATE, emoji); this.client.emit(Constants.Events.GUILD_EMOJI_CREATE, emoji);
guild.emojis.set(emoji.id, emoji); guild.emojis.set(emoji.id, emoji);
return emoji; return emoji;
} else if (already) { } else if (already) {
@@ -107,7 +107,7 @@ class ClientDataManager {
killEmoji(emoji) { killEmoji(emoji) {
if (!(emoji instanceof Emoji && emoji.guild)) return; if (!(emoji instanceof Emoji && emoji.guild)) return;
this.client.emit(Constants.Events.EMOJI_DELETE, emoji); this.client.emit(Constants.Events.GUILD_EMOJI_DELETE, emoji);
emoji.guild.emojis.delete(emoji.id); emoji.guild.emojis.delete(emoji.id);
} }

View File

@@ -1,9 +1,9 @@
const Action = require('./Action'); const Action = require('./Action');
class EmojiCreateAction extends Action { class GuildEmojiCreateAction extends Action {
handle(data, guild) { handle(guild, createdEmoji) {
const client = this.client; const client = this.client;
const emoji = client.dataManager.newEmoji(data, guild); const emoji = client.dataManager.newEmoji(createdEmoji, guild);
return { return {
emoji, emoji,
}; };
@@ -15,4 +15,4 @@ class EmojiCreateAction extends Action {
* @event Client#guildEmojiCreate * @event Client#guildEmojiCreate
* @param {Emoji} emoji The emoji that was created. * @param {Emoji} emoji The emoji that was created.
*/ */
module.exports = EmojiCreateAction; module.exports = GuildEmojiCreateAction;

View File

@@ -1,11 +1,11 @@
const Action = require('./Action'); const Action = require('./Action');
class EmojiDeleteAction extends Action { class GuildEmojiDeleteAction extends Action {
handle(data) { handle(emoji) {
const client = this.client; const client = this.client;
client.dataManager.killEmoji(data); client.dataManager.killEmoji(emoji);
return { return {
data, emoji,
}; };
} }
} }
@@ -15,4 +15,4 @@ class EmojiDeleteAction extends Action {
* @event Client#guildEmojiDelete * @event Client#guildEmojiDelete
* @param {Emoji} emoji The emoji that was deleted. * @param {Emoji} emoji The emoji that was deleted.
*/ */
module.exports = EmojiDeleteAction; module.exports = GuildEmojiDeleteAction;

View File

@@ -1,22 +1,8 @@
const Action = require('./Action'); const Action = require('./Action');
class GuildEmojiUpdateAction extends Action { class GuildEmojiUpdateAction extends Action {
handle(data, guild) { handle(oldEmoji, newEmoji) {
const client = this.client; this.client.dataManager.updateEmoji(oldEmoji, newEmoji);
for (let emoji of data.emojis) {
const already = guild.emojis.has(emoji.id);
if (already) {
client.dataManager.updateEmoji(guild.emojis.get(emoji.id), emoji);
} else {
emoji = client.dataManager.newEmoji(emoji, guild);
}
}
for (let emoji of guild.emojis) {
if (!data.emoijs.has(emoji.id)) client.dataManager.killEmoji(emoji);
}
return {
emojis: data.emojis,
};
} }
} }

View File

@@ -27,6 +27,7 @@ class WebSocketPacketManager {
this.register(Constants.WSEvents.GUILD_ROLE_CREATE, require('./handlers/GuildRoleCreate')); this.register(Constants.WSEvents.GUILD_ROLE_CREATE, require('./handlers/GuildRoleCreate'));
this.register(Constants.WSEvents.GUILD_ROLE_DELETE, require('./handlers/GuildRoleDelete')); this.register(Constants.WSEvents.GUILD_ROLE_DELETE, require('./handlers/GuildRoleDelete'));
this.register(Constants.WSEvents.GUILD_ROLE_UPDATE, require('./handlers/GuildRoleUpdate')); this.register(Constants.WSEvents.GUILD_ROLE_UPDATE, require('./handlers/GuildRoleUpdate'));
this.register(Constants.WSEvents.GUILD_EMOJIS_UPDATE, require('./handlers/GuildEmojisUpdate'));
this.register(Constants.WSEvents.GUILD_MEMBERS_CHUNK, require('./handlers/GuildMembersChunk')); this.register(Constants.WSEvents.GUILD_MEMBERS_CHUNK, require('./handlers/GuildMembersChunk'));
this.register(Constants.WSEvents.CHANNEL_CREATE, require('./handlers/ChannelCreate')); this.register(Constants.WSEvents.CHANNEL_CREATE, require('./handlers/ChannelCreate'));
this.register(Constants.WSEvents.CHANNEL_DELETE, require('./handlers/ChannelDelete')); this.register(Constants.WSEvents.CHANNEL_DELETE, require('./handlers/ChannelDelete'));

View File

@@ -1,13 +0,0 @@
const AbstractHandler = require('./AbstractHandler');
class GuildEmojiUpdate extends AbstractHandler {
handle(packet) {
const client = this.packetManager.client;
const data = packet.d;
const guild = client.guilds.get(data.guild_id);
if (!guild) return;
client.actions.EmojiUpdate.handle(data, guild);
}
}
module.exports = GuildEmojiUpdate;

View File

@@ -0,0 +1,40 @@
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);
}
}
}
module.exports = GuildEmojisUpdate;

View File

@@ -103,6 +103,27 @@ class Emoji {
return this.requiresColons ? `<:${this.name}:${this.id}>` : this.name; return this.requiresColons ? `<:${this.name}:${this.id}>` : this.name;
} }
/**
* Whether this emoji is the same as another one
* @param {Emoji|Object} other the emoji to compare it to
* @returns {boolean} whether the emoji is equal to the given emoji or not
*/
equals(other) {
if (other instanceof Emoji) {
return (
other.id === this.id &&
other.name === this.name &&
other.managed === this.managed &&
other.requiresColons === this.requiresColons
);
} else {
return (
other.id === this.id &&
other.name === this.name
);
}
}
/** /**
* The identifier of this emoji, used for message reactions * The identifier of this emoji, used for message reactions
* @readonly * @readonly

View File

@@ -294,6 +294,7 @@ exports.WSEvents = {
GUILD_ROLE_UPDATE: 'GUILD_ROLE_UPDATE', GUILD_ROLE_UPDATE: 'GUILD_ROLE_UPDATE',
GUILD_BAN_ADD: 'GUILD_BAN_ADD', GUILD_BAN_ADD: 'GUILD_BAN_ADD',
GUILD_BAN_REMOVE: 'GUILD_BAN_REMOVE', GUILD_BAN_REMOVE: 'GUILD_BAN_REMOVE',
GUILD_EMOJIS_UPDATE: 'GUILD_EMOJIS_UPDATE',
CHANNEL_CREATE: 'CHANNEL_CREATE', CHANNEL_CREATE: 'CHANNEL_CREATE',
CHANNEL_DELETE: 'CHANNEL_DELETE', CHANNEL_DELETE: 'CHANNEL_DELETE',
CHANNEL_UPDATE: 'CHANNEL_UPDATE', CHANNEL_UPDATE: 'CHANNEL_UPDATE',

View File

@@ -20,6 +20,10 @@ client.on('userUpdate', (o, n) => {
console.log(o.username, n.username); 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('guildMemberAdd', m => console.log(`${m.user.username} joined ${m.guild.name}`)); client.on('guildMemberAdd', m => console.log(`${m.user.username} joined ${m.guild.name}`));
client.on('channelCreate', channel => { client.on('channelCreate', channel => {