From 1ecda83da7953052977e6297143b82f89adf1058 Mon Sep 17 00:00:00 2001 From: ckohen Date: Mon, 10 May 2021 03:27:36 -0700 Subject: [PATCH] refactor(MessageOptions): move replyTo to reply#messageReference and add failIfNotExists (#5298) Co-authored-by: SpaceEEC Co-authored-by: Shubham Parihar --- src/structures/APIMessage.js | 11 +++++++---- src/structures/Message.js | 7 +++++-- src/structures/interfaces/TextBasedChannel.js | 10 +++++++++- typings/index.d.ts | 7 ++++++- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/structures/APIMessage.js b/src/structures/APIMessage.js index a0dcf9f52..c169e32b0 100644 --- a/src/structures/APIMessage.js +++ b/src/structures/APIMessage.js @@ -175,12 +175,15 @@ class APIMessage { } let message_reference; - if (typeof this.options.replyTo !== 'undefined') { + if (typeof this.options.reply === 'object') { const message_id = this.isMessage - ? this.target.channel.messages.resolveID(this.options.replyTo) - : this.target.messages.resolveID(this.options.replyTo); + ? this.target.channel.messages.resolveID(this.options.reply.messageReference) + : this.target.messages.resolveID(this.options.reply.messageReference); if (message_id) { - message_reference = { message_id }; + message_reference = { + message_id, + fail_if_not_exists: this.options.reply.failIfNotExists ?? true, + }; } } diff --git a/src/structures/Message.js b/src/structures/Message.js index d9d8a209c..aec8e7ccd 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -610,7 +610,7 @@ class Message extends Base { * Send an inline reply to this message. * @param {StringResolvable|APIMessage} [content=''] The content for the message * @param {MessageOptions|MessageAdditions} [options] The additional options to provide - * @param {MessageResolvable} [options.replyTo=this] The message to reply to + * @param {MessageResolvable} [options.reply.messageReference=this] The message to reply to * @returns {Promise} */ reply(content, options) { @@ -618,7 +618,10 @@ class Message extends Base { content instanceof APIMessage ? content : APIMessage.transformOptions(content, options, { - replyTo: this, + reply: { + messageReference: this, + failIfNotExists: options?.reply?.failIfNotExists ?? content?.reply?.failIfNotExists ?? true, + }, }), ); } diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index 6d4690548..b53873c29 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -63,7 +63,7 @@ class TextBasedChannel { * @property {string|boolean} [code] Language for optional codeblock formatting to apply * @property {boolean|SplitOptions} [split=false] Whether or not the message should be split into multiple messages if * it exceeds the character limit. If an object is provided, these are the options for splitting the message - * @property {MessageResolvable} [replyTo] The message to reply to (must be in the same channel and not system) + * @property {ReplyOptions} [reply] The options for replying to a message */ /** @@ -98,6 +98,14 @@ class TextBasedChannel { * @property {string} [append=''] Text to append to every piece except the last */ + /** + * Options for sending a message with a reply. + * @typedef {Object} ReplyOptions + * @param {MessageResolvable} messageReference The message to reply to (must be in the same channel and not system) + * @param {boolean} [failIfNotExists=true] Whether to error if the referenced message + * does not exist (creates a standard message in this case when false) + */ + /** * Sends a message to this channel. * @param {StringResolvable|APIMessage} [content=''] The content to send diff --git a/typings/index.d.ts b/typings/index.d.ts index 9275049eb..c44de9184 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -3198,7 +3198,7 @@ declare module 'discord.js' { files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[]; code?: string | boolean; split?: boolean | SplitOptions; - replyTo?: MessageResolvable; + reply?: ReplyOptions; } type MessageReactionResolvable = MessageReaction | Snowflake; @@ -3447,6 +3447,11 @@ declare module 'discord.js' { maxUsers?: number; } + interface ReplyOptions { + messageReference: MessageResolvable; + failIfNotExists?: boolean; + } + interface ResolvedOverwriteOptions { allow: Permissions; deny: Permissions;