Merge branch 'master' into indev-prism

This commit is contained in:
Amish Shah
2017-01-13 18:58:37 +00:00
15 changed files with 285 additions and 160 deletions

View File

@@ -163,33 +163,33 @@ class ClientDataResolver {
* Possible strings:
* ```js
* [
* "CREATE_INSTANT_INVITE",
* "KICK_MEMBERS",
* "BAN_MEMBERS",
* "ADMINISTRATOR",
* "MANAGE_CHANNELS",
* "MANAGE_GUILD",
* "ADD_REACTIONS", // add reactions to messages
* "READ_MESSAGES",
* "SEND_MESSAGES",
* "SEND_TTS_MESSAGES",
* "MANAGE_MESSAGES",
* "EMBED_LINKS",
* "ATTACH_FILES",
* "READ_MESSAGE_HISTORY",
* "MENTION_EVERYONE",
* "EXTERNAL_EMOJIS", // use external emojis
* "CONNECT", // connect to voice
* "SPEAK", // speak on voice
* "MUTE_MEMBERS", // globally mute members on voice
* "DEAFEN_MEMBERS", // globally deafen members on voice
* "MOVE_MEMBERS", // move member's voice channels
* "USE_VAD", // use voice activity detection
* "CHANGE_NICKNAME",
* "MANAGE_NICKNAMES", // change nicknames of others
* "MANAGE_ROLES_OR_PERMISSIONS",
* "MANAGE_WEBHOOKS",
* "MANAGE_EMOJIS"
* 'CREATE_INSTANT_INVITE',
* 'KICK_MEMBERS',
* 'BAN_MEMBERS',
* 'ADMINISTRATOR',
* 'MANAGE_CHANNELS',
* 'MANAGE_GUILD',
* 'ADD_REACTIONS', // add reactions to messages
* 'READ_MESSAGES',
* 'SEND_MESSAGES',
* 'SEND_TTS_MESSAGES',
* 'MANAGE_MESSAGES',
* 'EMBED_LINKS',
* 'ATTACH_FILES',
* 'READ_MESSAGE_HISTORY',
* 'MENTION_EVERYONE',
* 'EXTERNAL_EMOJIS', // use external emojis
* 'CONNECT', // connect to voice
* 'SPEAK', // speak on voice
* 'MUTE_MEMBERS', // globally mute members on voice
* 'DEAFEN_MEMBERS', // globally deafen members on voice
* 'MOVE_MEMBERS', // move member's voice channels
* 'USE_VAD', // use voice activity detection
* 'CHANGE_NICKNAME',
* 'MANAGE_NICKNAMES', // change nicknames of others
* 'MANAGE_ROLES_OR_PERMISSIONS',
* 'MANAGE_WEBHOOKS',
* 'MANAGE_EMOJIS'
* ]
* ```
* @typedef {string|number} PermissionResolvable
@@ -317,6 +317,67 @@ class ClientDataResolver {
}
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;

View File

@@ -35,6 +35,7 @@ class ClientManager {
this.client.ws.once('close', event => {
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 === 4011) reject(new Error(Constants.Errors.SHARDING_REQUIRED));
});
this.client.once(Constants.Events.READY, () => {
resolve(token);

View File

@@ -1,16 +1,6 @@
const request = require('superagent');
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 {
constructor(rest, method, url, auth, data, file) {
this.rest = rest;
@@ -19,7 +9,17 @@ class APIRequest {
this.auth = auth;
this.data = data;
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() {

View File

@@ -1,3 +1,4 @@
const querystring = require('querystring');
const Constants = require('../../util/Constants');
const Collection = require('../../util/Collection');
const splitMessage = require('../../util/SplitMessage');
@@ -45,21 +46,37 @@ class RESTMethods {
return this.rest.makeRequest('get', Constants.Endpoints.botGateway, true);
}
sendMessage(channel, content, { tts, nonce, embed, disableEveryone, split, code } = {}, file = null) {
return new Promise((resolve, reject) => {
sendMessage(channel, content, { tts, nonce, embed, disableEveryone, split, code, reply } = {}, file = null) {
return new Promise((resolve, reject) => { // eslint-disable-line complexity
if (typeof content !== 'undefined') content = this.client.resolver.resolveString(content);
if (content) {
if (split && typeof split !== 'object') split = {};
// Wrap everything in a code block
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
content = escapeMarkdown(this.client.resolver.resolveString(content), true);
content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``;
}
// Add zero-width spaces to @everyone/@here
if (disableEveryone || (typeof disableEveryone === 'undefined' && this.client.options.disableEveryone)) {
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 => {
@@ -88,12 +105,22 @@ class RESTMethods {
});
}
updateMessage(message, content, { embed, code } = {}) {
content = this.client.resolver.resolveString(content);
updateMessage(message, content, { embed, code, reply } = {}) {
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)) {
content = escapeMarkdown(this.client.resolver.resolveString(content), true);
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, {
content, embed,
}).then(data => this.client.actions.MessageUpdate.handle(data).updated);
@@ -123,11 +150,7 @@ class RESTMethods {
search(target, options) {
options = transformSearchOptions(options, this.client);
const queryString = Object.keys(options)
.filter(k => options[k])
.map(k => [k, options[k]])
.map(x => x.join('='))
.join('&');
const queryString = querystring.stringify(options);
let type;
if (target instanceof Channel) {
@@ -436,10 +459,7 @@ class RESTMethods {
const data = {};
data.name = _data.name || role.name;
data.position = typeof _data.position !== 'undefined' ? _data.position : role.position;
data.color = _data.color || role.color;
if (typeof data.color === 'string' && data.color.startsWith('#')) {
data.color = parseInt(data.color.replace('#', ''), 16);
}
data.color = this.client.resolver.resolveColor(_data.color || role.color);
data.hoist = typeof _data.hoist !== 'undefined' ? _data.hoist : role.hoist;
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);
}
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 (content) {
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, {
username: webhook.name,
username,
avatar_url: avatarURL,
content,
tts,
file,
embeds,
});
}, file);
}
sendSlackWebhookMessage(webhook, body) {

View File

@@ -249,8 +249,7 @@ class WebSocketManager extends EventEmitter {
* @param {CloseEvent} event The WebSocket close event
*/
if (!this.reconnecting) this.client.emit(Constants.Events.DISCONNECT, event);
if (event.code === 4004) return;
if (event.code === 4010) return;
if ([4004, 4010, 4011].includes(event.code)) return;
if (!this.reconnecting && event.code !== 1000) this.tryReconnect();
}