mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 00:53:31 +01:00
Merge branch 'master' into indev-prism
This commit is contained in:
@@ -278,8 +278,8 @@ class Guild {
|
||||
}
|
||||
|
||||
/**
|
||||
* The `#general` GuildChannel of the server.
|
||||
* @type {GuildChannel}
|
||||
* The `#general` TextChannel of the server.
|
||||
* @type {TextChannel}
|
||||
* @readonly
|
||||
*/
|
||||
get defaultChannel() {
|
||||
|
||||
@@ -470,8 +470,8 @@ class Message {
|
||||
|
||||
/**
|
||||
* Reply to the message
|
||||
* @param {StringResolvable} content The content for the message
|
||||
* @param {MessageOptions} [options = {}] The options to provide
|
||||
* @param {StringResolvable} [content] The content for the message
|
||||
* @param {MessageOptions} [options] The options to provide
|
||||
* @returns {Promise<Message|Message[]>}
|
||||
* @example
|
||||
* // reply to a message
|
||||
@@ -479,9 +479,14 @@ class Message {
|
||||
* .then(msg => console.log(`Sent a reply to ${msg.author}`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
reply(content, options = {}) {
|
||||
content = `${this.guild || this.channel.type === 'group' ? `${this.author}, ` : ''}${content}`;
|
||||
return this.channel.send(content, options);
|
||||
reply(content, options) {
|
||||
if (!options && typeof content === 'object' && !(content instanceof Array)) {
|
||||
options = content;
|
||||
content = '';
|
||||
} else if (!options) {
|
||||
options = {};
|
||||
}
|
||||
return this.channel.send(content, Object.assign(options, { reply: this.member || this.author }));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
const ClientDataResolver = require('../client/ClientDataResolver');
|
||||
|
||||
/**
|
||||
* A rich embed to be sent with a message
|
||||
* A rich embed to be sent with a message with a fluent interface for creation
|
||||
* @param {Object} [data] Data to set in the rich embed
|
||||
*/
|
||||
class RichEmbed {
|
||||
@@ -101,24 +103,11 @@ class RichEmbed {
|
||||
|
||||
/**
|
||||
* Sets the color of this embed
|
||||
* @param {string|number|number[]} color The color to set
|
||||
* @param {ColorResolvable} color The color to set
|
||||
* @returns {RichEmbed} This embed
|
||||
*/
|
||||
setColor(color) {
|
||||
let radix = 10;
|
||||
if (color instanceof Array) {
|
||||
color = (color[0] << 16) + (color[1] << 8) + color[2];
|
||||
} else if (typeof color === 'string' && color.startsWith('#')) {
|
||||
radix = 16;
|
||||
color = color.replace('#', '');
|
||||
}
|
||||
color = parseInt(color, radix);
|
||||
if (color < 0 || color > 0xFFFFFF) {
|
||||
throw new RangeError('RichEmbed color must be within the range 0 - 16777215 (0xFFFFFF).');
|
||||
} else if (color && isNaN(color)) {
|
||||
throw new TypeError('Unable to convert RichEmbed color to a number.');
|
||||
}
|
||||
this.color = color;
|
||||
this.color = ClientDataResolver.resolveColor(color);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -180,7 +180,7 @@ class Role {
|
||||
* The data for a role
|
||||
* @typedef {Object} RoleData
|
||||
* @property {string} [name] The name of the role
|
||||
* @property {number|string} [color] The color of the role, either a hex string or a base 10 number
|
||||
* @property {ColorResolvable} [color] The color of the role, either a hex string or a base 10 number
|
||||
* @property {boolean} [hoist] Whether or not the role should be hoisted
|
||||
* @property {number} [position] The position of the role
|
||||
* @property {string[]} [permissions] The permissions of the role
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const path = require('path');
|
||||
const escapeMarkdown = require('../util/EscapeMarkdown');
|
||||
|
||||
/**
|
||||
* Represents a webhook
|
||||
@@ -59,21 +58,73 @@ class Webhook {
|
||||
*/
|
||||
this.channelID = data.channel_id;
|
||||
|
||||
/**
|
||||
* The owner of the webhook
|
||||
* @type {User}
|
||||
*/
|
||||
if (data.user) this.owner = data.user;
|
||||
if (data.user) {
|
||||
/**
|
||||
* The owner of the webhook
|
||||
* @type {?User|Object}
|
||||
*/
|
||||
this.owner = this.client.users ? this.client.users.get(data.user.id) : data.user;
|
||||
} else {
|
||||
this.owner = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Options that can be passed into sendMessage, sendTTSMessage, sendFile, sendCode
|
||||
* Options that can be passed into send, sendMessage, sendFile, sendEmbed, and sendCode
|
||||
* @typedef {Object} WebhookMessageOptions
|
||||
* @property {string} [username=this.name] Username override for the message
|
||||
* @property {string} [avatarURL] Avatar URL override for the message
|
||||
* @property {boolean} [tts=false] Whether or not the message should be spoken aloud
|
||||
* @property {boolean} [disableEveryone=this.options.disableEveryone] Whether or not @everyone and @here
|
||||
* @property {string} [nonce=''] The nonce for the message
|
||||
* @property {Object[]} [embeds] An array of embeds for the message
|
||||
* (see [here](https://discordapp.com/developers/docs/resources/channel#embed-object) for more details)
|
||||
* @property {boolean} [disableEveryone=this.client.options.disableEveryone] Whether or not @everyone and @here
|
||||
* should be replaced with plain-text
|
||||
* @property {FileOptions|string} [file] A file to send with the message
|
||||
* @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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Send a message with this webhook
|
||||
* @param {StringResolvable} content The content to send.
|
||||
* @param {WebhookMessageOptions} [options={}] The options to provide.
|
||||
* @returns {Promise<Message|Message[]>}
|
||||
* @example
|
||||
* // send a message
|
||||
* webhook.send('hello!')
|
||||
* .then(message => console.log(`Sent message: ${message.content}`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
send(content, options) {
|
||||
if (!options && typeof content === 'object' && !(content instanceof Array)) {
|
||||
options = content;
|
||||
content = '';
|
||||
} else if (!options) {
|
||||
options = {};
|
||||
}
|
||||
if (options.file) {
|
||||
if (typeof options.file === 'string') options.file = { attachment: options.file };
|
||||
if (!options.file.name) {
|
||||
if (typeof options.file.attachment === 'string') {
|
||||
options.file.name = path.basename(options.file.attachment);
|
||||
} else if (options.file.attachment && options.file.attachment.path) {
|
||||
options.file.name = path.basename(options.file.attachment.path);
|
||||
} else {
|
||||
options.file.name = 'file.jpg';
|
||||
}
|
||||
}
|
||||
return this.client.resolver.resolveBuffer(options.file.attachment).then(file =>
|
||||
this.client.rest.methods.sendWebhookMessage(this, content, options, {
|
||||
file,
|
||||
name: options.file.name,
|
||||
})
|
||||
);
|
||||
}
|
||||
return this.client.rest.methods.sendWebhookMessage(this, content, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message with this webhook
|
||||
* @param {StringResolvable} content The content to send.
|
||||
@@ -86,7 +137,30 @@ class Webhook {
|
||||
* .catch(console.error);
|
||||
*/
|
||||
sendMessage(content, options = {}) {
|
||||
return this.client.rest.methods.sendWebhookMessage(this, content, options);
|
||||
return this.send(content, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a file with this webhook
|
||||
* @param {BufferResolvable} attachment The file to send
|
||||
* @param {string} [name='file.jpg'] The name and extension of the file
|
||||
* @param {StringResolvable} [content] Text message to send with the attachment
|
||||
* @param {WebhookMessageOptions} [options] The options to provide
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
sendFile(attachment, name, content, options = {}) {
|
||||
return this.send(content, Object.assign(options, { file: { attachment, name } }));
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a code block with this webhook
|
||||
* @param {string} lang Language for the code block
|
||||
* @param {StringResolvable} content Content of the code block
|
||||
* @param {WebhookMessageOptions} options The options to provide
|
||||
* @returns {Promise<Message|Message[]>}
|
||||
*/
|
||||
sendCode(lang, content, options = {}) {
|
||||
return this.send(content, Object.assign(options, { code: lang }));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,65 +184,6 @@ class Webhook {
|
||||
return this.client.rest.methods.sendSlackWebhookMessage(this, body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a text-to-speech message with this webhook
|
||||
* @param {StringResolvable} content The content to send
|
||||
* @param {WebhookMessageOptions} [options={}] The options to provide
|
||||
* @returns {Promise<Message|Message[]>}
|
||||
* @example
|
||||
* // send a TTS message
|
||||
* webhook.sendTTSMessage('hello!')
|
||||
* .then(message => console.log(`Sent tts message: ${message.content}`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
sendTTSMessage(content, options = {}) {
|
||||
Object.assign(options, { tts: true });
|
||||
return this.client.rest.methods.sendWebhookMessage(this, content, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a file with this webhook
|
||||
* @param {BufferResolvable} attachment The file to send
|
||||
* @param {string} [fileName="file.jpg"] The name and extension of the file
|
||||
* @param {StringResolvable} [content] Text message to send with the attachment
|
||||
* @param {WebhookMessageOptions} [options] The options to provide
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
sendFile(attachment, fileName, content, options = {}) {
|
||||
if (!fileName) {
|
||||
if (typeof attachment === 'string') {
|
||||
fileName = path.basename(attachment);
|
||||
} else if (attachment && attachment.path) {
|
||||
fileName = path.basename(attachment.path);
|
||||
} else {
|
||||
fileName = 'file.jpg';
|
||||
}
|
||||
}
|
||||
return this.client.resolver.resolveBuffer(attachment).then(file =>
|
||||
this.client.rest.methods.sendWebhookMessage(this, content, options, {
|
||||
file,
|
||||
name: fileName,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a code block with this webhook
|
||||
* @param {string} lang Language for the code block
|
||||
* @param {StringResolvable} content Content of the code block
|
||||
* @param {WebhookMessageOptions} options The options to provide
|
||||
* @returns {Promise<Message|Message[]>}
|
||||
*/
|
||||
sendCode(lang, content, options = {}) {
|
||||
if (options.split) {
|
||||
if (typeof options.split !== 'object') options.split = {};
|
||||
if (!options.split.prepend) options.split.prepend = `\`\`\`${lang || ''}\n`;
|
||||
if (!options.split.append) options.split.append = '\n```';
|
||||
}
|
||||
content = escapeMarkdown(this.client.resolver.resolveString(content), true);
|
||||
return this.sendMessage(`\`\`\`${lang || ''}\n${content}\n\`\`\``, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit the webhook.
|
||||
* @param {string} name The new name for the Webhook
|
||||
|
||||
@@ -23,11 +23,11 @@ class TextBasedChannel {
|
||||
}
|
||||
|
||||
/**
|
||||
* Options that can be passed into send, sendMessage, sendFile, sendEmbed, sendCode, and Message#reply
|
||||
* Options provided when sending or editing a message
|
||||
* @typedef {Object} MessageOptions
|
||||
* @property {boolean} [tts=false] Whether or not the message should be spoken aloud
|
||||
* @property {string} [nonce=''] The nonce for the message
|
||||
* @property {Object} [embed] An embed for the message
|
||||
* @property {RichEmbed|Object} [embed] An embed for the message
|
||||
* (see [here](https://discordapp.com/developers/docs/resources/channel#embed-object) for more details)
|
||||
* @property {boolean} [disableEveryone=this.client.options.disableEveryone] Whether or not @everyone and @here
|
||||
* should be replaced with plain-text
|
||||
@@ -35,12 +35,13 @@ 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 {UserResolvable} [reply] User to reply to (prefixes the message with a mention, except in DMs)
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} FileOptions
|
||||
* @property {BufferResolvable} attachment
|
||||
* @property {string} [name='file.jpg']
|
||||
* @property {BufferResolvable} attachment File to attach
|
||||
* @property {string} [name='file.jpg'] Filename of the attachment
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -70,6 +71,7 @@ class TextBasedChannel {
|
||||
} else if (!options) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
if (options.file) {
|
||||
if (typeof options.file === 'string') options.file = { attachment: options.file };
|
||||
if (!options.file.name) {
|
||||
@@ -81,6 +83,7 @@ class TextBasedChannel {
|
||||
options.file.name = 'file.jpg';
|
||||
}
|
||||
}
|
||||
|
||||
return this.client.resolver.resolveBuffer(options.file.attachment).then(file =>
|
||||
this.client.rest.methods.sendMessage(this, content, options, {
|
||||
file,
|
||||
@@ -88,12 +91,13 @@ class TextBasedChannel {
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return this.client.rest.methods.sendMessage(this, content, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to this channel
|
||||
* @param {StringResolvable} content Text for the message
|
||||
* @param {StringResolvable} [content] Text for the message
|
||||
* @param {MessageOptions} [options={}] Options for the message
|
||||
* @returns {Promise<Message|Message[]>}
|
||||
* @example
|
||||
@@ -114,7 +118,7 @@ class TextBasedChannel {
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
sendEmbed(embed, content, options) {
|
||||
if (!options && typeof content === 'object') {
|
||||
if (!options && typeof content === 'object' && !(content instanceof Array)) {
|
||||
options = content;
|
||||
content = '';
|
||||
} else if (!options) {
|
||||
|
||||
Reference in New Issue
Block a user