Clean up Actions code (#591)

This commit is contained in:
Schuyler Cebulskie
2016-09-03 17:48:51 -04:00
committed by Amish Shah
parent 432c5ccc82
commit 59a5862f2d
22 changed files with 71 additions and 98 deletions

View File

@@ -8,7 +8,7 @@ the REST API methods, in order to prevent rewriting code to handle data,
used to be but they're strictly for manipulating data and making sure
that WebSocket events don't clash with REST methods.
*/
*/
class GenericAction {

View File

@@ -1,6 +1,5 @@
const requireAction = name => require(`./${name}`);
class ActionsManager {
constructor(client) {
this.client = client;
@@ -24,7 +23,7 @@ class ActionsManager {
}
register(name) {
const Action = requireAction(name);
const Action = require(`./${name}`);
this[name] = new Action(this.client);
}
}

View File

@@ -5,7 +5,6 @@ class ChannelCreateAction extends Action {
handle(data) {
const client = this.client;
const channel = client.dataManager.newChannel(data);
return {
channel,
};

View File

@@ -10,8 +10,8 @@ class ChannelDeleteAction extends Action {
handle(data) {
const client = this.client;
let channel = client.channels.get(data.id);
let channel = client.channels.get(data.id);
if (channel) {
client.dataManager.killChannel(channel);
this.deleted[channel.id] = channel;

View File

@@ -6,15 +6,12 @@ class ChannelUpdateAction extends Action {
handle(data) {
const client = this.client;
const channel = client.channels.get(data.id);
const channel = client.channels.get(data.id);
if (channel) {
const oldChannel = cloneObject(channel);
channel.setup(data);
if (!oldChannel.equals(data)) {
client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel);
}
if (!oldChannel.equals(data)) client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel);
return {
old: oldChannel,
updated: channel,

View File

@@ -5,13 +5,9 @@ class GuildBanRemove extends Action {
handle(data) {
const client = this.client;
const guild = client.guilds.get(data.guild_id);
const user = client.dataManager.newUser(data.user);
if (guild && user) {
client.emit(Constants.Events.GUILD_BAN_REMOVE, guild, user);
}
if (guild && user) client.emit(Constants.Events.GUILD_BAN_REMOVE, guild, user);
}
}

View File

@@ -10,8 +10,8 @@ class GuildDeleteAction extends Action {
handle(data) {
const client = this.client;
let guild = client.guilds.get(data.id);
let guild = client.guilds.get(data.id);
if (guild) {
if (guild.available && data.unavailable) {
// guild is unavailable
@@ -24,6 +24,7 @@ class GuildDeleteAction extends Action {
guild: null,
};
}
// delete guild
client.guilds.delete(guild.id);
this.deleted[guild.id] = guild;
@@ -43,10 +44,10 @@ class GuildDeleteAction extends Action {
}
/**
* Emitted whenever a guild becomes unavailable, likely due to a server outage.
*
* @event Client#guildUnavailable
* @param {Guild} guild the guild that has become unavailable.
* Emitted whenever a guild becomes unavailable, likely due to a server outage.
*
* @event Client#guildUnavailable
* @param {Guild} guild the guild that has become unavailable.
*/
module.exports = GuildDeleteAction;

View File

@@ -10,6 +10,7 @@ class GuildMemberRemoveAction extends Action {
handle(data) {
const client = this.client;
const guild = client.guilds.get(data.guild_id);
if (guild) {
let member = guild.members.get(data.user.id);
@@ -17,25 +18,21 @@ class GuildMemberRemoveAction extends Action {
guild.memberCount--;
guild._removeMember(member);
this.deleted[guild.id + data.user.id] = member;
if (client.status === Constants.Status.READY) {
client.emit(Constants.Events.GUILD_MEMBER_REMOVE, guild, member);
}
if (client.status === Constants.Status.READY) client.emit(Constants.Events.GUILD_MEMBER_REMOVE, guild, member);
this.scheduleForDeletion(guild.id, data.user.id);
}
if (!member) {
} else {
member = this.deleted[guild.id + data.user.id];
}
return {
g: guild,
m: member,
guild,
member,
};
}
return {
g: guild,
m: null,
guild,
member: null,
};
}
@@ -44,14 +41,12 @@ class GuildMemberRemoveAction extends Action {
}
}
/**
* Emitted whenever a member leaves a guild, or is kicked.
*
* @event Client#guildMemberRemove
* @param {Guild} guild the guild that the member has left.
* @param {GuildMember} member the member that has left the guild.
*/
* Emitted whenever a member leaves a guild, or is kicked.
*
* @event Client#guildMemberRemove
* @param {Guild} guild the guild that the member has left.
* @param {GuildMember} member the member that has left the guild.
*/
module.exports = GuildMemberRemoveAction;

View File

@@ -6,17 +6,13 @@ class GuildRoleCreate extends Action {
handle(data) {
const client = this.client;
const guild = client.guilds.get(data.guild_id);
const guild = client.guilds.get(data.guild_id);
if (guild) {
const already = guild.roles.get(data.role.id);
const already = guild.roles.has(data.role.id);
const role = new Role(guild, data.role);
guild.roles.set(role.id, role);
if (!already) {
client.emit(Constants.Events.GUILD_ROLE_CREATE, guild, role);
}
if (!already) client.emit(Constants.Events.GUILD_ROLE_CREATE, guild, role);
return {
role,
};
@@ -28,7 +24,6 @@ class GuildRoleCreate extends Action {
}
}
/**
* Emitted whenever a guild role is created.
*
@@ -37,5 +32,4 @@ class GuildRoleCreate extends Action {
* @param {Role} role the role that was created.
*/
module.exports = GuildRoleCreate;

View File

@@ -10,23 +10,21 @@ class GuildRoleDeleteAction extends Action {
handle(data) {
const client = this.client;
const guild = client.guilds.get(data.guild_id);
if (guild) {
let exists = guild.roles.get(data.role_id);
if (exists) {
let role = guild.roles.get(data.role_id);
if (role) {
guild.roles.delete(data.role_id);
this.deleted[guild.id + data.role_id] = exists;
this.deleted[guild.id + data.role_id] = role;
this.scheduleForDeletion(guild.id, data.role_id);
client.emit(Constants.Events.GUILD_ROLE_DELETE, guild, exists);
}
if (!exists) {
exists = this.deleted[guild.id + data.role_id];
client.emit(Constants.Events.GUILD_ROLE_DELETE, guild, role);
} else {
role = this.deleted[guild.id + data.role_id];
}
return {
role: exists,
role,
};
}

View File

@@ -6,23 +6,22 @@ class GuildRoleUpdateAction extends Action {
handle(data) {
const client = this.client;
const guild = client.guilds.get(data.guild_id);
const roleData = data.role;
if (guild) {
let oldRole;
const existingRole = guild.roles.get(roleData.id);
// exists and not the same
if (existingRole && !existingRole.equals(roleData)) {
oldRole = cloneObject(existingRole);
existingRole.setup(data.role);
client.emit(Constants.Events.GUILD_ROLE_UPDATE, guild, oldRole, existingRole);
const roleData = data.role;
let oldRole = null;
const role = guild.roles.get(roleData.id);
if (role && !role.equals(roleData)) {
oldRole = cloneObject(role);
role.setup(data.role);
client.emit(Constants.Events.GUILD_ROLE_UPDATE, guild, oldRole, role);
}
return {
old: oldRole,
updated: existingRole,
updated: role,
};
}

View File

@@ -4,11 +4,10 @@ class GuildSync extends Action {
handle(data) {
const client = this.client;
const guild = client.guilds.get(data.id);
const guild = client.guilds.get(data.id);
if (guild) {
data.presences = data.presences || [];
data.members = data.members || [];
for (const presence of data.presences) {
const user = client.users.get(presence.user.id);
if (user) {
@@ -16,6 +15,8 @@ class GuildSync extends Action {
user.game = presence.game;
}
}
data.members = data.members || [];
for (const syncMember of data.members) {
const member = guild.members.get(syncMember.user.id);
if (member) {

View File

@@ -12,16 +12,12 @@ class GuildUpdateAction extends Action {
handle(data) {
const client = this.client;
const guild = client.guilds.get(data.id);
const guild = client.guilds.get(data.id);
if (guild) {
const oldGuild = cloneObject(guild);
guild.setup(data);
if (!oldGuild.equals(data)) {
client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild);
}
if (!oldGuild.equals(data)) client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild);
return {
old: oldGuild,
updated: guild,

View File

@@ -5,17 +5,17 @@ class MessageCreateAction extends Action {
handle(data) {
const client = this.client;
const channel = client.channels.get(data.channel_id);
const channel = client.channels.get(data.channel_id);
if (channel) {
const message = channel._cacheMessage(new Message(channel, data, client));
return {
m: message,
message,
};
}
return {
m: null,
message: null,
};
}
}

View File

@@ -9,6 +9,7 @@ class MessageDeleteAction extends Action {
handle(data) {
const client = this.client;
const channel = client.channels.get(data.channel_id);
if (channel) {
let message = channel.messages.get(data.id);
@@ -22,18 +23,18 @@ class MessageDeleteAction extends Action {
}
return {
m: message,
message,
};
}
return {
m: null,
message: null,
};
}
scheduleForDeletion(channelID, messageID) {
this.client.setTimeout(
() => delete this.deleted[channelID + messageID], this.client.options.rest_ws_bridge_timeout);
this.client.setTimeout(() => delete this.deleted[channelID + messageID],
this.client.options.rest_ws_bridge_timeout);
}
}

View File

@@ -13,6 +13,7 @@ class MessageDeleteBulkAction extends Action {
handle(data) {
const client = this.client;
const channel = client.channels.get(data.channel_id);
const ids = data.ids;
const messages = new Collection();
for (const id of ids) {
@@ -21,9 +22,8 @@ class MessageDeleteBulkAction extends Action {
messages.set(message.id, message);
}
}
if (messages.size > 0) {
client.emit(Constants.Events.MESSAGE_BULK_DELETE, messages);
}
if (messages.size > 0) client.emit(Constants.Events.MESSAGE_BULK_DELETE, messages);
return {
messages,
};

View File

@@ -6,8 +6,8 @@ class MessageUpdateAction extends Action {
handle(data) {
const client = this.client;
const channel = client.channels.get(data.channel_id);
const channel = client.channels.get(data.channel_id);
if (channel) {
const message = channel.messages.get(data.id);
if (message && !message.equals(data, true)) {

View File

@@ -5,7 +5,6 @@ class UserGetAction extends Action {
handle(data) {
const client = this.client;
const user = client.dataManager.newUser(data);
return {
user,
};

View File

@@ -17,9 +17,7 @@ class UserUpdateAction extends Action {
const oldUser = cloneObject(client.user);
client.user.setup(data);
client.emit(Constants.Events.USER_UPDATE, oldUser, client.user);
return {
old: oldUser,
updated: client.user,

View File

@@ -54,7 +54,7 @@ class RESTMethods {
$this.rest.makeRequest('post', Constants.Endpoints.channelMessages(channel.id), true, {
content, tts, nonce,
}, file)
.then(data => resolve($this.rest.client.actions.MessageCreate.handle(data).m))
.then(data => resolve($this.rest.client.actions.MessageCreate.handle(data).message))
.catch(reject);
}
@@ -77,7 +77,7 @@ class RESTMethods {
resolve(this.rest.client.actions.MessageDelete.handle({
id: message.id,
channel_id: message.channel.id,
}).m);
}).message);
})
.catch(reject);
});
@@ -286,7 +286,7 @@ class RESTMethods {
resolve(this.rest.client.actions.GuildMemberRemove.handle({
guild_id: guild.id,
user: member.user,
}).m);
}).member);
})
.catch(reject);
});

View File

@@ -9,8 +9,8 @@ class MessageCreateHandler extends AbstractHandler {
const response = client.actions.MessageCreate.handle(data);
if (response.m) {
client.emit(Constants.Events.MESSAGE_CREATE, response.m);
if (response.message) {
client.emit(Constants.Events.MESSAGE_CREATE, response.message);
}
}

View File

@@ -9,8 +9,8 @@ class MessageDeleteHandler extends AbstractHandler {
const response = client.actions.MessageDelete.handle(data);
if (response.m) {
client.emit(Constants.Events.MESSAGE_DELETE, response.m);
if (response.message) {
client.emit(Constants.Events.MESSAGE_DELETE, response.message);
}
}