mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 03:23:29 +01:00
Add centralised reply option to message options
This commit is contained in:
@@ -46,21 +46,37 @@ class RESTMethods {
|
|||||||
return this.rest.makeRequest('get', Constants.Endpoints.botGateway, true);
|
return this.rest.makeRequest('get', Constants.Endpoints.botGateway, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessage(channel, content, { tts, nonce, embed, disableEveryone, split, code } = {}, file = null) {
|
sendMessage(channel, content, { tts, nonce, embed, disableEveryone, split, code, reply } = {}, file = null) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => { // eslint-disable-line complexity
|
||||||
if (typeof content !== 'undefined') content = this.client.resolver.resolveString(content);
|
if (typeof content !== 'undefined') content = this.client.resolver.resolveString(content);
|
||||||
|
|
||||||
if (content) {
|
if (content) {
|
||||||
|
if (split && typeof split !== 'object') split = {};
|
||||||
|
|
||||||
|
// Wrap everything in a code block
|
||||||
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
|
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
|
||||||
content = escapeMarkdown(this.client.resolver.resolveString(content), true);
|
content = escapeMarkdown(this.client.resolver.resolveString(content), true);
|
||||||
content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``;
|
content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add zero-width spaces to @everyone/@here
|
||||||
if (disableEveryone || (typeof disableEveryone === 'undefined' && this.client.options.disableEveryone)) {
|
if (disableEveryone || (typeof disableEveryone === 'undefined' && this.client.options.disableEveryone)) {
|
||||||
content = content.replace(/@(everyone|here)/g, '@\u200b$1');
|
content = content.replace(/@(everyone|here)/g, '@\u200b$1');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (split) content = splitMessage(content, typeof split === 'object' ? split : {});
|
// Add the reply prefix
|
||||||
|
if (reply && !(channel instanceof User || channel instanceof GuildMember) && channel.type !== 'dm') {
|
||||||
|
const id = this.client.resolver.resolveUserID(reply);
|
||||||
|
const mention = `<@${reply instanceof GuildMember && reply.nickname ? '!' : ''}${id}>`;
|
||||||
|
content = `${mention}${content ? `, ${content}` : ''}`;
|
||||||
|
if (split) split.prepend = `${mention}, ${split.prepend || ''}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Split the content
|
||||||
|
if (split) content = splitMessage(content, split);
|
||||||
|
} else if (reply && !(channel instanceof User || channel instanceof GuildMember) && channel.type !== 'dm') {
|
||||||
|
const id = this.client.resolver.resolveUserID(reply);
|
||||||
|
content = `<@${reply instanceof GuildMember && reply.nickname ? '!' : ''}${id}>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const send = chan => {
|
const send = chan => {
|
||||||
@@ -89,12 +105,22 @@ class RESTMethods {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
updateMessage(message, content, { embed, code } = {}) {
|
updateMessage(message, content, { embed, code, reply } = {}) {
|
||||||
content = this.client.resolver.resolveString(content);
|
if (typeof content !== 'undefined') content = this.client.resolver.resolveString(content);
|
||||||
|
|
||||||
|
// Wrap everything in a code block
|
||||||
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
|
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
|
||||||
content = escapeMarkdown(this.client.resolver.resolveString(content), true);
|
content = escapeMarkdown(this.client.resolver.resolveString(content), true);
|
||||||
content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``;
|
content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add the reply prefix
|
||||||
|
if (reply && message.channel.type !== 'dm') {
|
||||||
|
const id = this.client.resolver.resolveUserID(reply);
|
||||||
|
const mention = `<@${reply instanceof GuildMember && reply.nickname ? '!' : ''}${id}>`;
|
||||||
|
content = `${mention}${content ? `, ${content}` : ''}`;
|
||||||
|
}
|
||||||
|
|
||||||
return this.rest.makeRequest('patch', Constants.Endpoints.channelMessage(message.channel.id, message.id), true, {
|
return this.rest.makeRequest('patch', Constants.Endpoints.channelMessage(message.channel.id, message.id), true, {
|
||||||
content, embed,
|
content, embed,
|
||||||
}).then(data => this.client.actions.MessageUpdate.handle(data).updated);
|
}).then(data => this.client.actions.MessageUpdate.handle(data).updated);
|
||||||
|
|||||||
@@ -470,8 +470,8 @@ class Message {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Reply to the message
|
* Reply to the message
|
||||||
* @param {StringResolvable} content The content for the message
|
* @param {StringResolvable} [content] The content for the message
|
||||||
* @param {MessageOptions} [options = {}] The options to provide
|
* @param {MessageOptions} [options] The options to provide
|
||||||
* @returns {Promise<Message|Message[]>}
|
* @returns {Promise<Message|Message[]>}
|
||||||
* @example
|
* @example
|
||||||
* // reply to a message
|
* // reply to a message
|
||||||
@@ -479,9 +479,14 @@ class Message {
|
|||||||
* .then(msg => console.log(`Sent a reply to ${msg.author}`))
|
* .then(msg => console.log(`Sent a reply to ${msg.author}`))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
reply(content, options = {}) {
|
reply(content, options) {
|
||||||
content = `${this.guild || this.channel.type === 'group' ? `${this.author}, ` : ''}${content}`;
|
if (!options && typeof content === 'object' && !(content instanceof Array)) {
|
||||||
return this.channel.send(content, options);
|
options = content;
|
||||||
|
content = '';
|
||||||
|
} else if (!options) {
|
||||||
|
options = {};
|
||||||
|
}
|
||||||
|
return this.channel.send(content, Object.assign(options, { reply: this.member || this.author }));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user