Tracking of GUILD_ROLE_CREATE/UPDATE/DELETE events

This commit is contained in:
hydrabolt
2016-04-17 15:17:18 +01:00
parent 4de1f4ce99
commit bbf7be7dfa
6 changed files with 123 additions and 0 deletions

View File

@@ -17,6 +17,9 @@ class WebSocketPacketManager {
this.register(Constants.WSEvents.GUILD_MEMBER_ADD, 'GuildMemberAdd');
this.register(Constants.WSEvents.GUILD_MEMBER_REMOVE, 'GuildMemberRemove');
this.register(Constants.WSEvents.GUILD_MEMBER_UPDATE, 'GuildMemberUpdate');
this.register(Constants.WSEvents.GUILD_ROLE_CREATE, 'GuildRoleCreate');
this.register(Constants.WSEvents.GUILD_ROLE_DELETE, 'GuildRoleDelete');
this.register(Constants.WSEvents.GUILD_ROLE_UPDATE, 'GuildRoleUpdate');
this.register(Constants.WSEvents.CHANNEL_CREATE, 'ChannelCreate');
this.register(Constants.WSEvents.CHANNEL_DELETE, 'ChannelDelete');
this.register(Constants.WSEvents.CHANNEL_UPDATE, 'ChannelUpdate');

View File

@@ -0,0 +1,36 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const Constants = require('../../../../util/Constants');
const Role = Structure('Role');
const Guild = Structure('Guild');
class GuildRoleCreateHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
let guild = client.store.get('guilds', data.guild_id);
if (guild) {
let already = guild.store.get('roles', data.role.id);
let role = new Role(guild, data.role);
guild.store.add('roles', role);
if (!already) {
client.emit(Constants.Events.GUILD_ROLE_CREATE, guild, role);
}
}
}
};
module.exports = GuildRoleCreateHandler;

View File

@@ -0,0 +1,34 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const Constants = require('../../../../util/Constants');
const Role = Structure('Role');
const Guild = Structure('Guild');
class GuildRoleDeleteHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
let guild = client.store.get('guilds', data.guild_id);
if (guild) {
let exists = guild.store.get('roles', data.role_id);
if (exists) {
guild.store.remove('roles', data.role_id);
client.emit(Constants.Events.GUILD_ROLE_DELETE, guild, exists);
}
}
}
};
module.exports = GuildRoleDeleteHandler;

View File

@@ -0,0 +1,36 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const Constants = require('../../../../util/Constants');
const CloneObject = require('../../../../util/CloneObject');
const Role = Structure('Role');
const Guild = Structure('Guild');
class GuildRoleUpdateHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
let guild = client.store.get('guilds', data.guild_id);
if (guild) {
let existingRole = guild.store.get('roles', data.role.id);
if (existingRole) {
let oldRole = CloneObject(existingRole);
existingRole.setup(data.role);
client.emit(Constants.Events.GUILD_ROLE_UPDATE, guild, oldRole, existingRole);
}
}
}
};
module.exports = GuildRoleUpdateHandler;

View File

@@ -84,6 +84,9 @@ const Events = exports.Events = {
GUILD_MEMBER_ADD: 'guildMemberAdd',
GUILD_MEMBER_REMOVE: 'guildMemberRemove',
GUILD_MEMBER_ROLES_UPDATE: 'guildMemberRolesUpdate',
GUILD_ROLE_CREATE: 'guildRoleCreate',
GUILD_ROLE_DELETE: 'guildRoleDelete',
GUILD_ROLE_UPDATE: 'guildRoleUpdate',
CHANNEL_CREATE: 'channelCreate',
CHANNEL_DELETE: 'channelDelete',
CHANNEL_UPDATE: 'channelUpdate',

View File

@@ -37,3 +37,14 @@ client.on('guildMemberRemove', (guild, user) => {
console.log('dead guild member', user.username, 'in', guild.name);
});
client.on('guildRoleCreate', (guild, role) => {
console.log('new role', role.name, 'in', guild.name);
});
client.on('guildRoleDelete', (guild, role) => {
console.log('dead role', role.name, 'in', guild.name);
});
client.on('guildRoleUpdate', (guild, old, newRole) => {
console.log('updated role', old.name, 'to', newRole.name, 'in', guild.name);
});