Merge branch 'master' into indev-prism

This commit is contained in:
Amish Shah
2017-02-22 20:37:59 +00:00
48 changed files with 738 additions and 420 deletions

View File

@@ -1,14 +0,0 @@
module.exports = function arraysEqual(a, b) {
if (a === b) return true;
if (a.length !== b.length) return false;
for (const itemInd in a) {
const item = a[itemInd];
const ind = b.indexOf(item);
if (ind) {
b.splice(ind, 1);
}
}
return b.length === 0;
};

View File

@@ -1,5 +0,0 @@
module.exports = function cloneObject(obj) {
const cloned = Object.create(obj);
Object.assign(cloned, obj);
return cloned;
};

View File

@@ -6,6 +6,8 @@ exports.Package = require('../../package.json');
* @property {string} [apiRequestMethod='sequential'] One of `sequential` or `burst`. The sequential handler executes
* all requests in the order they are triggered, whereas the burst handler runs multiple in parallel, and doesn't
* provide the guarantee of any particular order.
* <warn>Burst mode is more likely to hit a 429 ratelimit by its nature,
* be advised if you are very unlucky you could be IP banned</warn>
* @property {number} [shardId=0] ID of the shard to run
* @property {number} [shardCount=0] Total number of shards
* @property {number} [messageCacheMaxSize=200] Maximum number of messages to cache per channel
@@ -156,7 +158,7 @@ const Endpoints = exports.Endpoints = {
webhook: (webhookID, token) => `${API}/webhooks/${webhookID}${token ? `/${token}` : ''}`,
// oauth
myApplication: `${API}/oauth2/applications/@me`,
oauth2Application: (appID) => `${API}/oauth2/applications/${appID}`,
getApp: (id) => `${API}/oauth2/authorize?client_id=${id}`,
// emoji
@@ -200,10 +202,10 @@ exports.VoiceStatus = {
};
exports.ChannelTypes = {
text: 0,
TEXT: 0,
DM: 1,
voice: 2,
groupDM: 3,
VOICE: 2,
GROUP_DM: 3,
};
exports.OPCodes = {

View File

@@ -1,11 +0,0 @@
function str2ab(str) {
const buffer = new ArrayBuffer(str.length * 2);
const view = new Uint16Array(buffer);
for (var i = 0, strLen = str.length; i < strLen; i++) view[i] = str.charCodeAt(i);
return buffer;
}
module.exports = function convertArrayBuffer(x) {
if (typeof x === 'string') x = str2ab(x);
return Buffer.from(x);
};

View File

@@ -1,5 +0,0 @@
module.exports = function escapeMarkdown(text, onlyCodeBlock = false, onlyInlineCode = false) {
if (onlyCodeBlock) return text.replace(/```/g, '`\u200b``');
if (onlyInlineCode) return text.replace(/\\(`|\\)/g, '$1').replace(/(`|\\)/g, '\\$1');
return text.replace(/\\(\*|_|`|~|\\)/g, '$1').replace(/(\*|_|`|~|\\)/g, '\\$1');
};

View File

@@ -1,22 +0,0 @@
const superagent = require('superagent');
const botGateway = require('./Constants').Endpoints.botGateway;
/**
* Gets the recommended shard count from Discord
* @param {string} token Discord auth token
* @param {number} [guildsPerShard=1000] Number of guilds per shard
* @returns {Promise<number>} the recommended number of shards
*/
function fetchRecommendedShards(token, guildsPerShard = 1000) {
return new Promise((resolve, reject) => {
if (!token) throw new Error('A token must be provided.');
superagent.get(botGateway)
.set('Authorization', `Bot ${token.replace(/^Bot\s*/i, '')}`)
.end((err, res) => {
if (err) reject(err);
resolve(res.body.shards * (1000 / guildsPerShard));
});
});
}
module.exports = fetchRecommendedShards;

View File

@@ -1,6 +0,0 @@
module.exports = function makeError(obj) {
const err = new Error(obj.message);
err.name = obj.name;
err.stack = obj.stack;
return err;
};

View File

@@ -1,7 +0,0 @@
module.exports = function makePlainError(err) {
const obj = {};
obj.name = err.name;
obj.message = err.message;
obj.stack = err.stack;
return obj;
};

View File

@@ -1,12 +0,0 @@
module.exports = function merge(def, given) {
if (!given) return def;
for (const key in def) {
if (!{}.hasOwnProperty.call(given, key)) {
given[key] = def[key];
} else if (given[key] === Object(given[key])) {
given[key] = merge(def[key], given[key]);
}
}
return given;
};

View File

@@ -1,17 +0,0 @@
/**
* Moves an element in an array *in place*
* @param {Array} array Array to modify
* @param {*} element Element to move
* @param {number} newIndex Index or offset to move the element to
* @param {boolean} [offset=false] Move the element by an offset amount rather than to a set index
* @returns {Array}
*/
module.exports = function moveElementInArray(array, element, newIndex, offset = false) {
const index = array.indexOf(element);
newIndex = (offset ? index : 0) + newIndex;
if (newIndex > -1 && newIndex < array.length) {
const removedElement = array.splice(index, 1)[0];
array.splice(newIndex, 0, removedElement);
}
return array;
};

View File

@@ -1,14 +0,0 @@
module.exports = function parseEmoji(text) {
if (text.includes('%')) {
text = decodeURIComponent(text);
}
if (text.includes(':')) {
const [name, id] = text.split(':');
return { name, id };
} else {
return {
name: text,
id: null,
};
}
};

View File

@@ -1,16 +0,0 @@
module.exports = function splitMessage(text, { maxLength = 1950, char = '\n', prepend = '', append = '' } = {}) {
if (text.length <= maxLength) return text;
const splitText = text.split(char);
if (splitText.length === 1) throw new Error('Message exceeds the max length and contains no split characters.');
const messages = [''];
let msg = 0;
for (let i = 0; i < splitText.length; i++) {
if (messages[msg].length + splitText[i].length + 1 > maxLength) {
messages[msg] += append;
messages.push(prepend);
msg++;
}
messages[msg] += (messages[msg].length > 0 && messages[msg] !== prepend ? char : '') + splitText[i];
}
return messages;
};

View File

@@ -1,75 +0,0 @@
const long = require('long');
/**
* @typedef {Object} MessageSearchOptions
* @property {string} [content] Message content
* @property {string} [maxID] Maximum ID for the filter
* @property {string} [minID] Minimum ID for the filter
* @property {string} [has] One of `link`, `embed`, `file`, `video`, `image`, or `sound`,
* or add `-` to negate (e.g. `-file`)
* @property {ChannelResolvable} [channel] Channel to limit search to (only for guild search endpoint)
* @property {UserResolvable} [author] Author to limit search
* @property {string} [authorType] One of `user`, `bot`, `webhook`, or add `-` to negate (e.g. `-webhook`)
* @property {string} [sortBy='recent'] `recent` or `relevant`
* @property {string} [sortOrder='desc'] `asc` or `desc`
* @property {number} [contextSize=2] How many messages to get around the matched message (0 to 2)
* @property {number} [limit=25] Maximum number of results to get (1 to 25)
* @property {number} [offset=0] Offset the "pages" of results (since you can only see 25 at a time)
* @property {UserResolvable} [mentions] Mentioned user filter
* @property {boolean} [mentionsEveryone] If everyone is mentioned
* @property {string} [linkHostname] Filter links by hostname
* @property {string} [embedProvider] The name of an embed provider
* @property {string} [embedType] one of `image`, `video`, `url`, `rich`
* @property {string} [attachmentFilename] The name of an attachment
* @property {string} [attachmentExtension] The extension of an attachment
* @property {Date} [before] Date to find messages before
* @property {Date} [after] Date to find messages before
* @property {Date} [during] Date to find messages during (range of date to date + 24 hours)
*/
module.exports = function TransformSearchOptions(options, client) {
if (options.before) {
if (!(options.before instanceof Date)) options.before = new Date(options.before);
options.maxID = long.fromNumber(options.before.getTime() - 14200704e5).shiftLeft(22).toString();
}
if (options.after) {
if (!(options.after instanceof Date)) options.after = new Date(options.after);
options.minID = long.fromNumber(options.after.getTime() - 14200704e5).shiftLeft(22).toString();
}
if (options.during) {
if (!(options.during instanceof Date)) options.during = new Date(options.during);
const t = options.during.getTime() - 14200704e5;
options.minID = long.fromNumber(t).shiftLeft(22).toString();
options.maxID = long.fromNumber(t + 86400000).shiftLeft(22).toString();
}
if (options.channel) options.channel = client.resolver.resolveChannelID(options.channel);
if (options.author) options.author = client.resolver.resolveUserID(options.author);
if (options.mentions) options.mentions = client.resolver.resolveUserID(options.options.mentions);
return {
content: options.content,
max_id: options.maxID,
min_id: options.minID,
has: options.has,
channel_id: options.channel,
author_id: options.author,
author_type: options.authorType,
context_size: options.contextSize,
sort_by: options.sortBy,
sort_order: options.sortOrder,
limit: options.limit,
offset: options.offset,
mentions: options.mentions,
mentions_everyone: options.mentionsEveryone,
link_hostname: options.linkHostname,
embed_provider: options.embedProvider,
embed_type: options.embedType,
attachment_filename: options.attachmentFilename,
attachment_extension: options.attachmentExtension,
};
};

213
src/util/Util.js Normal file
View File

@@ -0,0 +1,213 @@
const superagent = require('superagent');
const botGateway = require('./Constants').Endpoints.botGateway;
/**
* Contains various general-purpose utility methods. These functions are also available on the base `Discord` object.
*/
class Util {
constructor() {
throw new Error(`The ${this.constructor.name} class may not be instantiated.`);
}
/**
* Splits a string into multiple chunks at a designated character that do not exceed a specific length.
* @param {string} text Content to split
* @param {SplitOptions} [options] Options controlling the behaviour of the split
* @returns {string|string[]}
*/
static splitMessage(text, { maxLength = 1950, char = '\n', prepend = '', append = '' } = {}) {
if (text.length <= maxLength) return text;
const splitText = text.split(char);
if (splitText.length === 1) throw new Error('Message exceeds the max length and contains no split characters.');
const messages = [''];
let msg = 0;
for (let i = 0; i < splitText.length; i++) {
if (messages[msg].length + splitText[i].length + 1 > maxLength) {
messages[msg] += append;
messages.push(prepend);
msg++;
}
messages[msg] += (messages[msg].length > 0 && messages[msg] !== prepend ? char : '') + splitText[i];
}
return messages;
}
/**
* Escapes any Discord-flavour markdown in a string.
* @param {string} text Content to escape
* @param {boolean} [onlyCodeBlock=false] Whether to only escape codeblocks (takes priority)
* @param {boolean} [onlyInlineCode=false] Whether to only escape inline code
* @returns {string}
*/
static escapeMarkdown(text, onlyCodeBlock = false, onlyInlineCode = false) {
if (onlyCodeBlock) return text.replace(/```/g, '`\u200b``');
if (onlyInlineCode) return text.replace(/\\(`|\\)/g, '$1').replace(/(`|\\)/g, '\\$1');
return text.replace(/\\(\*|_|`|~|\\)/g, '$1').replace(/(\*|_|`|~|\\)/g, '\\$1');
}
/**
* Gets the recommended shard count from Discord.
* @param {string} token Discord auth token
* @param {number} [guildsPerShard=1000] Number of guilds per shard
* @returns {Promise<number>} the recommended number of shards
*/
static fetchRecommendedShards(token, guildsPerShard = 1000) {
return new Promise((resolve, reject) => {
if (!token) throw new Error('A token must be provided.');
superagent.get(botGateway)
.set('Authorization', `Bot ${token.replace(/^Bot\s*/i, '')}`)
.end((err, res) => {
if (err) reject(err);
resolve(res.body.shards * (1000 / guildsPerShard));
});
});
}
/**
* Parses emoji info out of a string. The string must be one of:
* - A UTF-8 emoji (no ID)
* - A URL-encoded UTF-8 emoji (no ID)
* - A Discord custom emoji (`<:name:id>`)
* @param {string} text Emoji string to parse
* @returns {Object} Object with `name` and `id` properties
* @private
*/
static parseEmoji(text) {
if (text.includes('%')) text = decodeURIComponent(text);
if (text.includes(':')) {
const [name, id] = text.split(':');
return { name, id };
} else {
return {
name: text,
id: null,
};
}
}
/**
* Does some weird shit to test the equality of two arrays' elements.
* <warn>Do not use. This will give your dog/cat severe untreatable cancer of the everything. RIP Fluffykins.</warn>
* @param {Array<*>} a ????
* @param {Array<*>} b ?????????
* @returns {boolean}
* @private
*/
static arraysEqual(a, b) {
if (a === b) return true;
if (a.length !== b.length) return false;
for (const itemInd in a) {
const item = a[itemInd];
const ind = b.indexOf(item);
if (ind) b.splice(ind, 1);
}
return b.length === 0;
}
/**
* Shallow-copies an object with its class/prototype intact.
* @param {Object} obj Object to clone
* @returns {Object}
* @private
*/
static cloneObject(obj) {
return Object.assign(Object.create(obj), obj);
}
/**
* Sets default properties on an object that aren't already specified.
* @param {Object} def Default properties
* @param {Object} given Object to assign defaults to
* @returns {Object}
* @private
*/
static mergeDefault(def, given) {
if (!given) return def;
for (const key in def) {
if (!{}.hasOwnProperty.call(given, key)) {
given[key] = def[key];
} else if (given[key] === Object(given[key])) {
given[key] = this.mergeDefault(def[key], given[key]);
}
}
return given;
}
/**
* Converts an ArrayBuffer or string to a Buffer.
* @param {ArrayBuffer|string} ab ArrayBuffer to convert
* @returns {Buffer}
* @private
*/
static convertToBuffer(ab) {
if (typeof ab === 'string') ab = this.str2ab(ab);
return Buffer.from(ab);
}
/**
* Converts a string to an ArrayBuffer.
* @param {string} str String to convert
* @returns {ArrayBuffer}
* @private
*/
static str2ab(str) {
const buffer = new ArrayBuffer(str.length * 2);
const view = new Uint16Array(buffer);
for (var i = 0, strLen = str.length; i < strLen; i++) view[i] = str.charCodeAt(i);
return buffer;
}
/**
* Makes an Error from a plain info object
* @param {Object} obj Error info
* @param {string} obj.name Error type
* @param {string} obj.message Message for the error
* @param {string} obj.stack Stack for the error
* @returns {Error}
* @private
*/
static makeError(obj) {
const err = new Error(obj.message);
err.name = obj.name;
err.stack = obj.stack;
return err;
}
/**
* Makes a plain error info object from an Error
* @param {Error} err Error to get info from
* @returns {Object}
* @private
*/
static makePlainError(err) {
const obj = {};
obj.name = err.name;
obj.message = err.message;
obj.stack = err.stack;
return obj;
}
/**
* Moves an element in an array *in place*
* @param {Array<*>} array Array to modify
* @param {*} element Element to move
* @param {number} newIndex Index or offset to move the element to
* @param {boolean} [offset=false] Move the element by an offset amount rather than to a set index
* @returns {Array<*>}
* @private
*/
static moveElementInArray(array, element, newIndex, offset = false) {
const index = array.indexOf(element);
newIndex = (offset ? index : 0) + newIndex;
if (newIndex > -1 && newIndex < array.length) {
const removedElement = array.splice(index, 1)[0];
array.splice(newIndex, 0, removedElement);
}
return array;
}
}
module.exports = Util;