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 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' ?

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
* 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
*/
/**

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
* 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,

4
typings/index.d.ts vendored
View file

@ -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;