From 3b59dfc2d0315a3c57b862b5f217ba834c30d5f2 Mon Sep 17 00:00:00 2001 From: izexi <43889168+izexi@users.noreply.github.com> Date: Mon, 6 May 2019 10:56:00 +0100 Subject: [PATCH 1/4] ref: add getPayload and use for other get* methods --- src/client/actions/Action.js | 65 +++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/src/client/actions/Action.js b/src/client/actions/Action.js index 0b03c071be22..5179d1aad4a2 100644 --- a/src/client/actions/Action.js +++ b/src/client/actions/Action.js @@ -23,47 +23,52 @@ class GenericAction { return data; } + getPayload(data, store, id, partialType, cache = true) { + const existing = store.get(id); + if (!existing && this.client.options.partials.includes(partialType)) { + return store.add(data, cache); + } + return existing; + } + getChannel(data) { const id = data.channel_id || data.id; - return data.channel || (this.client.options.partials.includes(PartialTypes.CHANNEL) ? - this.client.channels.add({ - id, - guild_id: data.guild_id, - }) : - this.client.channels.get(id)); + return this.getPayload({ + id, + guild_id: data.guild_id, + }, this.client.channels, id, PartialTypes.CHANNEL); } - getMessage(data, channel, cache = true) { + getMessage(data, channel, cache) { const id = data.message_id || data.id; - return data.message || (this.client.options.partials.includes(PartialTypes.MESSAGE) ? - channel.messages.add({ - id, - channel_id: channel.id, - guild_id: data.guild_id || (channel.guild ? channel.guild.id : null), - }, cache) : - channel.messages.get(id)); + return this.getPayload({ + id, + channel_id: channel.id, + guild_id: data.guild_id || (channel.guild ? channel.guild.id : null), + }, channel.messages, id, PartialTypes.MESSAGE, cache); } getReaction(data, message, user) { - const emojiID = data.emoji.id || decodeURIComponent(data.emoji.name); - const existing = message.reactions.get(emojiID); - if (!existing && this.client.options.partials.includes(PartialTypes.MESSAGE)) { - return message.reactions.add({ - emoji: data.emoji, - count: 0, - me: user.id === this.client.user.id, - }); - } - return existing; + const id = data.emoji.id || decodeURIComponent(data.emoji.name); + return this.getPayload({ + emoji: data.emoji, + count: 0, + me: user.id === this.client.user.id, + }, message.reactions, id, PartialTypes.MESSAGE); } getMember(data, guild) { - const userID = data.user.id; - const existing = guild.members.get(userID); - if (!existing && this.client.options.partials.includes(PartialTypes.GUILD_MEMBER)) { - return guild.members.add({ user: { id: userID } }); - } - return existing; + const id = data.user.id; + return this.getPayload({ + user: { + id, + }, + }, guild.members, id, PartialTypes.GUILD_MEMBER); + } + + getUser(data) { + const id = data.user_id; + return this.getPayload({ id }, this.client.users, id, PartialTypes.USER); } } From 7d86c3f80e7318cfeca4048024b609ddb745512d Mon Sep 17 00:00:00 2001 From: izexi <43889168+izexi@users.noreply.github.com> Date: Mon, 6 May 2019 12:11:22 +0100 Subject: [PATCH 2/4] return existing data.* --- src/client/actions/Action.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/client/actions/Action.js b/src/client/actions/Action.js index 5179d1aad4a2..7f21318b4d37 100644 --- a/src/client/actions/Action.js +++ b/src/client/actions/Action.js @@ -23,7 +23,7 @@ class GenericAction { return data; } - getPayload(data, store, id, partialType, cache = true) { + getPayload(data, store, id, partialType, cache) { const existing = store.get(id); if (!existing && this.client.options.partials.includes(partialType)) { return store.add(data, cache); @@ -33,7 +33,7 @@ class GenericAction { getChannel(data) { const id = data.channel_id || data.id; - return this.getPayload({ + return data.channel || this.getPayload({ id, guild_id: data.guild_id, }, this.client.channels, id, PartialTypes.CHANNEL); @@ -41,7 +41,7 @@ class GenericAction { getMessage(data, channel, cache) { const id = data.message_id || data.id; - return this.getPayload({ + return data.message || this.getPayload({ id, channel_id: channel.id, guild_id: data.guild_id || (channel.guild ? channel.guild.id : null), @@ -68,7 +68,7 @@ class GenericAction { getUser(data) { const id = data.user_id; - return this.getPayload({ id }, this.client.users, id, PartialTypes.USER); + return data.user || this.getPayload({ id }, this.client.users, id, PartialTypes.USER); } } From 3a13bb0ccef2dd169e0c3719f7cf1340e3540819 Mon Sep 17 00:00:00 2001 From: izexi <43889168+izexi@users.noreply.github.com> Date: Mon, 6 May 2019 12:17:41 +0100 Subject: [PATCH 3/4] use Action.getUser() --- src/client/actions/MessageReactionAdd.js | 2 +- src/client/actions/MessageReactionRemove.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/actions/MessageReactionAdd.js b/src/client/actions/MessageReactionAdd.js index 185b980ade1b..e7ae7e26a79f 100644 --- a/src/client/actions/MessageReactionAdd.js +++ b/src/client/actions/MessageReactionAdd.js @@ -14,7 +14,7 @@ class MessageReactionAdd extends Action { handle(data) { if (!data.emoji) return false; - const user = data.user || this.client.users.get(data.user_id); + const user = this.getUser(data); if (!user) return false; // Verify channel diff --git a/src/client/actions/MessageReactionRemove.js b/src/client/actions/MessageReactionRemove.js index 4e7995f8e0a4..c6c0d664a807 100644 --- a/src/client/actions/MessageReactionRemove.js +++ b/src/client/actions/MessageReactionRemove.js @@ -14,7 +14,7 @@ class MessageReactionRemove extends Action { handle(data) { if (!data.emoji) return false; - const user = this.client.users.get(data.user_id); + const user = this.getUser(data); if (!user) return false; // Verify channel From 9595b7068f67ecded19adf39312948fe28437501 Mon Sep 17 00:00:00 2001 From: Khoo Hao Yit <40757009+KhooHaoYit@users.noreply.github.com> Date: Mon, 6 May 2019 23:34:37 +0800 Subject: [PATCH 4/4] Fix messageDelete double emission --- src/structures/Message.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/structures/Message.js b/src/structures/Message.js index c390ac231b76..d66124c9e803 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -454,11 +454,7 @@ class Message extends Base { */ delete({ timeout = 0, reason } = {}) { if (timeout <= 0) { - return this.channel.messages.remove(this.id, reason).then(() => - this.client.actions.MessageDelete.handle({ - id: this.id, - channel_id: this.channel.id, - }).message); + return this.channel.messages.remove(this.id, reason).then(() => this); } else { return new Promise(resolve => { this.client.setTimeout(() => {