From 1398431bca9a3743758295f1effa2e7f6c35093e Mon Sep 17 00:00:00 2001 From: monbrey Date: Mon, 10 May 2021 18:56:46 +1000 Subject: [PATCH] feat(Message): replace referencedMessage with fetchReference (#5577) --- src/errors/Messages.js | 1 + src/structures/Message.js | 17 +++++++++-------- typings/index.d.ts | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/errors/Messages.js b/src/errors/Messages.js index b8f309c0c7bd..49df0178c0f5 100644 --- a/src/errors/Messages.js +++ b/src/errors/Messages.js @@ -96,6 +96,7 @@ const Messages = { INVALID_ELEMENT: (type, name, elem) => `Supplied ${type} ${name} includes an invalid element: ${elem}`, WEBHOOK_MESSAGE: 'The message was not sent by a webhook.', + MESSAGE_REFERENCE_MISSING: 'The message does not reference another message', EMOJI_TYPE: 'Emoji must be a string or GuildEmoji/ReactionEmoji', EMOJI_MANAGED: 'Emoji is managed and has no Author.', diff --git a/src/structures/Message.js b/src/structures/Message.js index 186372f57aa8..d9d8a209c778 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -442,15 +442,16 @@ class Message extends Base { } /** - * The Message this crosspost/reply/pin-add references, if cached - * @type {?Message} - * @readonly + * Fetches the Message this crosspost/reply/pin-add references, if available to the client + * @returns {Promise} */ - get referencedMessage() { - if (!this.reference) return null; - const referenceChannel = this.client.channels.resolve(this.reference.channelID); - if (!referenceChannel) return null; - return referenceChannel.messages.resolve(this.reference.messageID); + async fetchReference() { + if (!this.reference) throw new Error('MESSAGE_REFERENCE_MISSING'); + const { channelID, messageID } = this.reference; + const channel = this.client.channels.resolve(channelID); + if (!channel) throw new Error('GUILD_CHANNEL_RESOLVE'); + const message = await channel.messages.fetch(messageID); + return message; } /** diff --git a/typings/index.d.ts b/typings/index.d.ts index 6ab43f7d819f..59ab672fcd1e 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1118,7 +1118,6 @@ declare module 'discord.js' { public webhookID: Snowflake | null; public flags: Readonly; public reference: MessageReference | null; - public readonly referencedMessage: Message | null; public awaitReactions( filter: CollectorFilter<[MessageReaction, User]>, options?: AwaitReactionsOptions, @@ -1133,6 +1132,7 @@ declare module 'discord.js' { ): Promise; public edit(content: StringResolvable, options: MessageEditOptions | MessageEmbed): Promise; public equals(message: Message, rawData: object): boolean; + public fetchReference(): Promise; public fetchWebhook(): Promise; public crosspost(): Promise; public fetch(force?: boolean): Promise;