diff --git a/src/structures/APIMessage.js b/src/structures/APIMessage.js index 8b714fcd..b0a22365 100644 --- a/src/structures/APIMessage.js +++ b/src/structures/APIMessage.js @@ -74,25 +74,25 @@ class APIMessage { const isCode = typeof this.options.code !== 'undefined' && this.options.code !== false; const splitOptions = isSplit ? { ...this.options.split } : undefined; - let mentionPart = ''; + let replyPrefix = ''; if (this.options.reply && !this.isUser && this.target.type !== 'dm') { const id = this.target.client.users.resolveID(this.options.reply); - mentionPart = `<@${this.options.reply instanceof GuildMember && this.options.reply.nickname ? '!' : ''}${id}>, `; - if (isSplit) { - splitOptions.prepend = `${mentionPart}${splitOptions.prepend || ''}`; - } + replyPrefix = this.options.replyPrefixer ? this.options.replyPrefixer(this.options.reply) : + this.target.client.options.replyPrefixer ? this.target.client.options.replyPrefixer(this.options.reply) : + `<@${this.options.reply instanceof GuildMember && this.options.reply.nickname ? '!' : ''}${id}>, `; + if (isSplit) splitOptions.prepend = `${replyPrefix}${splitOptions.prepend || ''}`; } - if (content || mentionPart) { + if (content || replyPrefix) { if (isCode) { const codeName = typeof this.options.code === 'string' ? this.options.code : ''; - content = `${mentionPart}\`\`\`${codeName}\n${Util.escapeMarkdown(content, true)}\n\`\`\``; + content = `${replyPrefix}\`\`\`${codeName}\n${Util.escapeMarkdown(content, true)}\n\`\`\``; if (isSplit) { splitOptions.prepend = `${splitOptions.prepend || ''}\`\`\`${codeName}\n`; splitOptions.append = `\n\`\`\`${splitOptions.append || ''}`; } - } else if (mentionPart) { - content = `${mentionPart}${content}`; + } else if (replyPrefix) { + content = `${replyPrefix}${content}`; } const disableEveryone = typeof this.options.disableEveryone === 'undefined' ? diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index f9c668de..524ae4cf 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -62,6 +62,8 @@ class TextBasedChannel { * @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 {UserResolvable} [reply] User to reply to (prefixes the message with a mention, except in DMs) + * @property {ReplyPrefixer} [replyPrefixer] Function to override the default mention-based prefix for the reply + * option */ /** diff --git a/src/util/Constants.js b/src/util/Constants.js index 32413c25..461db414 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -17,6 +17,8 @@ const browser = exports.browser = typeof window !== 'undefined'; * @property {boolean} [fetchAllMembers=false] Whether to cache all guild members and users upon startup, as well as * upon joining a guild (should be avoided whenever possible) * @property {boolean} [disableEveryone=false] Default value for {@link MessageOptions#disableEveryone} + * @property {ReplyPrefixer} [replyPrefixer] Function to override the default mention-based prefix for a message + * reply * @property {number} [restWsBridgeTimeout=5000] Maximum time permitted between REST responses and their * corresponding websocket events * @property {number} [restTimeOffset=500] Extra time in millseconds to wait before continuing to make REST @@ -41,6 +43,13 @@ exports.DefaultOptions = { messageSweepInterval: 0, fetchAllMembers: false, disableEveryone: false, + /** + * Function to override the default mention-based reply prefix behaviour + * @callback ReplyPrefixer + * @param {UserResolvable} user User being replied to (see {@link MessageOptions#reply}) + * @returns {string} Prefix for the content of the reply message + */ + replyPrefixer: null, restWsBridgeTimeout: 5000, disabledEvents: [], retryLimit: 1, diff --git a/typings/index.d.ts b/typings/index.d.ts index 70ff34b3..9a66fee1 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1575,6 +1575,7 @@ declare module 'discord.js' { messageSweepInterval?: number; fetchAllMembers?: boolean; disableEveryone?: boolean; + replyPrefixer?: ReplyPrefixer; restWsBridgeTimeout?: number; restTimeOffset?: number; retryLimit?: number, @@ -1896,6 +1897,7 @@ declare module 'discord.js' { code?: string | boolean; split?: boolean | SplitOptions; reply?: UserResolvable; + replyPrefixer?: ReplyPrefixer; }; type MessageReactionResolvable = MessageReaction | Snowflake; @@ -2006,6 +2008,8 @@ declare module 'discord.js' { maxUsers?: number; }; + type ReplyPrefixer = (user: UserResolvable) => string; + type ResolvedOverwriteOptions = { allow: Permissions; deny: Permissions;