Add replyPrefixer client & message option

This commit is contained in:
Schuyler Cebulskie 2018-10-27 16:40:48 -04:00
parent b759fc415e
commit 5bdd162c24
4 changed files with 24 additions and 9 deletions

View file

@ -74,25 +74,25 @@ class APIMessage {
const isCode = typeof this.options.code !== 'undefined' && this.options.code !== false; const isCode = typeof this.options.code !== 'undefined' && this.options.code !== false;
const splitOptions = isSplit ? { ...this.options.split } : undefined; const splitOptions = isSplit ? { ...this.options.split } : undefined;
let mentionPart = ''; let replyPrefix = '';
if (this.options.reply && !this.isUser && this.target.type !== 'dm') { if (this.options.reply && !this.isUser && this.target.type !== 'dm') {
const id = this.target.client.users.resolveID(this.options.reply); const id = this.target.client.users.resolveID(this.options.reply);
mentionPart = `<@${this.options.reply instanceof GuildMember && this.options.reply.nickname ? '!' : ''}${id}>, `; replyPrefix = this.options.replyPrefixer ? this.options.replyPrefixer(this.options.reply) :
if (isSplit) { this.target.client.options.replyPrefixer ? this.target.client.options.replyPrefixer(this.options.reply) :
splitOptions.prepend = `${mentionPart}${splitOptions.prepend || ''}`; `<@${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) { if (isCode) {
const codeName = typeof this.options.code === 'string' ? this.options.code : ''; 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) { if (isSplit) {
splitOptions.prepend = `${splitOptions.prepend || ''}\`\`\`${codeName}\n`; splitOptions.prepend = `${splitOptions.prepend || ''}\`\`\`${codeName}\n`;
splitOptions.append = `\n\`\`\`${splitOptions.append || ''}`; splitOptions.append = `\n\`\`\`${splitOptions.append || ''}`;
} }
} else if (mentionPart) { } else if (replyPrefix) {
content = `${mentionPart}${content}`; content = `${replyPrefix}${content}`;
} }
const disableEveryone = typeof this.options.disableEveryone === 'undefined' ? const disableEveryone = typeof this.options.disableEveryone === 'undefined' ?

View file

@ -62,6 +62,8 @@ class TextBasedChannel {
* @property {boolean|SplitOptions} [split=false] Whether or not the message should be split into multiple messages if * @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 * 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 {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
*/ */
/** /**

View file

@ -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 * @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) * upon joining a guild (should be avoided whenever possible)
* @property {boolean} [disableEveryone=false] Default value for {@link MessageOptions#disableEveryone} * @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 * @property {number} [restWsBridgeTimeout=5000] Maximum time permitted between REST responses and their
* corresponding websocket events * corresponding websocket events
* @property {number} [restTimeOffset=500] Extra time in millseconds to wait before continuing to make REST * @property {number} [restTimeOffset=500] Extra time in millseconds to wait before continuing to make REST
@ -41,6 +43,13 @@ exports.DefaultOptions = {
messageSweepInterval: 0, messageSweepInterval: 0,
fetchAllMembers: false, fetchAllMembers: false,
disableEveryone: 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, restWsBridgeTimeout: 5000,
disabledEvents: [], disabledEvents: [],
retryLimit: 1, retryLimit: 1,

4
typings/index.d.ts vendored
View file

@ -1575,6 +1575,7 @@ declare module 'discord.js' {
messageSweepInterval?: number; messageSweepInterval?: number;
fetchAllMembers?: boolean; fetchAllMembers?: boolean;
disableEveryone?: boolean; disableEveryone?: boolean;
replyPrefixer?: ReplyPrefixer;
restWsBridgeTimeout?: number; restWsBridgeTimeout?: number;
restTimeOffset?: number; restTimeOffset?: number;
retryLimit?: number, retryLimit?: number,
@ -1896,6 +1897,7 @@ declare module 'discord.js' {
code?: string | boolean; code?: string | boolean;
split?: boolean | SplitOptions; split?: boolean | SplitOptions;
reply?: UserResolvable; reply?: UserResolvable;
replyPrefixer?: ReplyPrefixer;
}; };
type MessageReactionResolvable = MessageReaction | Snowflake; type MessageReactionResolvable = MessageReaction | Snowflake;
@ -2006,6 +2008,8 @@ declare module 'discord.js' {
maxUsers?: number; maxUsers?: number;
}; };
type ReplyPrefixer = (user: UserResolvable) => string;
type ResolvedOverwriteOptions = { type ResolvedOverwriteOptions = {
allow: Permissions; allow: Permissions;
deny: Permissions; deny: Permissions;