diff --git a/src/managers/MessageManager.js b/src/managers/MessageManager.js index 739818d6be65..40759da0e4b1 100644 --- a/src/managers/MessageManager.js +++ b/src/managers/MessageManager.js @@ -28,7 +28,7 @@ class MessageManager extends CachedManager { */ _add(data, cache) { - return super._add(data, cache, { extras: [this.channel] }); + return super._add(data, cache); } /** diff --git a/src/managers/ReactionManager.js b/src/managers/ReactionManager.js index 57e7c0022487..b587a0e94d34 100644 --- a/src/managers/ReactionManager.js +++ b/src/managers/ReactionManager.js @@ -58,7 +58,7 @@ class ReactionManager extends CachedManager { * @returns {Promise} */ async removeAll() { - await this.client.api.channels(this.message.channel.id).messages(this.message.id).reactions.delete(); + await this.client.api.channels(this.message.channelId).messages(this.message.id).reactions.delete(); return this.message; } } diff --git a/src/managers/ReactionUserManager.js b/src/managers/ReactionUserManager.js index 5fc2147021e8..cc86187e7979 100644 --- a/src/managers/ReactionUserManager.js +++ b/src/managers/ReactionUserManager.js @@ -40,7 +40,7 @@ class ReactionUserManager extends CachedManager { */ async fetch({ limit = 100, after } = {}) { const message = this.reaction.message; - const data = await this.client.api.channels[message.channel.id].messages[message.id].reactions[ + const data = await this.client.api.channels[message.channelId].messages[message.id].reactions[ this.reaction.emoji.identifier ].get({ query: { limit, after } }); const users = new Collection(); @@ -61,7 +61,7 @@ class ReactionUserManager extends CachedManager { const userId = this.client.users.resolveId(user); if (!userId) throw new Error('REACTION_RESOLVE_USER'); const message = this.reaction.message; - await this.client.api.channels[message.channel.id].messages[message.id].reactions[this.reaction.emoji.identifier][ + await this.client.api.channels[message.channelId].messages[message.id].reactions[this.reaction.emoji.identifier][ userId === this.client.user.id ? '@me' : userId ].delete(); return this.reaction; diff --git a/src/structures/Message.js b/src/structures/Message.js index bec1e23464ea..71f62654a0e1 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -27,16 +27,21 @@ class Message extends Base { /** * @param {Client} client The instantiating client * @param {APIMessage} data The data for the message - * @param {TextBasedChannels} channel The channel the message was sent in */ - constructor(client, data, channel) { + constructor(client, data) { super(client); /** - * The channel that the message was sent in - * @type {TextBasedChannels} + * The id of the channel the message was sent in + * @type {Snowflake} */ - this.channel = channel; + this.channelId = data.channel_id; + + /** + * The id of the guild the message was sent in, if any + * @type {?Snowflake} + */ + this.guildId = data.guild_id ?? null; /** * Whether this message has been deleted @@ -299,7 +304,7 @@ class Message extends Base { } if (data.referenced_message) { - this.channel.messages._add(data.referenced_message); + this.channel?.messages._add(data.referenced_message); } /** @@ -333,6 +338,15 @@ class Message extends Base { return clone; } + /** + * The channel that the message was sent in + * @type {TextChannel|DMChannel|NewsChannel|ThreadChannel} + * @readonly + */ + get channel() { + return this.client.channels.resolve(this.channelId); + } + /** * Whether or not this message is a partial * @type {boolean} @@ -376,7 +390,7 @@ class Message extends Base { * @readonly */ get guild() { - return this.channel.guild ?? null; + return this.client.guilds.resolve(this.guildId); } /** @@ -396,7 +410,7 @@ class Message extends Base { * @readonly */ get thread() { - return this.channel.threads.resolve(this.id); + return this.channel?.threads.resolve(this.id) ?? null; } /** @@ -405,7 +419,7 @@ class Message extends Base { * @readonly */ get url() { - return `https://discord.com/channels/${this.guild ? this.guild.id : '@me'}/${this.channel.id}/${this.id}`; + return `https://discord.com/channels/${this.guildId ?? '@me'}/${this.channelId}/${this.id}`; } /** diff --git a/src/structures/MessageCollector.js b/src/structures/MessageCollector.js index 6fba05d6a68a..faf5b527db41 100644 --- a/src/structures/MessageCollector.js +++ b/src/structures/MessageCollector.js @@ -70,7 +70,7 @@ class MessageCollector extends Collector { * @event MessageCollector#collect * @param {Message} message The message that was collected */ - if (message.channel.id !== this.channel.id) return null; + if (message.channelId !== this.channel.id) return null; this.received++; return message.id; } @@ -86,7 +86,7 @@ class MessageCollector extends Collector { * @event MessageCollector#dispose * @param {Message} message The message that was disposed of */ - return message.channel.id === this.channel.id ? message.id : null; + return message.channelId === this.channel.id ? message.id : null; } /** diff --git a/src/structures/MessageMentions.js b/src/structures/MessageMentions.js index cd2c697d596d..1a68def1b238 100644 --- a/src/structures/MessageMentions.js +++ b/src/structures/MessageMentions.js @@ -69,8 +69,9 @@ class MessageMentions { this.roles = new Collection(roles); } else { this.roles = new Collection(); + const guild = message.guild; for (const mention of roles) { - const role = message.channel.guild.roles.cache.get(mention); + const role = guild.roles.cache.get(mention); if (role) this.roles.set(role.id, role); } } diff --git a/src/structures/MessagePayload.js b/src/structures/MessagePayload.js index 8ab988ee4779..8a91dc2f4817 100644 --- a/src/structures/MessagePayload.js +++ b/src/structures/MessagePayload.js @@ -167,9 +167,8 @@ class MessagePayload { let message_reference; if (typeof this.options.reply === 'object') { - const message_id = this.isMessage - ? this.target.channel.messages.resolveId(this.options.reply.messageReference) - : this.target.messages.resolveId(this.options.reply.messageReference); + const reference = this.options.reply.messageReference; + const message_id = this.isMessage ? reference.id ?? reference : this.target.messages.resolveId(reference); if (message_id) { message_reference = { message_id, diff --git a/src/structures/MessageReaction.js b/src/structures/MessageReaction.js index 690435706c37..a6d532823b82 100644 --- a/src/structures/MessageReaction.js +++ b/src/structures/MessageReaction.js @@ -63,7 +63,7 @@ class MessageReaction { */ async remove() { await this.client.api - .channels(this.message.channel.id) + .channels(this.message.channelId) .messages(this.message.id) .reactions(this._emoji.identifier) .delete(); diff --git a/src/structures/ReactionCollector.js b/src/structures/ReactionCollector.js index 3c0588d5e634..246c3fb3bed4 100644 --- a/src/structures/ReactionCollector.js +++ b/src/structures/ReactionCollector.js @@ -176,7 +176,7 @@ class ReactionCollector extends Collector { * @returns {void} */ _handleChannelDeletion(channel) { - if (channel.id === this.message.channel.id) { + if (channel.id === this.message.channelId) { this.stop('channelDelete'); } } diff --git a/test/random.js b/test/random.js index 53162dd11d72..04717360e50d 100644 --- a/test/random.js +++ b/test/random.js @@ -234,12 +234,12 @@ client.on('messageCreate', msg => { }); client.on('messageReactionAdd', (reaction, user) => { - if (reaction.message.channel.id !== '222086648706498562') return; + if (reaction.message.channelId !== '222086648706498562') return; reaction.message.channel.send(`${user.username} added reaction ${reaction.emoji}, count is now ${reaction.count}`); }); client.on('messageReactionRemove', (reaction, user) => { - if (reaction.message.channel.id !== '222086648706498562') return; + if (reaction.message.channelId !== '222086648706498562') return; reaction.message.channel.send(`${user.username} removed reaction ${reaction.emoji}, count is now ${reaction.count}`); }); diff --git a/typings/index.d.ts b/typings/index.d.ts index 44d3302c5eda..3297788ccfcf 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1048,7 +1048,7 @@ export class LimitedCollection extends Collection { } export class Message extends Base { - public constructor(client: Client, data: RawMessageData, channel: TextBasedChannels); + public constructor(client: Client, data: RawMessageData); private _patch(data: RawPartialMessageData, partial: true): Message; private _patch(data: RawMessageData, partial?: boolean): Message; private _update(data: RawPartialMessageData, partial: true): Message; @@ -1058,7 +1058,8 @@ export class Message extends Base { public applicationId: Snowflake | null; public attachments: Collection; public author: User; - public channel: TextBasedChannels; + public readonly channel: TextBasedChannels; + public channelId: Snowflake; public readonly cleanContent: string; public components: MessageActionRow[]; public content: string; @@ -1072,6 +1073,7 @@ export class Message extends Base { public editedTimestamp: number | null; public embeds: MessageEmbed[]; public groupActivityApplication: ClientApplication | null; + public guildId: Snowflake | null; public readonly guild: Guild | null; public readonly hasThread: boolean; public id: Snowflake;