mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 18:43:31 +01:00
Merge branch 'master' into indev-prism
This commit is contained in:
@@ -39,7 +39,7 @@ Using opusscript is only recommended for development environments where node-opu
|
|||||||
For production bots, using node-opus should be considered a necessity, especially if they're going to be running on multiple servers.
|
For production bots, using node-opus should be considered a necessity, especially if they're going to be running on multiple servers.
|
||||||
|
|
||||||
### Optional packages
|
### Optional packages
|
||||||
- [uws](https://www.npmjs.com/package/uws) for much a much faster WebSocket connection (`npm install uws --save`)
|
- [uws](https://www.npmjs.com/package/uws) for a much faster WebSocket connection (`npm install uws --save`)
|
||||||
- [erlpack](https://github.com/hammerandchisel/erlpack) for significantly faster WebSocket data (de)serialisation (`npm install hammerandchisel/erlpack --save`)
|
- [erlpack](https://github.com/hammerandchisel/erlpack) for significantly faster WebSocket data (de)serialisation (`npm install hammerandchisel/erlpack --save`)
|
||||||
|
|
||||||
## Example Usage
|
## Example Usage
|
||||||
|
|||||||
@@ -163,33 +163,33 @@ class ClientDataResolver {
|
|||||||
* Possible strings:
|
* Possible strings:
|
||||||
* ```js
|
* ```js
|
||||||
* [
|
* [
|
||||||
* "CREATE_INSTANT_INVITE",
|
* 'CREATE_INSTANT_INVITE',
|
||||||
* "KICK_MEMBERS",
|
* 'KICK_MEMBERS',
|
||||||
* "BAN_MEMBERS",
|
* 'BAN_MEMBERS',
|
||||||
* "ADMINISTRATOR",
|
* 'ADMINISTRATOR',
|
||||||
* "MANAGE_CHANNELS",
|
* 'MANAGE_CHANNELS',
|
||||||
* "MANAGE_GUILD",
|
* 'MANAGE_GUILD',
|
||||||
* "ADD_REACTIONS", // add reactions to messages
|
* 'ADD_REACTIONS', // add reactions to messages
|
||||||
* "READ_MESSAGES",
|
* 'READ_MESSAGES',
|
||||||
* "SEND_MESSAGES",
|
* 'SEND_MESSAGES',
|
||||||
* "SEND_TTS_MESSAGES",
|
* 'SEND_TTS_MESSAGES',
|
||||||
* "MANAGE_MESSAGES",
|
* 'MANAGE_MESSAGES',
|
||||||
* "EMBED_LINKS",
|
* 'EMBED_LINKS',
|
||||||
* "ATTACH_FILES",
|
* 'ATTACH_FILES',
|
||||||
* "READ_MESSAGE_HISTORY",
|
* 'READ_MESSAGE_HISTORY',
|
||||||
* "MENTION_EVERYONE",
|
* 'MENTION_EVERYONE',
|
||||||
* "EXTERNAL_EMOJIS", // use external emojis
|
* 'EXTERNAL_EMOJIS', // use external emojis
|
||||||
* "CONNECT", // connect to voice
|
* 'CONNECT', // connect to voice
|
||||||
* "SPEAK", // speak on voice
|
* 'SPEAK', // speak on voice
|
||||||
* "MUTE_MEMBERS", // globally mute members on voice
|
* 'MUTE_MEMBERS', // globally mute members on voice
|
||||||
* "DEAFEN_MEMBERS", // globally deafen members on voice
|
* 'DEAFEN_MEMBERS', // globally deafen members on voice
|
||||||
* "MOVE_MEMBERS", // move member's voice channels
|
* 'MOVE_MEMBERS', // move member's voice channels
|
||||||
* "USE_VAD", // use voice activity detection
|
* 'USE_VAD', // use voice activity detection
|
||||||
* "CHANGE_NICKNAME",
|
* 'CHANGE_NICKNAME',
|
||||||
* "MANAGE_NICKNAMES", // change nicknames of others
|
* 'MANAGE_NICKNAMES', // change nicknames of others
|
||||||
* "MANAGE_ROLES_OR_PERMISSIONS",
|
* 'MANAGE_ROLES_OR_PERMISSIONS',
|
||||||
* "MANAGE_WEBHOOKS",
|
* 'MANAGE_WEBHOOKS',
|
||||||
* "MANAGE_EMOJIS"
|
* 'MANAGE_EMOJIS'
|
||||||
* ]
|
* ]
|
||||||
* ```
|
* ```
|
||||||
* @typedef {string|number} PermissionResolvable
|
* @typedef {string|number} PermissionResolvable
|
||||||
@@ -317,6 +317,67 @@ class ClientDataResolver {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Can be a Hex Literal, Hex String, Number, RGB Array, or one of the following
|
||||||
|
* ```
|
||||||
|
* [
|
||||||
|
* 'DEFAULT',
|
||||||
|
* 'AQUA',
|
||||||
|
* 'GREEN',
|
||||||
|
* 'BLUE',
|
||||||
|
* 'PURPLE',
|
||||||
|
* 'GOLD',
|
||||||
|
* 'ORANGE',
|
||||||
|
* 'RED',
|
||||||
|
* 'GREY',
|
||||||
|
* 'DARKER_GREY',
|
||||||
|
* 'NAVY',
|
||||||
|
* 'DARK_AQUA',
|
||||||
|
* 'DARK_GREEN',
|
||||||
|
* 'DARK_BLUE',
|
||||||
|
* 'DARK_PURPLE',
|
||||||
|
* 'DARK_GOLD',
|
||||||
|
* 'DARK_ORANGE',
|
||||||
|
* 'DARK_RED',
|
||||||
|
* 'DARK_GREY',
|
||||||
|
* 'LIGHT_GREY',
|
||||||
|
* 'DARK_NAVY',
|
||||||
|
* ]
|
||||||
|
* ```
|
||||||
|
* or something like
|
||||||
|
* ```
|
||||||
|
* [255, 0, 255]
|
||||||
|
* ```
|
||||||
|
* for purple
|
||||||
|
* @typedef {String|number|Array} ColorResolvable
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ColorResolvable} color Color to resolve
|
||||||
|
* @returns {number} A color
|
||||||
|
*/
|
||||||
|
static resolveColor(color) {
|
||||||
|
if (typeof color === 'string') {
|
||||||
|
color = Constants.Colors[color] || parseInt(color.replace('#', ''), 16);
|
||||||
|
} else if (color instanceof Array) {
|
||||||
|
color = (color[0] << 16) + (color[1] << 8) + color[2];
|
||||||
|
}
|
||||||
|
if (color < 0 || color > 0xFFFFFF) {
|
||||||
|
throw new RangeError('Color must be within the range 0 - 16777215 (0xFFFFFF).');
|
||||||
|
} else if (color && isNaN(color)) {
|
||||||
|
throw new TypeError('Unable to convert color to a number.');
|
||||||
|
}
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ColorResolvable} color Color to resolve
|
||||||
|
* @returns {number} A color
|
||||||
|
*/
|
||||||
|
resolveColor(color) {
|
||||||
|
return ClientDataResolver.resolveColor(color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = ClientDataResolver;
|
module.exports = ClientDataResolver;
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ class ClientManager {
|
|||||||
this.client.ws.once('close', event => {
|
this.client.ws.once('close', event => {
|
||||||
if (event.code === 4004) reject(new Error(Constants.Errors.BAD_LOGIN));
|
if (event.code === 4004) reject(new Error(Constants.Errors.BAD_LOGIN));
|
||||||
if (event.code === 4010) reject(new Error(Constants.Errors.INVALID_SHARD));
|
if (event.code === 4010) reject(new Error(Constants.Errors.INVALID_SHARD));
|
||||||
|
if (event.code === 4011) reject(new Error(Constants.Errors.SHARDING_REQUIRED));
|
||||||
});
|
});
|
||||||
this.client.once(Constants.Events.READY, () => {
|
this.client.once(Constants.Events.READY, () => {
|
||||||
resolve(token);
|
resolve(token);
|
||||||
|
|||||||
@@ -1,16 +1,6 @@
|
|||||||
const request = require('superagent');
|
const request = require('superagent');
|
||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
|
|
||||||
function getRoute(url) {
|
|
||||||
let route = url.split('?')[0];
|
|
||||||
if (route.includes('/channels/') || route.includes('/guilds/')) {
|
|
||||||
const startInd = route.includes('/channels/') ? route.indexOf('/channels/') : route.indexOf('/guilds/');
|
|
||||||
const majorID = route.substring(startInd).split('/')[2];
|
|
||||||
route = route.replace(/(\d{8,})/g, ':id').replace(':id', majorID);
|
|
||||||
}
|
|
||||||
return route;
|
|
||||||
}
|
|
||||||
|
|
||||||
class APIRequest {
|
class APIRequest {
|
||||||
constructor(rest, method, url, auth, data, file) {
|
constructor(rest, method, url, auth, data, file) {
|
||||||
this.rest = rest;
|
this.rest = rest;
|
||||||
@@ -19,7 +9,17 @@ class APIRequest {
|
|||||||
this.auth = auth;
|
this.auth = auth;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.file = file;
|
this.file = file;
|
||||||
this.route = getRoute(this.url);
|
this.route = this.getRoute(this.url);
|
||||||
|
}
|
||||||
|
|
||||||
|
getRoute(url) {
|
||||||
|
let route = url.split('?')[0];
|
||||||
|
if (route.includes('/channels/') || route.includes('/guilds/')) {
|
||||||
|
const startInd = route.includes('/channels/') ? route.indexOf('/channels/') : route.indexOf('/guilds/');
|
||||||
|
const majorID = route.substring(startInd).split('/')[2];
|
||||||
|
route = route.replace(/(\d{8,})/g, ':id').replace(':id', majorID);
|
||||||
|
}
|
||||||
|
return route;
|
||||||
}
|
}
|
||||||
|
|
||||||
getAuth() {
|
getAuth() {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
const querystring = require('querystring');
|
||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
const Collection = require('../../util/Collection');
|
const Collection = require('../../util/Collection');
|
||||||
const splitMessage = require('../../util/SplitMessage');
|
const splitMessage = require('../../util/SplitMessage');
|
||||||
@@ -45,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 => {
|
||||||
@@ -88,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);
|
||||||
@@ -123,11 +150,7 @@ class RESTMethods {
|
|||||||
search(target, options) {
|
search(target, options) {
|
||||||
options = transformSearchOptions(options, this.client);
|
options = transformSearchOptions(options, this.client);
|
||||||
|
|
||||||
const queryString = Object.keys(options)
|
const queryString = querystring.stringify(options);
|
||||||
.filter(k => options[k])
|
|
||||||
.map(k => [k, options[k]])
|
|
||||||
.map(x => x.join('='))
|
|
||||||
.join('&');
|
|
||||||
|
|
||||||
let type;
|
let type;
|
||||||
if (target instanceof Channel) {
|
if (target instanceof Channel) {
|
||||||
@@ -436,10 +459,7 @@ class RESTMethods {
|
|||||||
const data = {};
|
const data = {};
|
||||||
data.name = _data.name || role.name;
|
data.name = _data.name || role.name;
|
||||||
data.position = typeof _data.position !== 'undefined' ? _data.position : role.position;
|
data.position = typeof _data.position !== 'undefined' ? _data.position : role.position;
|
||||||
data.color = _data.color || role.color;
|
data.color = this.client.resolver.resolveColor(_data.color || role.color);
|
||||||
if (typeof data.color === 'string' && data.color.startsWith('#')) {
|
|
||||||
data.color = parseInt(data.color.replace('#', ''), 16);
|
|
||||||
}
|
|
||||||
data.hoist = typeof _data.hoist !== 'undefined' ? _data.hoist : role.hoist;
|
data.hoist = typeof _data.hoist !== 'undefined' ? _data.hoist : role.hoist;
|
||||||
data.mentionable = typeof _data.mentionable !== 'undefined' ? _data.mentionable : role.mentionable;
|
data.mentionable = typeof _data.mentionable !== 'undefined' ? _data.mentionable : role.mentionable;
|
||||||
|
|
||||||
@@ -565,7 +585,8 @@ class RESTMethods {
|
|||||||
return this.rest.makeRequest('delete', Constants.Endpoints.webhook(webhook.id, webhook.token), false);
|
return this.rest.makeRequest('delete', Constants.Endpoints.webhook(webhook.id, webhook.token), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendWebhookMessage(webhook, content, { avatarURL, tts, disableEveryone, embeds } = {}, file = null) {
|
sendWebhookMessage(webhook, content, { avatarURL, tts, disableEveryone, embeds, username } = {}, file = null) {
|
||||||
|
username = username || webhook.name;
|
||||||
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 (disableEveryone || (typeof disableEveryone === 'undefined' && this.client.options.disableEveryone)) {
|
if (disableEveryone || (typeof disableEveryone === 'undefined' && this.client.options.disableEveryone)) {
|
||||||
@@ -573,13 +594,12 @@ class RESTMethods {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this.rest.makeRequest('post', `${Constants.Endpoints.webhook(webhook.id, webhook.token)}?wait=true`, false, {
|
return this.rest.makeRequest('post', `${Constants.Endpoints.webhook(webhook.id, webhook.token)}?wait=true`, false, {
|
||||||
username: webhook.name,
|
username,
|
||||||
avatar_url: avatarURL,
|
avatar_url: avatarURL,
|
||||||
content,
|
content,
|
||||||
tts,
|
tts,
|
||||||
file,
|
|
||||||
embeds,
|
embeds,
|
||||||
});
|
}, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendSlackWebhookMessage(webhook, body) {
|
sendSlackWebhookMessage(webhook, body) {
|
||||||
|
|||||||
@@ -249,8 +249,7 @@ class WebSocketManager extends EventEmitter {
|
|||||||
* @param {CloseEvent} event The WebSocket close event
|
* @param {CloseEvent} event The WebSocket close event
|
||||||
*/
|
*/
|
||||||
if (!this.reconnecting) this.client.emit(Constants.Events.DISCONNECT, event);
|
if (!this.reconnecting) this.client.emit(Constants.Events.DISCONNECT, event);
|
||||||
if (event.code === 4004) return;
|
if ([4004, 4010, 4011].includes(event.code)) return;
|
||||||
if (event.code === 4010) return;
|
|
||||||
if (!this.reconnecting && event.code !== 1000) this.tryReconnect();
|
if (!this.reconnecting && event.code !== 1000) this.tryReconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -278,8 +278,8 @@ class Guild {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The `#general` GuildChannel of the server.
|
* The `#general` TextChannel of the server.
|
||||||
* @type {GuildChannel}
|
* @type {TextChannel}
|
||||||
* @readonly
|
* @readonly
|
||||||
*/
|
*/
|
||||||
get defaultChannel() {
|
get defaultChannel() {
|
||||||
|
|||||||
@@ -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 }));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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
|
* @param {Object} [data] Data to set in the rich embed
|
||||||
*/
|
*/
|
||||||
class RichEmbed {
|
class RichEmbed {
|
||||||
@@ -101,24 +103,11 @@ class RichEmbed {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the color of this embed
|
* 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
|
* @returns {RichEmbed} This embed
|
||||||
*/
|
*/
|
||||||
setColor(color) {
|
setColor(color) {
|
||||||
let radix = 10;
|
this.color = ClientDataResolver.resolveColor(color);
|
||||||
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;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ class Role {
|
|||||||
* The data for a role
|
* The data for a role
|
||||||
* @typedef {Object} RoleData
|
* @typedef {Object} RoleData
|
||||||
* @property {string} [name] The name of the role
|
* @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 {boolean} [hoist] Whether or not the role should be hoisted
|
||||||
* @property {number} [position] The position of the role
|
* @property {number} [position] The position of the role
|
||||||
* @property {string[]} [permissions] The permissions of the role
|
* @property {string[]} [permissions] The permissions of the role
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const escapeMarkdown = require('../util/EscapeMarkdown');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a webhook
|
* Represents a webhook
|
||||||
@@ -59,21 +58,73 @@ class Webhook {
|
|||||||
*/
|
*/
|
||||||
this.channelID = data.channel_id;
|
this.channelID = data.channel_id;
|
||||||
|
|
||||||
/**
|
if (data.user) {
|
||||||
* The owner of the webhook
|
/**
|
||||||
* @type {User}
|
* The owner of the webhook
|
||||||
*/
|
* @type {?User|Object}
|
||||||
if (data.user) this.owner = data.user;
|
*/
|
||||||
|
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
|
* @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} [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
|
* 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
|
* Send a message with this webhook
|
||||||
* @param {StringResolvable} content The content to send.
|
* @param {StringResolvable} content The content to send.
|
||||||
@@ -86,7 +137,30 @@ class Webhook {
|
|||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
sendMessage(content, options = {}) {
|
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);
|
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.
|
* Edit the webhook.
|
||||||
* @param {string} name The new name for 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
|
* @typedef {Object} MessageOptions
|
||||||
* @property {boolean} [tts=false] Whether or not the message should be spoken aloud
|
* @property {boolean} [tts=false] Whether or not the message should be spoken aloud
|
||||||
* @property {string} [nonce=''] The nonce for the message
|
* @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)
|
* (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
|
* @property {boolean} [disableEveryone=this.client.options.disableEveryone] Whether or not @everyone and @here
|
||||||
* should be replaced with plain-text
|
* should be replaced with plain-text
|
||||||
@@ -35,12 +35,13 @@ class TextBasedChannel {
|
|||||||
* @property {string|boolean} [code] Language for optional codeblock formatting to apply
|
* @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
|
* @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)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} FileOptions
|
* @typedef {Object} FileOptions
|
||||||
* @property {BufferResolvable} attachment
|
* @property {BufferResolvable} attachment File to attach
|
||||||
* @property {string} [name='file.jpg']
|
* @property {string} [name='file.jpg'] Filename of the attachment
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -70,6 +71,7 @@ class TextBasedChannel {
|
|||||||
} else if (!options) {
|
} else if (!options) {
|
||||||
options = {};
|
options = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.file) {
|
if (options.file) {
|
||||||
if (typeof options.file === 'string') options.file = { attachment: options.file };
|
if (typeof options.file === 'string') options.file = { attachment: options.file };
|
||||||
if (!options.file.name) {
|
if (!options.file.name) {
|
||||||
@@ -81,6 +83,7 @@ class TextBasedChannel {
|
|||||||
options.file.name = 'file.jpg';
|
options.file.name = 'file.jpg';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.client.resolver.resolveBuffer(options.file.attachment).then(file =>
|
return this.client.resolver.resolveBuffer(options.file.attachment).then(file =>
|
||||||
this.client.rest.methods.sendMessage(this, content, options, {
|
this.client.rest.methods.sendMessage(this, content, options, {
|
||||||
file,
|
file,
|
||||||
@@ -88,12 +91,13 @@ class TextBasedChannel {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.client.rest.methods.sendMessage(this, content, options);
|
return this.client.rest.methods.sendMessage(this, content, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a message to this channel
|
* 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
|
* @param {MessageOptions} [options={}] Options for the message
|
||||||
* @returns {Promise<Message|Message[]>}
|
* @returns {Promise<Message|Message[]>}
|
||||||
* @example
|
* @example
|
||||||
@@ -114,7 +118,7 @@ class TextBasedChannel {
|
|||||||
* @returns {Promise<Message>}
|
* @returns {Promise<Message>}
|
||||||
*/
|
*/
|
||||||
sendEmbed(embed, content, options) {
|
sendEmbed(embed, content, options) {
|
||||||
if (!options && typeof content === 'object') {
|
if (!options && typeof content === 'object' && !(content instanceof Array)) {
|
||||||
options = content;
|
options = content;
|
||||||
content = '';
|
content = '';
|
||||||
} else if (!options) {
|
} else if (!options) {
|
||||||
|
|||||||
@@ -9,17 +9,19 @@ class Collection extends Map {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Cached array for the `array()` method - will be reset to `null` whenever `set()` or `delete()` are called.
|
* Cached array for the `array()` method - will be reset to `null` whenever `set()` or `delete()` are called.
|
||||||
|
* @name Collection#_array
|
||||||
* @type {?Array}
|
* @type {?Array}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
this._array = null;
|
Object.defineProperty(this, '_array', { value: null, writable: true, configurable: true });
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cached array for the `keyArray()` method - will be reset to `null` whenever `set()` or `delete()` are called.
|
* Cached array for the `keyArray()` method - will be reset to `null` whenever `set()` or `delete()` are called.
|
||||||
|
* @name Collection#_keyArray
|
||||||
* @type {?Array}
|
* @type {?Array}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
this._keyArray = null;
|
Object.defineProperty(this, '_keyArray', { value: null, writable: true, configurable: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
set(key, val) {
|
set(key, val) {
|
||||||
|
|||||||
@@ -72,6 +72,7 @@ exports.Errors = {
|
|||||||
INVALID_RATE_LIMIT_METHOD: 'Unknown rate limiting method.',
|
INVALID_RATE_LIMIT_METHOD: 'Unknown rate limiting method.',
|
||||||
BAD_LOGIN: 'Incorrect login details were provided.',
|
BAD_LOGIN: 'Incorrect login details were provided.',
|
||||||
INVALID_SHARD: 'Invalid shard settings were provided.',
|
INVALID_SHARD: 'Invalid shard settings were provided.',
|
||||||
|
SHARDING_REQUIRED: 'This session would have handled too many guilds - Sharding is required.',
|
||||||
INVALID_TOKEN: 'An invalid token was provided.',
|
INVALID_TOKEN: 'An invalid token was provided.',
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -367,6 +368,34 @@ const PermissionFlags = exports.PermissionFlags = {
|
|||||||
MANAGE_EMOJIS: 1 << 30,
|
MANAGE_EMOJIS: 1 << 30,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.Colors = {
|
||||||
|
DEFAULT: 0x000000,
|
||||||
|
AQUA: 0x1ABC9C,
|
||||||
|
GREEN: 0x2ECC71,
|
||||||
|
BLUE: 0x3498DB,
|
||||||
|
PURPLE: 0x9B59B6,
|
||||||
|
GOLD: 0xF1C40F,
|
||||||
|
ORANGE: 0xE67E22,
|
||||||
|
RED: 0xE74C3C,
|
||||||
|
GREY: 0x95A5A6,
|
||||||
|
NAVY: 0x34495E,
|
||||||
|
DARK_AQUA: 0x11806A,
|
||||||
|
DARK_GREEN: 0x1F8B4C,
|
||||||
|
DARK_BLUE: 0x206694,
|
||||||
|
DARK_PURPLE: 0x71368A,
|
||||||
|
DARK_GOLD: 0xC27C0E,
|
||||||
|
DARK_ORANGE: 0xA84300,
|
||||||
|
DARK_RED: 0x992D22,
|
||||||
|
DARK_GREY: 0x979C9F,
|
||||||
|
DARKER_GREY: 0x7F8C8D,
|
||||||
|
LIGHT_GREY: 0xBCC0C0,
|
||||||
|
DARK_NAVY: 0x2C3E50,
|
||||||
|
BLURPLE: 0x7289DA,
|
||||||
|
GREYPLE: 0x99AAB5,
|
||||||
|
DARK_BUT_NOT_BLACK: 0x2C2F33,
|
||||||
|
NOT_QUITE_BLACK: 0x23272A,
|
||||||
|
};
|
||||||
|
|
||||||
let _ALL_PERMISSIONS = 0;
|
let _ALL_PERMISSIONS = 0;
|
||||||
for (const key in PermissionFlags) _ALL_PERMISSIONS |= PermissionFlags[key];
|
for (const key in PermissionFlags) _ALL_PERMISSIONS |= PermissionFlags[key];
|
||||||
exports.ALL_PERMISSIONS = _ALL_PERMISSIONS;
|
exports.ALL_PERMISSIONS = _ALL_PERMISSIONS;
|
||||||
|
|||||||
2
typings
2
typings
Submodule typings updated: c8b3f8b893...14c4b674cf
Reference in New Issue
Block a user