Skip to content

Commit

Permalink
Minor refactor of Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
amishshah committed Apr 29, 2017
1 parent dd8f77f commit f7d6599
Show file tree
Hide file tree
Showing 19 changed files with 37 additions and 104 deletions.
4 changes: 1 addition & 3 deletions src/client/actions/ChannelCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ class ChannelCreateAction extends Action {
handle(data) {
const client = this.client;
const channel = client.dataManager.newChannel(data);
return {
channel,
};
return { channel };
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/client/actions/ChannelDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class ChannelDeleteAction extends Action {
channel = this.deleted.get(data.id) || null;
}

return {
channel,
};
return { channel };
}

scheduleForDeletion(id) {
Expand Down
4 changes: 1 addition & 3 deletions src/client/actions/GuildChannelsPositionUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class GuildChannelsPositionUpdate extends Action {
}
}

return {
guild,
};
return { guild };
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/client/actions/GuildDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class GuildDeleteAction extends Action {
guild = this.deleted.get(data.id) || null;
}

return {
guild,
};
return { guild };
}

scheduleForDeletion(id) {
Expand Down
4 changes: 1 addition & 3 deletions src/client/actions/GuildEmojiCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ class GuildEmojiCreateAction extends Action {
handle(guild, createdEmoji) {
const client = this.client;
const emoji = client.dataManager.newEmoji(createdEmoji, guild);
return {
emoji,
};
return { emoji };
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/client/actions/GuildEmojiDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ class GuildEmojiDeleteAction extends Action {
handle(emoji) {
const client = this.client;
client.dataManager.killEmoji(emoji);
return {
emoji,
};
return { emoji };
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/client/actions/GuildEmojiUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ const Action = require('./Action');
class GuildEmojiUpdateAction extends Action {
handle(oldEmoji, newEmoji) {
const emoji = this.client.dataManager.updateEmoji(oldEmoji, newEmoji);
return {
emoji,
};
return { emoji };
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/client/actions/GuildMemberGet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ const Action = require('./Action');
class GuildMemberGetAction extends Action {
handle(guild, data) {
const member = guild._addMember(data, false);
return {
member,
};
return { member };
}
}

Expand Down
15 changes: 3 additions & 12 deletions src/client/actions/GuildMemberRemove.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class GuildMemberRemoveAction extends Action {

handle(data) {
const client = this.client;

const guild = client.guilds.get(data.guild_id);
let member = null;
if (guild) {
let member = guild.members.get(data.user.id);
member = guild.members.get(data.user.id);
if (member) {
guild.memberCount--;
guild._removeMember(member);
Expand All @@ -22,17 +22,8 @@ class GuildMemberRemoveAction extends Action {
} else {
member = this.deleted.get(guild.id + data.user.id) || null;
}

return {
guild,
member,
};
}

return {
guild,
member: null,
};
return { guild, member };
}

scheduleForDeletion(guildID, userID) {
Expand Down
13 changes: 3 additions & 10 deletions src/client/actions/GuildRoleCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@ const Role = require('../../structures/Role');
class GuildRoleCreate extends Action {
handle(data) {
const client = this.client;

const guild = client.guilds.get(data.guild_id);
let role;
if (guild) {
const already = guild.roles.has(data.role.id);
const role = new Role(guild, data.role);
role = new Role(guild, data.role);
guild.roles.set(role.id, role);
if (!already) client.emit(Constants.Events.GUILD_ROLE_CREATE, role);

return {
role,
};
}

return {
role: null,
};
return { role };
}
}

Expand Down
13 changes: 4 additions & 9 deletions src/client/actions/GuildRoleDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ class GuildRoleDeleteAction extends Action {

handle(data) {
const client = this.client;

const guild = client.guilds.get(data.guild_id);
let role;

if (guild) {
let role = guild.roles.get(data.role_id);
role = guild.roles.get(data.role_id);
if (role) {
guild.roles.delete(data.role_id);
this.deleted.set(guild.id + data.role_id, role);
Expand All @@ -21,15 +22,9 @@ class GuildRoleDeleteAction extends Action {
} else {
role = this.deleted.get(guild.id + data.role_id) || null;
}

return {
role,
};
}

return {
role: null,
};
return { role };
}

scheduleForDeletion(guildID, roleID) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/actions/GuildRoleUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const Util = require('../../util/Util');
class GuildRoleUpdateAction extends Action {
handle(data) {
const client = this.client;

const guild = client.guilds.get(data.guild_id);

if (guild) {
const roleData = data.role;
let oldRole = null;
Expand Down
4 changes: 1 addition & 3 deletions src/client/actions/GuildRolesPositionUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class GuildRolesPositionUpdate extends Action {
}
}

return {
guild,
};
return { guild };
}
}

Expand Down
14 changes: 4 additions & 10 deletions src/client/actions/MessageDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,21 @@ 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);
let message;

if (channel) {
message = channel.messages.get(data.id);
if (message) {
channel.messages.delete(message.id);
this.deleted.set(channel.id + message.id, message);
this.scheduleForDeletion(channel.id, message.id);
} else {
message = this.deleted.get(channel.id + data.id) || null;
}

return {
message,
};
}

return {
message: null,
};
return { message };
}

scheduleForDeletion(channelID, messageID) {
Expand Down
4 changes: 1 addition & 3 deletions src/client/actions/MessageDeleteBulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ class MessageDeleteBulkAction extends Action {
}

if (messages.size > 0) client.emit(Constants.Events.MESSAGE_BULK_DELETE, messages);
return {
messages,
};
return { messages };
}
}

Expand Down
18 changes: 5 additions & 13 deletions src/client/actions/MessageReactionAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,18 @@ class MessageReactionAdd extends Action {
handle(data) {
const user = this.client.users.get(data.user_id);
if (!user) return false;

// Verify channel
const channel = this.client.channels.get(data.channel_id);
if (!channel || channel.type === 'voice') return false;

// Verify message
const message = channel.messages.get(data.message_id);
if (!message) return false;

if (!data.emoji) return false;

// Verify reaction
const reaction = message._addReaction(data.emoji, user);
if (reaction) this.client.emit(Constants.Events.MESSAGE_REACTION_ADD, reaction, user);

if (reaction) {
this.client.emit(Constants.Events.MESSAGE_REACTION_ADD, reaction, user);
}

return {
message,
reaction,
user,
};
return { message, reaction, user };
}
}

Expand Down
18 changes: 5 additions & 13 deletions src/client/actions/MessageReactionRemove.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,18 @@ class MessageReactionRemove extends Action {
handle(data) {
const user = this.client.users.get(data.user_id);
if (!user) return false;

// Verify channel
const channel = this.client.channels.get(data.channel_id);
if (!channel || channel.type === 'voice') return false;

// Verify message
const message = channel.messages.get(data.message_id);
if (!message) return false;

if (!data.emoji) return false;

// Verify reaction
const reaction = message._removeReaction(data.emoji, user);
if (reaction) this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE, reaction, user);

if (reaction) {
this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE, reaction, user);
}

return {
message,
reaction,
user,
};
return { message, reaction, user };
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/client/actions/MessageReactionRemoveAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class MessageReactionRemoveAll extends Action {
message._clearReactions();
this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE_ALL, message);

return {
message,
};
return { message };
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/client/actions/UserGet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ class UserGetAction extends Action {
handle(data) {
const client = this.client;
const user = client.dataManager.newUser(data);
return {
user,
};
return { user };
}
}

Expand Down

0 comments on commit f7d6599

Please sign in to comment.