From 630009f3cf6185f9b94ec146dd1a2f0bdf7f9acc Mon Sep 17 00:00:00 2001 From: bdistin Date: Thu, 8 Mar 2018 10:19:43 -0600 Subject: [PATCH] fix: Convert lastMessage to getters (#2384) * convert lastMessage to getters * fix bug in pr * requested changes --- src/client/actions/MessageCreate.js | 5 ++--- src/structures/DMChannel.js | 1 + src/structures/GroupDMChannel.js | 1 + src/structures/GuildMember.js | 16 +++++++++++++--- src/structures/TextChannel.js | 1 + src/structures/User.js | 16 +++++++++++++--- src/structures/interfaces/TextBasedChannel.js | 14 +++++++++----- 7 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/client/actions/MessageCreate.js b/src/client/actions/MessageCreate.js index e76c6071d..aebc0d389 100644 --- a/src/client/actions/MessageCreate.js +++ b/src/client/actions/MessageCreate.js @@ -12,14 +12,13 @@ class MessageCreateAction extends Action { const user = message.author; const member = channel.guild ? channel.guild.member(user) : null; channel.lastMessageID = data.id; - channel.lastMessage = message; if (user) { user.lastMessageID = data.id; - user.lastMessage = message; + user.lastMessageChannelID = channel.id; } if (member) { member.lastMessageID = data.id; - member.lastMessage = message; + member.lastMessageChannelID = channel.id; } client.emit(Events.MESSAGE_CREATE, message); diff --git a/src/structures/DMChannel.js b/src/structures/DMChannel.js index 20cb8a606..bbf744d22 100644 --- a/src/structures/DMChannel.js +++ b/src/structures/DMChannel.js @@ -44,6 +44,7 @@ class DMChannel extends Channel { // These are here only for documentation purposes - they are implemented by TextBasedChannel /* eslint-disable no-empty-function */ + get lastMessage() {} send() {} search() {} startTyping() {} diff --git a/src/structures/GroupDMChannel.js b/src/structures/GroupDMChannel.js index 697d9daec..f96665d00 100644 --- a/src/structures/GroupDMChannel.js +++ b/src/structures/GroupDMChannel.js @@ -220,6 +220,7 @@ class GroupDMChannel extends Channel { // These are here only for documentation purposes - they are implemented by TextBasedChannel /* eslint-disable no-empty-function */ + get lastMessage() {} send() {} search() {} startTyping() {} diff --git a/src/structures/GuildMember.js b/src/structures/GuildMember.js index ad450c5dd..ab74ad6db 100644 --- a/src/structures/GuildMember.js +++ b/src/structures/GuildMember.js @@ -43,10 +43,10 @@ class GuildMember extends Base { this.lastMessageID = null; /** - * The Message object of the last message sent by the member in their guild, if one was sent - * @type {?Message} + * The ID of the channel for the last message sent by the member in their guild, if one was sent + * @type {?Snowflake} */ - this.lastMessage = null; + this.lastMessageChannelID = null; } _patch(data) { @@ -81,6 +81,16 @@ class GuildMember extends Base { return clone; } + /** + * The Message object of the last message sent by the member in their guild, if one was sent + * @type {?Message} + * @readonly + */ + get lastMessage() { + const channel = this.guild.channels.get(this.lastMessageChannelID); + return (channel && channel.messages.get(this.lastMessageID)) || null; + } + get voiceState() { return this._frozenVoiceState || this.guild.voiceStates.get(this.id) || {}; } diff --git a/src/structures/TextChannel.js b/src/structures/TextChannel.js index ea199d225..bf5d23bb0 100644 --- a/src/structures/TextChannel.js +++ b/src/structures/TextChannel.js @@ -96,6 +96,7 @@ class TextChannel extends GuildChannel { // These are here only for documentation purposes - they are implemented by TextBasedChannel /* eslint-disable no-empty-function */ + get lastMessage() {} send() {} search() {} startTyping() {} diff --git a/src/structures/User.js b/src/structures/User.js index f765ce3b5..674583efb 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -59,10 +59,10 @@ class User extends Base { this.lastMessageID = null; /** - * The Message object of the last message sent by the user, if one was sent - * @type {?Message} + * The ID of the channel for the last message sent by the user, if one was sent + * @type {?Snowflake} */ - this.lastMessage = null; + this.lastMessageChannelID = null; } /** @@ -83,6 +83,16 @@ class User extends Base { return new Date(this.createdTimestamp); } + /** + * The Message object of the last message sent by the user, if one was sent + * @type {?Message} + * @readonly + */ + get lastMessage() { + const channel = this.client.channels.get(this.lastMessageChannelID); + return (channel && channel.messages.get(this.lastMessageID)) || null; + } + /** * The presence of this user * @type {Presence} diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index d67c245f3..a08c85fbe 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -21,12 +21,15 @@ class TextBasedChannel { * @type {?Snowflake} */ this.lastMessageID = null; + } - /** - * The Message object of the last message in the channel, if one was sent - * @type {?Message} - */ - this.lastMessage = null; + /** + * The Message object of the last message in the channel, if one was sent + * @type {?Message} + * @readonly + */ + get lastMessage() { + return this.messages.get(this.lastMessageID) || null; } /** @@ -334,6 +337,7 @@ class TextBasedChannel { if (full) { props.push( 'acknowledge', + 'lastMessage', 'search', 'bulkDelete', 'startTyping',