From ab5e57d94b00e84c8ae3f9a89c9cd58eafa009f6 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 6 Feb 2017 14:19:03 -0500 Subject: [PATCH 01/24] Create Emoji requires a base64 data uri (#1154) --- src/structures/Guild.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/structures/Guild.js b/src/structures/Guild.js index b42645452..131e09ff4 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -687,9 +687,10 @@ class Guild { if (typeof attachment === 'string' && attachment.startsWith('data:')) { resolve(this.client.rest.methods.createEmoji(this, attachment, name, roles)); } else { - this.client.resolver.resolveBuffer(attachment).then(data => - resolve(this.client.rest.methods.createEmoji(this, data, name, roles)) - ); + this.client.resolver.resolveBuffer(attachment).then(data => { + const dataURI = this.client.resolver.resolveBase64(data); + resolve(this.client.rest.methods.createEmoji(this, dataURI, name, roles)); + }); } }); } From 7c8f534a38cda97dec0610b749741ee4f18ff57f Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Tue, 7 Feb 2017 00:19:43 -0600 Subject: [PATCH 02/24] add random color (#1175) * add random color * Update ClientDataResolver.js * Update ClientDataResolver.js --- src/client/ClientDataResolver.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/client/ClientDataResolver.js b/src/client/ClientDataResolver.js index 3138a2ff9..12742e0bd 100644 --- a/src/client/ClientDataResolver.js +++ b/src/client/ClientDataResolver.js @@ -343,6 +343,7 @@ class ClientDataResolver { * 'DARK_GREY', * 'LIGHT_GREY', * 'DARK_NAVY', + * 'RANDOM', * ] * ``` * or something like @@ -360,6 +361,7 @@ class ClientDataResolver { */ static resolveColor(color) { if (typeof color === 'string') { + if (color === 'RANDOM') return Math.floor(Math.random() * (0xFFFFFF + 1)); color = Constants.Colors[color] || parseInt(color.replace('#', ''), 16); } else if (color instanceof Array) { color = (color[0] << 16) + (color[1] << 8) + color[2]; From cc3e7b26b1b3c331672e03f935274246212a8526 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 8 Feb 2017 18:17:13 +0100 Subject: [PATCH 03/24] Add convenience function RichEmbed.addBlankField (#1139) --- src/structures/RichEmbed.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/structures/RichEmbed.js b/src/structures/RichEmbed.js index c8c504fb1..1592799dd 100644 --- a/src/structures/RichEmbed.js +++ b/src/structures/RichEmbed.js @@ -150,6 +150,15 @@ class RichEmbed { return this; } + /** + * Convenience function for `.addField('\u200B', '\u200B', inline)`. + * @param {boolean} [inline=false] Set the field to display inline + * @returns {RichEmbed} This embed + */ + addBlankField(inline = false) { + return this.addField('\u200B', '\u200B', inline); + } + /** * Set the thumbnail of this embed * @param {string} url The URL of the thumbnail From cdb911f2af9171ff9005234b1d01b8de1d5c89ab Mon Sep 17 00:00:00 2001 From: Will Nelson Date: Wed, 8 Feb 2017 09:18:10 -0800 Subject: [PATCH 04/24] update setImage documentation (#1177) --- src/structures/RichEmbed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structures/RichEmbed.js b/src/structures/RichEmbed.js index 1592799dd..bc2e95cb9 100644 --- a/src/structures/RichEmbed.js +++ b/src/structures/RichEmbed.js @@ -171,7 +171,7 @@ class RichEmbed { /** * Set the image of this embed - * @param {string} url The URL of the thumbnail + * @param {string} url The URL of the image * @returns {RichEmbed} This embed */ setImage(url) { From 2518a0f7e256b6419c85b690bc5462b7e361f6a9 Mon Sep 17 00:00:00 2001 From: Zack Campbell Date: Wed, 8 Feb 2017 11:38:57 -0600 Subject: [PATCH 05/24] Fix for incorrect oldMember in guildMemberUpdate event after addRole (#1129) * Fix for incorrect oldMember in guildMemberUpdate event after addRole `addRole` would modify the cached GuildMember rather than letting it be handled internally when a guild member update packet is received from Discord, leading to the `oldMember` and `newMember` being identical following a call to `addRole` This is currently how `addRoles` does it, and a correct oldMember is passed to the `guildMemberUpdate` event following a call to `addRoles` * Return cloned member with added/removed role So we can return a member object with the added/removed role without affecting the member object sent to `guildMemberUpdate` * Wait for guildMemberUpdate and return updated GuildMember * Fix linter errors * Remove listeners after 10 seconds --- src/client/rest/RESTMethods.js | 46 ++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index 7b7528688..ce257b210 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -406,22 +406,42 @@ class RESTMethods { } addMemberRole(member, role) { - return this.rest.makeRequest('put', Constants.Endpoints.guildMemberRole(member.guild.id, member.id, role.id), true) - .then(() => { - if (!member._roles.includes(role.id)) member._roles.push(role.id); - return member; - }); + return new Promise(resolve => { + const listener = (oldMember, newMember) => { + if (!oldMember._roles.includes(role.id) && newMember._roles.includes(role.id)) { + this.client.removeListener('guildMemberUpdate', listener); + resolve(newMember); + } + }; + + this.client.on('guildMemberUpdate', listener); + this.client.setTimeout(() => this.client.removeListener('guildMemberUpdate', listener), 10e3); + + this.rest.makeRequest( + 'put', + Constants.Endpoints.guildMemberRole(member.guild.id, member.id, role.id), + true + ); + }); } removeMemberRole(member, role) { - return this.rest.makeRequest( - 'delete', - Constants.Endpoints.guildMemberRole(member.guild.id, member.id, role.id), - true - ).then(() => { - const index = member._roles.indexOf(role.id); - if (index >= 0) member._roles.splice(index, 1); - return member; + return new Promise(resolve => { + const listener = (oldMember, newMember) => { + if (oldMember._roles.includes(role.id) && !newMember._roles.includes(role.id)) { + this.client.removeListener('guildMemberUpdate', listener); + resolve(newMember); + } + }; + + this.client.on('guildMemberUpdate', listener); + this.client.setTimeout(() => this.client.removeListener('guildMemberUpdate', listener), 10e3); + + this.rest.makeRequest( + 'delete', + Constants.Endpoints.guildMemberRole(member.guild.id, member.id, role.id), + true + ); }); } From dd6dd6fb59d747be9edb7651bc55ff9181be2ef5 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 8 Feb 2017 22:04:39 +0100 Subject: [PATCH 06/24] Added support for adding users to guild 2 (#1179) * Added support for adding users to guild added RESTMethods#AddGuildMemberOptions and Guild#addMember with typedef AddGuildMemberOptions to be able to add user to guild as a member through `PUT/guilds/{guild.id}/members/{user.id}` https://discordapp.com/developers/docs/resources/guild#add-guild-member * fixing lint errors * Changes based on discussion * Changes based on discussion 2 * Changes based on discussion 3 Yay! More changes. --- src/client/rest/RESTMethods.js | 16 ++++++++++++++++ src/structures/Guild.js | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index ce257b210..32eb154e3 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -377,6 +377,22 @@ class RESTMethods { return this.rest.makeRequest('get', Constants.Endpoints.channelMessage(channel.id, messageID), true); } + putGuildMember(guild, user, options) { + if (options.roles) { + var roles = options.roles; + if (roles instanceof Collection || (roles instanceof Array && roles[0] instanceof Role)) { + options.roles = roles.map(role => role.id); + } + } + if (options.accessToken) { + options.access_token = options.accessToken; + } else { + return Promise.reject(new Error('OAuth2 access token was not specified.')); + } + return this.rest.makeRequest('put', Constants.Endpoints.guildMember(guild.id, user.id), true, options) + .then(data => this.client.actions.GuildMemberGet.handle(guild, data).member); + } + getGuildMember(guild, user, cache) { return this.rest.makeRequest('get', Constants.Endpoints.guildMember(guild.id, user.id), true).then(data => { if (cache) { diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 131e09ff4..02e605d93 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -331,6 +331,28 @@ class Guild { return this.client.rest.methods.fetchVoiceRegions(this.id); } + /** + * The data for a role + * @typedef {Object} AddGuildMemberOptions + * @property {string} accessToken An oauth2 access token granted with the guilds.join to the bot's application + * for the user you want to add to the guild + * @property {string} [nick] Value to set users nickname to + * @property {Collection|Role[]|string[]} [roles] The roles or role IDs to add + * @property {boolean} [mute] If the user is muted + * @property {boolean} [deaf] If the user is deafened + */ + + /** + * Add a user to this guild using OAuth2 + * @param {UserResolvable|string} user The user or ID of the user to add to guild + * @param {AddGuildMemberOptions} options Options object containing the access_token + * @returns {Promise} + */ + addMember(user, options) { + if (this.members.has(user.id)) return Promise.resolve(this.members.get(user.id)); + return this.client.rest.methods.putGuildMember(this, user, options); + } + /** * Fetch a single guild member from a user. * @param {UserResolvable} user The user to fetch the member for From ec1ed15c885c2c8adf99e00cc8b09a4d2e11c174 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Sat, 11 Feb 2017 06:04:24 -0600 Subject: [PATCH 07/24] Fix request handling (#1180) * clean up ratelimiters, and disable burst until some big questions are handled * burst mode is a work * fix burst again --- src/client/rest/RequestHandlers/Burst.js | 56 +++++++++---------- src/client/rest/RequestHandlers/Sequential.js | 22 ++------ src/util/Constants.js | 2 + 3 files changed, 31 insertions(+), 49 deletions(-) diff --git a/src/client/rest/RequestHandlers/Burst.js b/src/client/rest/RequestHandlers/Burst.js index 2cc1a590c..dd33a4b52 100644 --- a/src/client/rest/RequestHandlers/Burst.js +++ b/src/client/rest/RequestHandlers/Burst.js @@ -3,8 +3,15 @@ const RequestHandler = require('./RequestHandler'); class BurstRequestHandler extends RequestHandler { constructor(restManager, endpoint) { super(restManager, endpoint); - this.requestRemaining = 1; - this.first = true; + + this.client = restManager.client; + + this.limit = Infinity; + this.resetTime = null; + this.remaining = 1; + this.timeDifference = 0; + + this.resetTimeout = null; } push(request) { @@ -12,58 +19,45 @@ class BurstRequestHandler extends RequestHandler { this.handle(); } - handleNext(time) { - if (this.waiting) return; - this.waiting = true; - this.restManager.client.setTimeout(() => { - this.requestRemaining = this.requestLimit; - this.waiting = false; - this.handle(); - }, time); - } - execute(item) { + if (!item) return; item.request.gen().end((err, res) => { if (res && res.headers) { - this.requestLimit = res.headers['x-ratelimit-limit']; - this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000; - this.requestRemaining = Number(res.headers['x-ratelimit-remaining']); + this.limit = Number(res.headers['x-ratelimit-limit']); + this.resetTime = Number(res.headers['x-ratelimit-reset']) * 1000; + this.remaining = Number(res.headers['x-ratelimit-remaining']); this.timeDifference = Date.now() - new Date(res.headers.date).getTime(); - this.handleNext( - this.requestResetTime - Date.now() + this.timeDifference + this.restManager.client.options.restTimeOffset - ); } if (err) { if (err.status === 429) { - this.requestRemaining = 0; this.queue.unshift(item); - this.restManager.client.setTimeout(() => { + if (res.headers['x-ratelimit-global']) this.globalLimit = true; + if (this.resetTimeout) return; + this.resetTimeout = this.client.setTimeout(() => { + this.remaining = this.limit; this.globalLimit = false; this.handle(); - }, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset); - if (res.headers['x-ratelimit-global']) this.globalLimit = true; + this.resetTimeout = null; + }, Number(res.headers['retry-after']) + this.client.options.restTimeOffset); } else { item.reject(err); + this.handle(); } } else { this.globalLimit = false; const data = res && res.body ? res.body : {}; item.resolve(data); - if (this.first) { - this.first = false; - this.handle(); - } + this.handle(); } }); } handle() { super.handle(); - if (this.requestRemaining < 1 || this.queue.length === 0 || this.globalLimit) return; - while (this.queue.length > 0 && this.requestRemaining > 0) { - this.execute(this.queue.shift()); - this.requestRemaining--; - } + if (this.remaining <= 0 || this.queue.length === 0 || this.globalLimit) return; + this.execute(this.queue.shift()); + this.remaining--; + this.handle(); } } diff --git a/src/client/rest/RequestHandlers/Sequential.js b/src/client/rest/RequestHandlers/Sequential.js index 0abf36db2..261240df1 100644 --- a/src/client/rest/RequestHandlers/Sequential.js +++ b/src/client/rest/RequestHandlers/Sequential.js @@ -15,12 +15,6 @@ class SequentialRequestHandler extends RequestHandler { constructor(restManager, endpoint) { super(restManager, endpoint); - /** - * Whether this rate limiter is waiting for a response from a request - * @type {boolean} - */ - this.waiting = false; - /** * The endpoint that this handler is handling * @type {string} @@ -49,27 +43,24 @@ class SequentialRequestHandler extends RequestHandler { return new Promise(resolve => { item.request.gen().end((err, res) => { if (res && res.headers) { - this.requestLimit = res.headers['x-ratelimit-limit']; + this.requestLimit = Number(res.headers['x-ratelimit-limit']); this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000; this.requestRemaining = Number(res.headers['x-ratelimit-remaining']); this.timeDifference = Date.now() - new Date(res.headers.date).getTime(); } if (err) { if (err.status === 429) { + this.queue.unshift(item); this.restManager.client.setTimeout(() => { - this.waiting = false; this.globalLimit = false; resolve(); }, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset); if (res.headers['x-ratelimit-global']) this.globalLimit = true; } else { - this.queue.shift(); - this.waiting = false; item.reject(err); resolve(err); } } else { - this.queue.shift(); this.globalLimit = false; const data = res && res.body ? res.body : {}; item.resolve(data); @@ -82,7 +73,6 @@ class SequentialRequestHandler extends RequestHandler { this.requestResetTime - Date.now() + this.timeDifference + this.restManager.client.options.restTimeOffset ); } else { - this.waiting = false; resolve(data); } } @@ -92,12 +82,8 @@ class SequentialRequestHandler extends RequestHandler { handle() { super.handle(); - - if (this.waiting || this.queue.length === 0 || this.globalLimit) return; - this.waiting = true; - - const item = this.queue[0]; - this.execute(item).then(() => this.handle()); + if (this.remaining === 0 || this.queue.length === 0 || this.globalLimit) return; + this.execute(this.queue.shift()).then(() => this.handle()); } } diff --git a/src/util/Constants.js b/src/util/Constants.js index df1c7b227..cc7569697 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -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. + * 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 * @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 From b91f8f27be3883ad6a2ba70a57472261e84343b1 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Wed, 15 Feb 2017 16:37:31 -0500 Subject: [PATCH 08/24] Fix resolveBuffer file failure behaviour --- src/client/ClientDataResolver.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/client/ClientDataResolver.js b/src/client/ClientDataResolver.js index 12742e0bd..5d79fe1b3 100644 --- a/src/client/ClientDataResolver.js +++ b/src/client/ClientDataResolver.js @@ -284,11 +284,12 @@ class ClientDataResolver { } else { const file = path.resolve(resource); fs.stat(file, (err, stats) => { - if (err) reject(err); - if (!stats || !stats.isFile()) throw new Error(`The file could not be found: ${file}`); + if (err) return reject(err); + if (!stats || !stats.isFile()) return reject(new Error(`The file could not be found: ${file}`)); fs.readFile(file, (err2, data) => { if (err2) reject(err2); else resolve(data); }); + return null; }); } }); From 8d620ac33fb3b7b02ff185f9256b7e35ddd7d810 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Wed, 15 Feb 2017 20:32:39 -0500 Subject: [PATCH 09/24] Update typings --- typings | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typings b/typings index 3dbeb51fd..0acef9491 160000 --- a/typings +++ b/typings @@ -1 +1 @@ -Subproject commit 3dbeb51fd2a0ec0ca87c4ddcf20c1c1498633762 +Subproject commit 0acef94913b3d466a74fa581dbb2cc2b2d64f526 From 63ffd8aa7cedefb82223bd9b54f706b3e9526054 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Wed, 15 Feb 2017 21:04:06 -0500 Subject: [PATCH 10/24] Clarify bufferutil scenario a bit --- README.md | 2 +- docs/general/welcome.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 574d1366e..dfc3122b6 100644 --- a/README.md +++ b/README.md @@ -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. ### Optional packages -- [bufferutil](https://www.npmjs.com/package/bufferutil) to greatly speed up the `ws` WebSocket connection (`npm install bufferutil --save`) +- [bufferutil](https://www.npmjs.com/package/bufferutil) to greatly speed up the WebSocket when *not* using uws (`npm install bufferutil --save`) - [erlpack](https://github.com/hammerandchisel/erlpack) for significantly faster WebSocket data (de)serialisation (`npm install hammerandchisel/erlpack --save`) - [uws](https://www.npmjs.com/package/uws) for a much faster WebSocket connection (`npm install uws --save`) **Note:** This package does not handle disconnects entirely correctly, which causes automatic reconnection to Discord to not function. diff --git a/docs/general/welcome.md b/docs/general/welcome.md index fa78d5741..dc8ff5861 100644 --- a/docs/general/welcome.md +++ b/docs/general/welcome.md @@ -43,7 +43,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. ### Optional packages -- [bufferutil](https://www.npmjs.com/package/bufferutil) to greatly speed up the `ws` WebSocket connection (`npm install bufferutil --save`) +- [bufferutil](https://www.npmjs.com/package/bufferutil) to greatly speed up the WebSocket when *not* using uws (`npm install bufferutil --save`) - [erlpack](https://github.com/hammerandchisel/erlpack) for significantly faster WebSocket data (de)serialisation (`npm install hammerandchisel/erlpack --save`) - [uws](https://www.npmjs.com/package/uws) for a much faster WebSocket connection (`npm install uws --save`) **Note:** This package does not handle disconnects entirely correctly, which causes automatic reconnection to Discord to not function. From 7232531eb1a60d89a3a5ce2ba660aee5ce17ac62 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Wed, 15 Feb 2017 22:25:18 -0500 Subject: [PATCH 11/24] Move all util methods into class Remove TransformMessageOptions altogether --- src/client/Client.js | 4 +- src/client/ClientDataManager.js | 6 +- src/client/ClientDataResolver.js | 6 +- src/client/WebhookClient.js | 4 +- src/client/actions/ChannelUpdate.js | 4 +- src/client/actions/GuildRoleUpdate.js | 4 +- src/client/actions/GuildUpdate.js | 4 +- src/client/actions/MessageUpdate.js | 4 +- src/client/actions/UserUpdate.js | 4 +- src/client/rest/RESTMethods.js | 57 ++++- src/client/voice/ClientVoiceManager.js | 4 +- src/client/websocket/WebSocketManager.js | 4 +- .../packets/handlers/PresenceUpdate.js | 8 +- .../packets/handlers/VoiceStateUpdate.js | 4 +- src/index.js | 22 +- src/sharding/Shard.js | 9 +- src/sharding/ShardClientUtil.js | 9 +- src/sharding/ShardingManager.js | 7 +- src/structures/Guild.js | 12 +- src/structures/Message.js | 4 +- src/structures/interface/TextBasedChannel.js | 27 +++ src/util/ArraysEqual.js | 14 -- src/util/CloneObject.js | 5 - src/util/ConvertArrayBuffer.js | 11 - src/util/EscapeMarkdown.js | 5 - src/util/FetchRecommendedShards.js | 22 -- src/util/MakeError.js | 6 - src/util/MakePlainError.js | 7 - src/util/MergeDefault.js | 12 - src/util/MoveElementInArray.js | 17 -- src/util/ParseEmoji.js | 14 -- src/util/SplitMessage.js | 16 -- src/util/TransformSearchOptions.js | 75 ------ src/util/Util.js | 213 ++++++++++++++++++ 34 files changed, 349 insertions(+), 275 deletions(-) delete mode 100644 src/util/ArraysEqual.js delete mode 100644 src/util/CloneObject.js delete mode 100644 src/util/ConvertArrayBuffer.js delete mode 100644 src/util/EscapeMarkdown.js delete mode 100644 src/util/FetchRecommendedShards.js delete mode 100644 src/util/MakeError.js delete mode 100644 src/util/MakePlainError.js delete mode 100644 src/util/MergeDefault.js delete mode 100644 src/util/MoveElementInArray.js delete mode 100644 src/util/ParseEmoji.js delete mode 100644 src/util/SplitMessage.js delete mode 100644 src/util/TransformSearchOptions.js create mode 100644 src/util/Util.js diff --git a/src/client/Client.js b/src/client/Client.js index 06b777951..46c60b92a 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -1,7 +1,7 @@ const os = require('os'); const EventEmitter = require('events').EventEmitter; -const mergeDefault = require('../util/MergeDefault'); const Constants = require('../util/Constants'); +const Util = require('../util/Util'); const RESTManager = require('./rest/RESTManager'); const ClientDataManager = require('./ClientDataManager'); const ClientManager = require('./ClientManager'); @@ -32,7 +32,7 @@ class Client extends EventEmitter { * The options the client was instantiated with * @type {ClientOptions} */ - this.options = mergeDefault(Constants.DefaultOptions, options); + this.options = Util.mergeDefault(Constants.DefaultOptions, options); this._validateOptions(); /** diff --git a/src/client/ClientDataManager.js b/src/client/ClientDataManager.js index e663e107e..a938ef37f 100644 --- a/src/client/ClientDataManager.js +++ b/src/client/ClientDataManager.js @@ -1,5 +1,5 @@ const Constants = require('../util/Constants'); -const cloneObject = require('../util/CloneObject'); +const Util = require('../util/Util'); const Guild = require('../structures/Guild'); const User = require('../structures/User'); const DMChannel = require('../structures/DMChannel'); @@ -110,7 +110,7 @@ class ClientDataManager { } updateGuild(currentGuild, newData) { - const oldGuild = cloneObject(currentGuild); + const oldGuild = Util.cloneObject(currentGuild); currentGuild.setup(newData); if (this.pastReady) this.client.emit(Constants.Events.GUILD_UPDATE, oldGuild, currentGuild); } @@ -120,7 +120,7 @@ class ClientDataManager { } updateEmoji(currentEmoji, newData) { - const oldEmoji = cloneObject(currentEmoji); + const oldEmoji = Util.cloneObject(currentEmoji); currentEmoji.setup(newData); this.client.emit(Constants.Events.GUILD_EMOJI_UPDATE, oldEmoji, currentEmoji); return currentEmoji; diff --git a/src/client/ClientDataResolver.js b/src/client/ClientDataResolver.js index 5d79fe1b3..c6403afd4 100644 --- a/src/client/ClientDataResolver.js +++ b/src/client/ClientDataResolver.js @@ -3,7 +3,7 @@ const fs = require('fs'); const request = require('superagent'); const Constants = require('../util/Constants'); -const convertArrayBuffer = require('../util/ConvertArrayBuffer'); +const convertToBuffer = require('../util/Util').convertToBuffer; const User = require('../structures/User'); const Message = require('../structures/Message'); const Guild = require('../structures/Guild'); @@ -268,7 +268,7 @@ class ClientDataResolver { */ resolveBuffer(resource) { if (resource instanceof Buffer) return Promise.resolve(resource); - if (this.client.browser && resource instanceof ArrayBuffer) return Promise.resolve(convertArrayBuffer(resource)); + if (this.client.browser && resource instanceof ArrayBuffer) return Promise.resolve(convertToBuffer(resource)); if (typeof resource === 'string') { return new Promise((resolve, reject) => { @@ -277,7 +277,7 @@ class ClientDataResolver { if (this.client.browser) req.responseType('arraybuffer'); req.end((err, res) => { if (err) return reject(err); - if (this.client.browser) return resolve(convertArrayBuffer(res.xhr.response)); + if (this.client.browser) return resolve(convertToBuffer(res.xhr.response)); if (!(res.body instanceof Buffer)) return reject(new TypeError('The response body isn\'t a Buffer.')); return resolve(res.body); }); diff --git a/src/client/WebhookClient.js b/src/client/WebhookClient.js index 68a8a94d7..da26ca8cd 100644 --- a/src/client/WebhookClient.js +++ b/src/client/WebhookClient.js @@ -1,8 +1,8 @@ const Webhook = require('../structures/Webhook'); const RESTManager = require('./rest/RESTManager'); const ClientDataResolver = require('./ClientDataResolver'); -const mergeDefault = require('../util/MergeDefault'); const Constants = require('../util/Constants'); +const Util = require('../util/Util'); /** * The Webhook Client @@ -25,7 +25,7 @@ class WebhookClient extends Webhook { * The options the client was instantiated with * @type {ClientOptions} */ - this.options = mergeDefault(Constants.DefaultOptions, options); + this.options = Util.mergeDefault(Constants.DefaultOptions, options); /** * The REST manager of the client diff --git a/src/client/actions/ChannelUpdate.js b/src/client/actions/ChannelUpdate.js index df50ed483..43158b94c 100644 --- a/src/client/actions/ChannelUpdate.js +++ b/src/client/actions/ChannelUpdate.js @@ -1,6 +1,6 @@ const Action = require('./Action'); const Constants = require('../../util/Constants'); -const cloneObject = require('../../util/CloneObject'); +const Util = require('../../util/Util'); class ChannelUpdateAction extends Action { handle(data) { @@ -8,7 +8,7 @@ class ChannelUpdateAction extends Action { const channel = client.channels.get(data.id); if (channel) { - const oldChannel = cloneObject(channel); + const oldChannel = Util.cloneObject(channel); channel.setup(data); client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel); return { diff --git a/src/client/actions/GuildRoleUpdate.js b/src/client/actions/GuildRoleUpdate.js index 8270517b6..3c455e86d 100644 --- a/src/client/actions/GuildRoleUpdate.js +++ b/src/client/actions/GuildRoleUpdate.js @@ -1,6 +1,6 @@ const Action = require('./Action'); const Constants = require('../../util/Constants'); -const cloneObject = require('../../util/CloneObject'); +const Util = require('../../util/Util'); class GuildRoleUpdateAction extends Action { handle(data) { @@ -13,7 +13,7 @@ class GuildRoleUpdateAction extends Action { const role = guild.roles.get(roleData.id); if (role) { - oldRole = cloneObject(role); + oldRole = Util.cloneObject(role); role.setup(data.role); client.emit(Constants.Events.GUILD_ROLE_UPDATE, oldRole, role); } diff --git a/src/client/actions/GuildUpdate.js b/src/client/actions/GuildUpdate.js index efda7f7df..70cf700e9 100644 --- a/src/client/actions/GuildUpdate.js +++ b/src/client/actions/GuildUpdate.js @@ -1,6 +1,6 @@ const Action = require('./Action'); const Constants = require('../../util/Constants'); -const cloneObject = require('../../util/CloneObject'); +const Util = require('../../util/Util'); class GuildUpdateAction extends Action { handle(data) { @@ -8,7 +8,7 @@ class GuildUpdateAction extends Action { const guild = client.guilds.get(data.id); if (guild) { - const oldGuild = cloneObject(guild); + const oldGuild = Util.cloneObject(guild); guild.setup(data); client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild); return { diff --git a/src/client/actions/MessageUpdate.js b/src/client/actions/MessageUpdate.js index a62c332d6..3e197bb0a 100644 --- a/src/client/actions/MessageUpdate.js +++ b/src/client/actions/MessageUpdate.js @@ -1,6 +1,6 @@ const Action = require('./Action'); const Constants = require('../../util/Constants'); -const cloneObject = require('../../util/CloneObject'); +const Util = require('../../util/Util'); class MessageUpdateAction extends Action { handle(data) { @@ -10,7 +10,7 @@ class MessageUpdateAction extends Action { if (channel) { const message = channel.messages.get(data.id); if (message) { - const oldMessage = cloneObject(message); + const oldMessage = Util.cloneObject(message); message.patch(data); message._edits.unshift(oldMessage); client.emit(Constants.Events.MESSAGE_UPDATE, oldMessage, message); diff --git a/src/client/actions/UserUpdate.js b/src/client/actions/UserUpdate.js index b361eca59..6b917d175 100644 --- a/src/client/actions/UserUpdate.js +++ b/src/client/actions/UserUpdate.js @@ -1,6 +1,6 @@ const Action = require('./Action'); const Constants = require('../../util/Constants'); -const cloneObject = require('../../util/CloneObject'); +const Util = require('../../util/Util'); class UserUpdateAction extends Action { handle(data) { @@ -14,7 +14,7 @@ class UserUpdateAction extends Action { }; } - const oldUser = cloneObject(client.user); + const oldUser = Util.cloneObject(client.user); client.user.patch(data); client.emit(Constants.Events.USER_UPDATE, oldUser, client.user); return { diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index 32eb154e3..2f7aaf5c2 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -1,11 +1,9 @@ const querystring = require('querystring'); +const long = require('long'); const Constants = require('../../util/Constants'); const Collection = require('../../util/Collection'); -const splitMessage = require('../../util/SplitMessage'); -const parseEmoji = require('../../util/ParseEmoji'); -const escapeMarkdown = require('../../util/EscapeMarkdown'); -const transformSearchOptions = require('../../util/TransformSearchOptions'); const Snowflake = require('../../util/Snowflake'); +const Util = require('../../util/Util'); const User = require('../../structures/User'); const GuildMember = require('../../structures/GuildMember'); @@ -66,7 +64,7 @@ class RESTMethods { // Wrap everything in a code block if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) { - content = escapeMarkdown(this.client.resolver.resolveString(content), true); + content = Util.escapeMarkdown(this.client.resolver.resolveString(content), true); content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``; if (split) { split.prepend = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n`; @@ -88,7 +86,7 @@ class RESTMethods { } // Split the content - if (split) content = splitMessage(content, split); + if (split) content = Util.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}>`; @@ -125,7 +123,7 @@ class RESTMethods { // Wrap everything in a code block if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) { - content = escapeMarkdown(this.client.resolver.resolveString(content), true); + content = Util.escapeMarkdown(this.client.resolver.resolveString(content), true); content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``; } @@ -168,9 +166,46 @@ class RESTMethods { } search(target, options) { - options = transformSearchOptions(options, this.client); - for (const key in options) if (options[key] === undefined) delete options[key]; + 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 = this.client.resolver.resolveChannelID(options.channel); + if (options.author) options.author = this.client.resolver.resolveUserID(options.author); + if (options.mentions) options.mentions = this.client.resolver.resolveUserID(options.options.mentions); + options = { + 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, + }; + for (const key in options) if (options[key] === undefined) delete options[key]; const queryString = (querystring.stringify(options).match(/[^=&?]+=[^=&?]+/g) || []).join('&'); let type; @@ -736,7 +771,7 @@ class RESTMethods { this.client.actions.MessageReactionAdd.handle({ user_id: this.client.user.id, message_id: message.id, - emoji: parseEmoji(emoji), + emoji: Util.parseEmoji(emoji), channel_id: message.channel.id, }).reaction ); @@ -751,7 +786,7 @@ class RESTMethods { this.client.actions.MessageReactionRemove.handle({ user_id: user, message_id: message.id, - emoji: parseEmoji(emoji), + emoji: Util.parseEmoji(emoji), channel_id: message.channel.id, }).reaction ); diff --git a/src/client/voice/ClientVoiceManager.js b/src/client/voice/ClientVoiceManager.js index 1abe30eb4..e951891fd 100644 --- a/src/client/voice/ClientVoiceManager.js +++ b/src/client/voice/ClientVoiceManager.js @@ -1,6 +1,6 @@ const Collection = require('../../util/Collection'); -const mergeDefault = require('../../util/MergeDefault'); const Constants = require('../../util/Constants'); +const Util = require('../../util/Util'); const VoiceConnection = require('./VoiceConnection'); const EventEmitter = require('events').EventEmitter; @@ -58,7 +58,7 @@ class ClientVoiceManager { throw new Error('You do not have permission to join this voice channel.'); } - options = mergeDefault({ + options = Util.mergeDefault({ guild_id: channel.guild.id, channel_id: channel.id, self_mute: false, diff --git a/src/client/websocket/WebSocketManager.js b/src/client/websocket/WebSocketManager.js index 1770430fb..79babb623 100644 --- a/src/client/websocket/WebSocketManager.js +++ b/src/client/websocket/WebSocketManager.js @@ -1,7 +1,7 @@ const browser = require('os').platform() === 'browser'; const EventEmitter = require('events').EventEmitter; const Constants = require('../../util/Constants'); -const convertArrayBuffer = require('../../util/ConvertArrayBuffer'); +const convertToBuffer = require('../../util/Util').convertToBuffer; const pako = require('pako'); const zlib = require('zlib'); const PacketManager = require('./packets/WebSocketPacketManager'); @@ -279,7 +279,7 @@ class WebSocketManager extends EventEmitter { */ parseEventData(data) { if (erlpack) { - if (data instanceof ArrayBuffer) data = convertArrayBuffer(data); + if (data instanceof ArrayBuffer) data = convertToBuffer(data); return erlpack.unpack(data); } else { if (data instanceof ArrayBuffer) data = pako.inflate(data, { to: 'string' }); diff --git a/src/client/websocket/packets/handlers/PresenceUpdate.js b/src/client/websocket/packets/handlers/PresenceUpdate.js index 6e70639a8..01af53f06 100644 --- a/src/client/websocket/packets/handlers/PresenceUpdate.js +++ b/src/client/websocket/packets/handlers/PresenceUpdate.js @@ -1,6 +1,6 @@ const AbstractHandler = require('./AbstractHandler'); const Constants = require('../../../../util/Constants'); -const cloneObject = require('../../../../util/CloneObject'); +const Util = require('../../../../util/Util'); class PresenceUpdateHandler extends AbstractHandler { handle(packet) { @@ -18,7 +18,7 @@ class PresenceUpdateHandler extends AbstractHandler { } } - const oldUser = cloneObject(user); + const oldUser = Util.cloneObject(user); user.patch(data.user); if (!user.equals(oldUser)) { client.emit(Constants.Events.USER_UPDATE, oldUser, user); @@ -40,9 +40,9 @@ class PresenceUpdateHandler extends AbstractHandler { guild._setPresence(user.id, data); return; } - const oldMember = cloneObject(member); + const oldMember = Util.cloneObject(member); if (member.presence) { - oldMember.frozenPresence = cloneObject(member.presence); + oldMember.frozenPresence = Util.cloneObject(member.presence); } guild._setPresence(user.id, data); client.emit(Constants.Events.PRESENCE_UPDATE, oldMember, member); diff --git a/src/client/websocket/packets/handlers/VoiceStateUpdate.js b/src/client/websocket/packets/handlers/VoiceStateUpdate.js index ddbfbfcbd..def598b72 100644 --- a/src/client/websocket/packets/handlers/VoiceStateUpdate.js +++ b/src/client/websocket/packets/handlers/VoiceStateUpdate.js @@ -1,7 +1,7 @@ const AbstractHandler = require('./AbstractHandler'); const Constants = require('../../../../util/Constants'); -const cloneObject = require('../../../../util/CloneObject'); +const Util = require('../../../../util/Util'); class VoiceStateUpdateHandler extends AbstractHandler { handle(packet) { @@ -12,7 +12,7 @@ class VoiceStateUpdateHandler extends AbstractHandler { if (guild) { const member = guild.members.get(data.user_id); if (member) { - const oldVoiceChannelMember = cloneObject(member); + const oldVoiceChannelMember = Util.cloneObject(member); if (member.voiceChannel && member.voiceChannel.id !== data.channel_id) { member.voiceChannel.members.delete(oldVoiceChannelMember.id); } diff --git a/src/index.js b/src/index.js index 163819de7..6808c9815 100644 --- a/src/index.js +++ b/src/index.js @@ -1,17 +1,28 @@ +const Util = require('./util/Util'); + module.exports = { + // "Root" classes (starting points) Client: require('./client/Client'), - WebhookClient: require('./client/WebhookClient'), Shard: require('./sharding/Shard'), ShardClientUtil: require('./sharding/ShardClientUtil'), ShardingManager: require('./sharding/ShardingManager'), + WebhookClient: require('./client/WebhookClient'), + // Utilities Collection: require('./util/Collection'), - splitMessage: require('./util/SplitMessage'), - escapeMarkdown: require('./util/EscapeMarkdown'), - fetchRecommendedShards: require('./util/FetchRecommendedShards'), + Constants: require('./util/Constants'), Snowflake: require('./util/Snowflake'), SnowflakeUtil: require('./util/Snowflake'), + Util: Util, + util: Util, + version: require('../package').version, + // Shortcuts to Util methods + escapeMarkdown: Util.escapeMarkdown, + fetchRecommendedShards: Util.fetchRecommendedShards, + splitMessage: Util.splitMessage, + + // Structures Channel: require('./structures/Channel'), ClientOAuth2Application: require('./structures/ClientOAuth2Application'), ClientUser: require('./structures/ClientUser'), @@ -41,9 +52,6 @@ module.exports = { User: require('./structures/User'), VoiceChannel: require('./structures/VoiceChannel'), Webhook: require('./structures/Webhook'), - - version: require('../package').version, - Constants: require('./util/Constants'), }; if (require('os').platform() === 'browser') window.Discord = module.exports; // eslint-disable-line no-undef diff --git a/src/sharding/Shard.js b/src/sharding/Shard.js index ab9b923e1..f27f8d58f 100644 --- a/src/sharding/Shard.js +++ b/src/sharding/Shard.js @@ -1,7 +1,6 @@ const childProcess = require('child_process'); const path = require('path'); -const makeError = require('../util/MakeError'); -const makePlainError = require('../util/MakePlainError'); +const Util = require('../util/Util'); /** * Represents a Shard spawned by the ShardingManager. @@ -110,7 +109,7 @@ class Shard { if (!message || message._eval !== script) return; this.process.removeListener('message', listener); this._evals.delete(script); - if (!message._error) resolve(message._result); else reject(makeError(message._error)); + if (!message._error) resolve(message._result); else reject(Util.makeError(message._error)); }; this.process.on('message', listener); @@ -136,7 +135,7 @@ class Shard { if (message._sFetchProp) { this.manager.fetchClientValues(message._sFetchProp).then( results => this.send({ _sFetchProp: message._sFetchProp, _result: results }), - err => this.send({ _sFetchProp: message._sFetchProp, _error: makePlainError(err) }) + err => this.send({ _sFetchProp: message._sFetchProp, _error: Util.makePlainError(err) }) ); return; } @@ -145,7 +144,7 @@ class Shard { if (message._sEval) { this.manager.broadcastEval(message._sEval).then( results => this.send({ _sEval: message._sEval, _result: results }), - err => this.send({ _sEval: message._sEval, _error: makePlainError(err) }) + err => this.send({ _sEval: message._sEval, _error: Util.makePlainError(err) }) ); return; } diff --git a/src/sharding/ShardClientUtil.js b/src/sharding/ShardClientUtil.js index 6449941f5..3b1c5c703 100644 --- a/src/sharding/ShardClientUtil.js +++ b/src/sharding/ShardClientUtil.js @@ -1,5 +1,4 @@ -const makeError = require('../util/MakeError'); -const makePlainError = require('../util/MakePlainError'); +const Util = require('../util/Util'); /** * Helper class for sharded clients spawned as a child process, such as from a ShardingManager @@ -59,7 +58,7 @@ class ShardClientUtil { const listener = message => { if (!message || message._sFetchProp !== prop) return; process.removeListener('message', listener); - if (!message._error) resolve(message._result); else reject(makeError(message._error)); + if (!message._error) resolve(message._result); else reject(Util.makeError(message._error)); }; process.on('message', listener); @@ -80,7 +79,7 @@ class ShardClientUtil { const listener = message => { if (!message || message._sEval !== script) return; process.removeListener('message', listener); - if (!message._error) resolve(message._result); else reject(makeError(message._error)); + if (!message._error) resolve(message._result); else reject(Util.makeError(message._error)); }; process.on('message', listener); @@ -107,7 +106,7 @@ class ShardClientUtil { try { this._respond('eval', { _eval: message._eval, _result: this.client._eval(message._eval) }); } catch (err) { - this._respond('eval', { _eval: message._eval, _error: makePlainError(err) }); + this._respond('eval', { _eval: message._eval, _error: Util.makePlainError(err) }); } } } diff --git a/src/sharding/ShardingManager.js b/src/sharding/ShardingManager.js index 671b5d7b9..c4377a630 100644 --- a/src/sharding/ShardingManager.js +++ b/src/sharding/ShardingManager.js @@ -1,10 +1,9 @@ const path = require('path'); const fs = require('fs'); const EventEmitter = require('events').EventEmitter; -const mergeDefault = require('../util/MergeDefault'); const Shard = require('./Shard'); const Collection = require('../util/Collection'); -const fetchRecommendedShards = require('../util/FetchRecommendedShards'); +const Util = require('../util/Util'); /** * This is a utility class that can be used to help you spawn shards of your Client. Each shard is completely separate @@ -23,7 +22,7 @@ class ShardingManager extends EventEmitter { */ constructor(file, options = {}) { super(); - options = mergeDefault({ + options = Util.mergeDefault({ totalShards: 'auto', respawn: true, shardArgs: [], @@ -105,7 +104,7 @@ class ShardingManager extends EventEmitter { */ spawn(amount = this.totalShards, delay = 5500) { if (amount === 'auto') { - return fetchRecommendedShards(this.token).then(count => { + return Util.fetchRecommendedShards(this.token).then(count => { this.totalShards = count; return this._spawn(count, delay); }); diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 02e605d93..a39d1adce 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -5,9 +5,7 @@ const Presence = require('./Presence').Presence; const GuildMember = require('./GuildMember'); const Constants = require('../util/Constants'); const Collection = require('../util/Collection'); -const cloneObject = require('../util/CloneObject'); -const arraysEqual = require('../util/ArraysEqual'); -const moveElementInArray = require('../util/MoveElementInArray'); +const Util = require('../util/Util'); /** * Represents a guild (or a server) on Discord. @@ -681,7 +679,7 @@ class Guild { let updatedRoles = Object.assign([], this.roles.array() .sort((r1, r2) => r1.position !== r2.position ? r1.position - r2.position : r1.id - r2.id)); - moveElementInArray(updatedRoles, role, position, relative); + Util.moveElementInArray(updatedRoles, role, position, relative); updatedRoles = updatedRoles.map((r, i) => ({ id: r.id, position: i })); return this.client.rest.methods.setRolePositions(this.id, updatedRoles); @@ -771,7 +769,7 @@ class Guild { this.memberCount === guild.member_count && this.large === guild.large && this.icon === guild.icon && - arraysEqual(this.features, guild.features) && + Util.arraysEqual(this.features, guild.features) && this.ownerID === guild.owner_id && this.verificationLevel === guild.verification_level && this.embedEnabled === guild.embed_enabled; @@ -837,12 +835,12 @@ class Guild { } _updateMember(member, data) { - const oldMember = cloneObject(member); + const oldMember = Util.cloneObject(member); if (data.roles) member._roles = data.roles; if (typeof data.nick !== 'undefined') member.nickname = data.nick; - const notSame = member.nickname !== oldMember.nickname || !arraysEqual(member._roles, oldMember._roles); + const notSame = member.nickname !== oldMember.nickname || !Util.arraysEqual(member._roles, oldMember._roles); if (this.client.ws.status === Constants.Status.READY && notSame) { /** diff --git a/src/structures/Message.js b/src/structures/Message.js index 6f4331487..646a47c21 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -1,9 +1,9 @@ const Attachment = require('./MessageAttachment'); const Embed = require('./MessageEmbed'); const MessageReaction = require('./MessageReaction'); +const Util = require('../util/Util'); const Collection = require('../util/Collection'); const Constants = require('../util/Constants'); -const escapeMarkdown = require('../util/EscapeMarkdown'); let GuildMember; /** @@ -409,7 +409,7 @@ class Message { * @returns {Promise} */ editCode(lang, content) { - content = escapeMarkdown(this.client.resolver.resolveString(content), true); + content = Util.escapeMarkdown(this.client.resolver.resolveString(content), true); return this.edit(`\`\`\`${lang || ''}\n${content}\n\`\`\``); } diff --git a/src/structures/interface/TextBasedChannel.js b/src/structures/interface/TextBasedChannel.js index 3d90b8995..e8d6689f9 100644 --- a/src/structures/interface/TextBasedChannel.js +++ b/src/structures/interface/TextBasedChannel.js @@ -223,6 +223,33 @@ class TextBasedChannel { }); } + /** + * @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) + */ + /** * Performs a search within the channel. * @param {MessageSearchOptions} [options={}] Options to pass to the search diff --git a/src/util/ArraysEqual.js b/src/util/ArraysEqual.js deleted file mode 100644 index efd8275c9..000000000 --- a/src/util/ArraysEqual.js +++ /dev/null @@ -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; -}; diff --git a/src/util/CloneObject.js b/src/util/CloneObject.js deleted file mode 100644 index 13366a7b7..000000000 --- a/src/util/CloneObject.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports = function cloneObject(obj) { - const cloned = Object.create(obj); - Object.assign(cloned, obj); - return cloned; -}; diff --git a/src/util/ConvertArrayBuffer.js b/src/util/ConvertArrayBuffer.js deleted file mode 100644 index c6bd36433..000000000 --- a/src/util/ConvertArrayBuffer.js +++ /dev/null @@ -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); -}; diff --git a/src/util/EscapeMarkdown.js b/src/util/EscapeMarkdown.js deleted file mode 100644 index 9db8c13eb..000000000 --- a/src/util/EscapeMarkdown.js +++ /dev/null @@ -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'); -}; diff --git a/src/util/FetchRecommendedShards.js b/src/util/FetchRecommendedShards.js deleted file mode 100644 index aa8514f27..000000000 --- a/src/util/FetchRecommendedShards.js +++ /dev/null @@ -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} 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; diff --git a/src/util/MakeError.js b/src/util/MakeError.js deleted file mode 100644 index bbc84dbf8..000000000 --- a/src/util/MakeError.js +++ /dev/null @@ -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; -}; diff --git a/src/util/MakePlainError.js b/src/util/MakePlainError.js deleted file mode 100644 index b409462b1..000000000 --- a/src/util/MakePlainError.js +++ /dev/null @@ -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; -}; diff --git a/src/util/MergeDefault.js b/src/util/MergeDefault.js deleted file mode 100644 index b09f9701b..000000000 --- a/src/util/MergeDefault.js +++ /dev/null @@ -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; -}; diff --git a/src/util/MoveElementInArray.js b/src/util/MoveElementInArray.js deleted file mode 100644 index 24c4a59ae..000000000 --- a/src/util/MoveElementInArray.js +++ /dev/null @@ -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; -}; diff --git a/src/util/ParseEmoji.js b/src/util/ParseEmoji.js deleted file mode 100644 index d9f7b2212..000000000 --- a/src/util/ParseEmoji.js +++ /dev/null @@ -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, - }; - } -}; diff --git a/src/util/SplitMessage.js b/src/util/SplitMessage.js deleted file mode 100644 index 3833f009a..000000000 --- a/src/util/SplitMessage.js +++ /dev/null @@ -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; -}; diff --git a/src/util/TransformSearchOptions.js b/src/util/TransformSearchOptions.js deleted file mode 100644 index a71ceb0fd..000000000 --- a/src/util/TransformSearchOptions.js +++ /dev/null @@ -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, - }; -}; diff --git a/src/util/Util.js b/src/util/Util.js new file mode 100644 index 000000000..6b5a856dd --- /dev/null +++ b/src/util/Util.js @@ -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} 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. + * Do not use. This will give your dog/cat severe untreatable cancer of the everything. RIP Fluffykins. + * @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; From 05bba9b74abb4e8c08ef376d3cdf14ee1e0bdbe0 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Sat, 18 Feb 2017 03:13:46 -0500 Subject: [PATCH 12/24] Remove old comment part --- docs/examples/avatars.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/avatars.js b/docs/examples/avatars.js index 81375c5f4..cb496c5ae 100644 --- a/docs/examples/avatars.js +++ b/docs/examples/avatars.js @@ -5,7 +5,7 @@ // import the discord.js module const Discord = require('discord.js'); -// create an instance of a Discord Client, and call it bot +// create an instance of a Discord Client const client = new Discord.Client(); // the token of your bot - https://discordapp.com/developers/applications/me From 12136f8c54d56e5c82ee6f618f4e071d7824e378 Mon Sep 17 00:00:00 2001 From: Jamelele Date: Sat, 18 Feb 2017 18:59:25 +0000 Subject: [PATCH 13/24] Welcome example (#1194) * Add user greeting example * Add welcome.js to index.yml * Reword greeting message * Update welcome.js * Rename welcome.js to greeting.js * Update index.yml --- docs/examples/greeting.js | 33 +++++++++++++++++++++++++++++++++ docs/index.yml | 2 ++ 2 files changed, 35 insertions(+) create mode 100644 docs/examples/greeting.js diff --git a/docs/examples/greeting.js b/docs/examples/greeting.js new file mode 100644 index 000000000..15ce32711 --- /dev/null +++ b/docs/examples/greeting.js @@ -0,0 +1,33 @@ +/* + A bot that welcomes new guild members when they join +*/ + +// import the discord.js module +const Discord = require('discord.js'); + +// create an instance of a Discord Client +const client = new Discord.Client(); + +// the token of your bot - https://discordapp.com/developers/applications/me +const token = 'your bot token here'; + +// the ID of the channel in which the bot will greet new users +const channelID = 'your channel ID here'; + +// the ready event is vital, it means that your bot will only start reacting to information +// from Discord _after_ ready is emitted. +client.on('ready', () => { + console.log('I am ready!'); +}); + +// create an event listener for new guild members +client.on('guildMemberAdd', member => { + // get the channel by its ID + const channel = client.channels.get(channelID); + + // send the message, mentioning the member + channel.sendMessage(`Welcome to the server, ${member}!`); +}); + +// log our bot in +client.login(token); diff --git a/docs/index.yml b/docs/index.yml index 94444f298..d35365af9 100644 --- a/docs/index.yml +++ b/docs/index.yml @@ -16,5 +16,7 @@ path: ping.js - name: Avatars path: avatars.js + - name: Server greeting + path: greeting.js - name: Webhook path: webhook.js From 1273bb42ec760b505f9bf93d0bc7ab0846fef383 Mon Sep 17 00:00:00 2001 From: Jamelele Date: Sun, 19 Feb 2017 21:06:18 +0000 Subject: [PATCH 14/24] Doc fixes (#1197) * Improve ban/unban examples * Fix example comments * Replace nondescript 'user' parameter with 'some user ID' * Update Guild.js * Update Guild.js --- src/structures/Guild.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/structures/Guild.js b/src/structures/Guild.js index a39d1adce..99b0b6012 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -572,8 +572,10 @@ class Guild { * If the GuildMember cannot be resolved, the User will instead be attempted to be resolved. If that also cannot * be resolved, the user ID will be the result. * @example - * // ban a user - * guild.ban('123123123123'); + * // ban a user by ID (or with a user/guild member object) + * guild.ban('some user ID') + * .then(user => console.log(`Banned ${user.username || user.id || user} from ${guild.name}`)) + * .catch(console.error); */ ban(user, deleteDays = 0) { return this.client.rest.methods.banGuildMember(this, user, deleteDays); @@ -584,10 +586,10 @@ class Guild { * @param {UserResolvable} user The user to unban * @returns {Promise} * @example - * // unban a user - * guild.unban('123123123123') + * // unban a user by ID (or with a user/guild member object) + * guild.unban('some user ID') * .then(user => console.log(`Unbanned ${user.username} from ${guild.name}`)) - * .catch(reject); + * .catch(console.error); */ unban(user) { return this.client.rest.methods.unbanGuildMember(this, user); From d870b27ece7cea28ce6c6ae7839608ecbc88de7b Mon Sep 17 00:00:00 2001 From: Kelvin Wu Date: Tue, 21 Feb 2017 13:49:20 -0500 Subject: [PATCH 15/24] Message#member should be a nullable. (#1205) --- src/structures/Message.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structures/Message.js b/src/structures/Message.js index 646a47c21..c59fdc66c 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -56,7 +56,7 @@ class Message { /** * Represents the author of the message as a guild member. Only available if the message comes from a guild * where the author is still a member. - * @type {GuildMember} + * @type {?GuildMember} */ this.member = this.guild ? this.guild.member(this.author) || null : null; From f2a6c1d98cb24891b70876091788d0fe959cd5e4 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Tue, 21 Feb 2017 14:02:24 -0500 Subject: [PATCH 16/24] Improve Client docs a bit more --- src/client/Client.js | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/client/Client.js b/src/client/Client.js index 46c60b92a..4751333ee 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -43,42 +43,42 @@ class Client extends EventEmitter { this.rest = new RESTManager(this); /** - * The data manager of the Client + * The data manager of the client * @type {ClientDataManager} * @private */ this.dataManager = new ClientDataManager(this); /** - * The manager of the Client + * The manager of the client * @type {ClientManager} * @private */ this.manager = new ClientManager(this); /** - * The WebSocket Manager of the Client + * The WebSocket manager of the client * @type {WebSocketManager} * @private */ this.ws = new WebSocketManager(this); /** - * The Data Resolver of the Client + * The data resolver of the client * @type {ClientDataResolver} * @private */ this.resolver = new ClientDataResolver(this); /** - * The Action Manager of the Client + * The action manager of the client * @type {ActionsManager} * @private */ this.actions = new ActionsManager(this); /** - * The Voice Manager of the Client (`null` in browsers) + * The voice manager of the client (`null` in browsers) * @type {?ClientVoiceManager} * @private */ @@ -148,8 +148,25 @@ class Client extends EventEmitter { */ this.pings = []; + /** + * Timestamp of the latest ping's start time + * @type {number} + * @private + */ this._pingTimestamp = 0; + + /** + * Timeouts set by {@link Client#setTimeout} that are still active + * @type {Set} + * @private + */ this._timeouts = new Set(); + + /** + * Intervals set by {@link Client#setInterval} that are still active + * @type {Set} + * @private + */ this._intervals = new Set(); if (this.options.messageSweepInterval > 0) { @@ -241,7 +258,7 @@ class Client extends EventEmitter { } /** - * Logs out, terminates the connection to Discord, and destroys the client + * Logs out, terminates the connection to Discord, and destroys the client. * @returns {Promise} */ destroy() { @@ -391,7 +408,7 @@ class Client extends EventEmitter { } /** - * Clears a timeout + * Clears a timeout. * @param {Timeout} timeout Timeout to cancel */ clearTimeout(timeout) { @@ -413,7 +430,7 @@ class Client extends EventEmitter { } /** - * Clears an interval + * Clears an interval. * @param {Timeout} interval Interval to cancel */ clearInterval(interval) { @@ -447,7 +464,8 @@ class Client extends EventEmitter { } /** - * Calls `eval(script)` with the client as `this`. + * Calls {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval} on a script + * with the client as `this`. * @param {string} script Script to eval * @returns {*} * @private @@ -457,7 +475,7 @@ class Client extends EventEmitter { } /** - * Validates client options + * Validates the client options. * @param {ClientOptions} [options=this.options] Options to validate * @private */ From d4a84915e682e93153a6abc58502221fcdb62ec6 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Tue, 21 Feb 2017 13:16:41 -0600 Subject: [PATCH 17/24] Add timeout/interval methods to WebhookClient (fixes #1181), and clean up docs (#1204) * fix up 1181 * Clean up documentation * Update WebhookClient.js --- src/client/WebhookClient.js | 82 ++++++++++++++++++++++++++++++++++--- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/src/client/WebhookClient.js b/src/client/WebhookClient.js index da26ca8cd..69f489cbf 100644 --- a/src/client/WebhookClient.js +++ b/src/client/WebhookClient.js @@ -10,13 +10,13 @@ const Util = require('../util/Util'); */ class WebhookClient extends Webhook { /** - * @param {string} id The id of the webhook. - * @param {string} token the token of the webhook. + * @param {string} id ID of the webhook + * @param {string} token Token of the webhook * @param {ClientOptions} [options] Options for the client * @example * // create a new webhook and send a message - * let hook = new Discord.WebhookClient('1234', 'abcdef') - * hook.sendMessage('This will send a message').catch(console.error) + * const hook = new Discord.WebhookClient('1234', 'abcdef'); + * hook.sendMessage('This will send a message').catch(console.error); */ constructor(id, token, options) { super(null, id, token); @@ -35,11 +35,83 @@ class WebhookClient extends Webhook { this.rest = new RESTManager(this); /** - * The Data Resolver of the Client + * The data resolver of the client * @type {ClientDataResolver} * @private */ this.resolver = new ClientDataResolver(this); + + /** + * Timeouts set by {@link WebhookClient#setTimeout} that are still active + * @type {Set} + * @private + */ + this._timeouts = new Set(); + + /** + * Intervals set by {@link WebhookClient#setInterval} that are still active + * @type {Set} + * @private + */ + this._intervals = new Set(); + } + + /** + * Sets a timeout that will be automatically cancelled if the client is destroyed. + * @param {Function} fn Function to execute + * @param {number} delay Time to wait before executing (in milliseconds) + * @param {...*} args Arguments for the function + * @returns {Timeout} + */ + setTimeout(fn, delay, ...args) { + const timeout = setTimeout(() => { + fn(); + this._timeouts.delete(timeout); + }, delay, ...args); + this._timeouts.add(timeout); + return timeout; + } + + /** + * Clears a timeout. + * @param {Timeout} timeout Timeout to cancel + */ + clearTimeout(timeout) { + clearTimeout(timeout); + this._timeouts.delete(timeout); + } + + /** + * Sets an interval that will be automatically cancelled if the client is destroyed. + * @param {Function} fn Function to execute + * @param {number} delay Time to wait before executing (in milliseconds) + * @param {...*} args Arguments for the function + * @returns {Timeout} + */ + setInterval(fn, delay, ...args) { + const interval = setInterval(fn, delay, ...args); + this._intervals.add(interval); + return interval; + } + + /** + * Clears an interval. + * @param {Timeout} interval Interval to cancel + */ + clearInterval(interval) { + clearInterval(interval); + this._intervals.delete(interval); + } + + + /** + * Destroys the client. + */ + destroy() { + for (const t of this._timeouts) clearTimeout(t); + for (const i of this._intervals) clearInterval(i); + this._timeouts.clear(); + this._intervals.clear(); } } From db5259cdf15bea3b77d6178e567783a7f828b1d1 Mon Sep 17 00:00:00 2001 From: Zack Campbell Date: Tue, 21 Feb 2017 13:29:37 -0600 Subject: [PATCH 18/24] Add RichEmbed#attachFile (#1202) * Add RichEmbed#attachFile Mostly for attaching local images that can be accessed within the embed image/author icon/footer icon via `attachment//filename.png` and the like. * Update docstring to reflect valid param types * Update TextBasedChannel.js * Update RichEmbed.js * Update RichEmbed.js --- src/structures/RichEmbed.js | 18 ++++++++++++++++++ src/structures/interface/TextBasedChannel.js | 2 ++ 2 files changed, 20 insertions(+) diff --git a/src/structures/RichEmbed.js b/src/structures/RichEmbed.js index bc2e95cb9..04a27ca4a 100644 --- a/src/structures/RichEmbed.js +++ b/src/structures/RichEmbed.js @@ -65,6 +65,12 @@ class RichEmbed { * @type {Object} */ this.footer = data.footer; + + /** + * File to upload alongside this Embed + * @type {string} + */ + this.file = data.file; } /** @@ -191,6 +197,18 @@ class RichEmbed { this.footer = { text, icon_url: icon }; return this; } + + /** + * Sets the file to upload alongside the embed. This file can be accessed via `attachment://fileName.extension` when + * setting an embed image or author/footer icons. Only one file may be attached. + * @param {FileOptions|string} file Local path or URL to the file to attach, or valid FileOptions for a file to attach + * @returns {RichEmbed} This embed + */ + attachFile(file) { + if (this.file) throw new RangeError('You may not upload more than one file at once.'); + this.file = file; + return this; + } } module.exports = RichEmbed; diff --git a/src/structures/interface/TextBasedChannel.js b/src/structures/interface/TextBasedChannel.js index e8d6689f9..6d66e81c8 100644 --- a/src/structures/interface/TextBasedChannel.js +++ b/src/structures/interface/TextBasedChannel.js @@ -78,6 +78,8 @@ class TextBasedChannel { options = {}; } + if (options.embed && options.embed.file) options.file = options.embed.file; + if (options.file) { if (typeof options.file === 'string') options.file = { attachment: options.file }; if (!options.file.name) { From 8d1bc30e40cde738388073d6561b33beaead2669 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Tue, 21 Feb 2017 15:07:45 -0500 Subject: [PATCH 19/24] Clean up dd6dd6f --- src/client/rest/RESTMethods.js | 8 ++------ src/structures/Guild.js | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index 2f7aaf5c2..37556069f 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -413,17 +413,13 @@ class RESTMethods { } putGuildMember(guild, user, options) { + options.access_token = options.accessToken; if (options.roles) { - var roles = options.roles; + const roles = options.roles; if (roles instanceof Collection || (roles instanceof Array && roles[0] instanceof Role)) { options.roles = roles.map(role => role.id); } } - if (options.accessToken) { - options.access_token = options.accessToken; - } else { - return Promise.reject(new Error('OAuth2 access token was not specified.')); - } return this.rest.makeRequest('put', Constants.Endpoints.guildMember(guild.id, user.id), true, options) .then(data => this.client.actions.GuildMemberGet.handle(guild, data).member); } diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 99b0b6012..a0a4a65ce 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -330,20 +330,15 @@ class Guild { } /** - * The data for a role - * @typedef {Object} AddGuildMemberOptions - * @property {string} accessToken An oauth2 access token granted with the guilds.join to the bot's application - * for the user you want to add to the guild - * @property {string} [nick] Value to set users nickname to - * @property {Collection|Role[]|string[]} [roles] The roles or role IDs to add - * @property {boolean} [mute] If the user is muted - * @property {boolean} [deaf] If the user is deafened - */ - - /** - * Add a user to this guild using OAuth2 - * @param {UserResolvable|string} user The user or ID of the user to add to guild - * @param {AddGuildMemberOptions} options Options object containing the access_token + * Adds a user to the guild using OAuth2. Requires the `CREATE_INSTANT_INVITE` permission. + * @param {UserResolvable} user User to add to the guild + * @param {Object} options Options for the addition + * @param {string} options.accessToken An OAuth2 access token for the user with the `guilds.join` scope granted to the + * bot's application + * @param {string} [options.nick] Nickname to give the member + * @param {Collection|Role[]|Snowflake[]} [options.roles] Roles to add to the member + * @param {boolean} [options.mute] Whether the member should be muted + * @param {boolean} [options.deaf] Whether the member should be deafened * @returns {Promise} */ addMember(user, options) { From b0338df7db7775cf9b652dde0d8e4f831b465a18 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Tue, 21 Feb 2017 15:09:46 -0500 Subject: [PATCH 20/24] Add more required permissions to Guild#addMember --- src/structures/Guild.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/structures/Guild.js b/src/structures/Guild.js index a0a4a65ce..28c3b87ce 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -335,10 +335,11 @@ class Guild { * @param {Object} options Options for the addition * @param {string} options.accessToken An OAuth2 access token for the user with the `guilds.join` scope granted to the * bot's application - * @param {string} [options.nick] Nickname to give the member + * @param {string} [options.nick] Nickname to give the member (requires `MANAGE_NICKNAMES`) * @param {Collection|Role[]|Snowflake[]} [options.roles] Roles to add to the member - * @param {boolean} [options.mute] Whether the member should be muted - * @param {boolean} [options.deaf] Whether the member should be deafened + * (requires `MANAGE_ROLES`) + * @param {boolean} [options.mute] Whether the member should be muted (requires `MUTE_MEMBERS`) + * @param {boolean} [options.deaf] Whether the member should be deafened (requires `DEAFEN_MEMBERS`) * @returns {Promise} */ addMember(user, options) { From d09dfa4c3722682e6b3df1334c094a46b91a6846 Mon Sep 17 00:00:00 2001 From: Programmix Date: Wed, 22 Feb 2017 12:08:47 -0800 Subject: [PATCH 21/24] Capitalize channel type constants (#1209) --- src/client/ClientDataManager.js | 6 +++--- src/structures/PartialGuildChannel.js | 2 +- src/util/Constants.js | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/client/ClientDataManager.js b/src/client/ClientDataManager.js index a938ef37f..30992fb96 100644 --- a/src/client/ClientDataManager.js +++ b/src/client/ClientDataManager.js @@ -50,15 +50,15 @@ class ClientDataManager { let channel; if (data.type === Constants.ChannelTypes.DM) { channel = new DMChannel(this.client, data); - } else if (data.type === Constants.ChannelTypes.groupDM) { + } else if (data.type === Constants.ChannelTypes.GROUP_DM) { channel = new GroupDMChannel(this.client, data); } else { guild = guild || this.client.guilds.get(data.guild_id); if (guild) { - if (data.type === Constants.ChannelTypes.text) { + if (data.type === Constants.ChannelTypes.TEXT) { channel = new TextChannel(guild, data); guild.channels.set(channel.id, channel); - } else if (data.type === Constants.ChannelTypes.voice) { + } else if (data.type === Constants.ChannelTypes.VOICE) { channel = new VoiceChannel(guild, data); guild.channels.set(channel.id, channel); } diff --git a/src/structures/PartialGuildChannel.js b/src/structures/PartialGuildChannel.js index 13cf4c2f9..4c634b981 100644 --- a/src/structures/PartialGuildChannel.js +++ b/src/structures/PartialGuildChannel.js @@ -37,7 +37,7 @@ class PartialGuildChannel { * The type of this guild channel - `text` or `voice` * @type {string} */ - this.type = Constants.ChannelTypes.text === data.type ? 'text' : 'voice'; + this.type = Constants.ChannelTypes.TEXT === data.type ? 'text' : 'voice'; } } diff --git a/src/util/Constants.js b/src/util/Constants.js index cc7569697..76bb705df 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -175,10 +175,10 @@ exports.Status = { }; exports.ChannelTypes = { - text: 0, + TEXT: 0, DM: 1, - voice: 2, - groupDM: 3, + VOICE: 2, + GROUP_DM: 3, }; exports.OPCodes = { From 61e12c637b8198c3d01551efecec2c96ed133072 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Wed, 22 Feb 2017 14:09:40 -0600 Subject: [PATCH 22/24] Add support for more OAuth features (#1203) --- src/client/Client.js | 7 ++- src/client/rest/RESTMethods.js | 16 +++++-- src/index.js | 1 - src/structures/ClientOAuth2Application.js | 26 ------------ src/structures/OAuth2Application.js | 52 ++++++++++++++++++++++- src/util/Constants.js | 2 +- 6 files changed, 67 insertions(+), 37 deletions(-) delete mode 100644 src/structures/ClientOAuth2Application.js diff --git a/src/client/Client.js b/src/client/Client.js index 4751333ee..d7ec95dfb 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -361,12 +361,11 @@ class Client extends EventEmitter { /** * Obtains the OAuth Application of the bot from Discord. - * This is only available when using a bot account. + * @param {Snowflake} [id='@me'] ID of application to fetch * @returns {Promise} */ - fetchApplication() { - if (!this.user.bot) throw new Error(Constants.Errors.NO_BOT_ACCOUNT); - return this.rest.methods.getMyApplication(); + fetchApplication(id = '@me') { + return this.rest.methods.getApplication(id); } /** diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index 37556069f..cd1987f3e 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -12,7 +12,7 @@ const Role = require('../../structures/Role'); const Invite = require('../../structures/Invite'); const Webhook = require('../../structures/Webhook'); const UserProfile = require('../../structures/UserProfile'); -const ClientOAuth2Application = require('../../structures/ClientOAuth2Application'); +const OAuth2Application = require('../../structures/OAuth2Application'); const Channel = require('../../structures/Channel'); const Guild = require('../../structures/Guild'); const VoiceRegion = require('../../structures/VoiceRegion'); @@ -799,12 +799,20 @@ class RESTMethods { ); } - getMyApplication() { - return this.rest.makeRequest('get', Constants.Endpoints.myApplication, true).then(app => - new ClientOAuth2Application(this.client, app) + getApplication(id) { + return this.rest.makeRequest('get', Constants.Endpoints.oauth2Application(id), true).then(app => + new OAuth2Application(this.client, app) ); } + resetApplication(id) { + return this.rest.makeRequest( + 'post', + `${Constants.Endpoints.oauth2Application(id)}/reset`, + true + ).then(app => new OAuth2Application(this.client, app)); + } + setNote(user, note) { return this.rest.makeRequest('put', Constants.Endpoints.note(user.id), true, { note }).then(() => user); } diff --git a/src/index.js b/src/index.js index 6808c9815..2b1345e34 100644 --- a/src/index.js +++ b/src/index.js @@ -24,7 +24,6 @@ module.exports = { // Structures Channel: require('./structures/Channel'), - ClientOAuth2Application: require('./structures/ClientOAuth2Application'), ClientUser: require('./structures/ClientUser'), DMChannel: require('./structures/DMChannel'), Emoji: require('./structures/Emoji'), diff --git a/src/structures/ClientOAuth2Application.js b/src/structures/ClientOAuth2Application.js deleted file mode 100644 index 46e125040..000000000 --- a/src/structures/ClientOAuth2Application.js +++ /dev/null @@ -1,26 +0,0 @@ -const User = require('./User'); -const OAuth2Application = require('./OAuth2Application'); - -/** - * Represents the client's OAuth2 Application - * @extends {OAuth2Application} - */ -class ClientOAuth2Application extends OAuth2Application { - setup(data) { - super.setup(data); - - /** - * The app's flags - * @type {number} - */ - this.flags = data.flags; - - /** - * The app's owner - * @type {User} - */ - this.owner = new User(this.client, data.owner); - } -} - -module.exports = ClientOAuth2Application; diff --git a/src/structures/OAuth2Application.js b/src/structures/OAuth2Application.js index cbe162466..30e9b3f34 100644 --- a/src/structures/OAuth2Application.js +++ b/src/structures/OAuth2Application.js @@ -47,9 +47,51 @@ class OAuth2Application { /** * The app's RPC origins - * @type {Array} + * @type {?string[]} */ this.rpcOrigins = data.rpc_origins; + + /** + * The app's redirect URIs + * @type {string[]} + */ + this.redirectURIs = data.redirect_uris; + + /** + * If this app's bot requires a code grant when using the oauth2 flow + * @type {boolean} + */ + this.botRequireCodeGrant = data.bot_require_code_grant; + + /** + * If this app's bot is public + * @type {boolean} + */ + this.botPublic = data.bot_public; + + /** + * If this app can use rpc + * @type {boolean} + */ + this.rpcApplicationState = data.rpc_application_state; + + /** + * Object containing basic info about this app's bot + * @type {Object} + */ + this.bot = data.bot; + + /** + * Flags for the app + * @type {number} + */ + this.flags = data.flags; + + /** + * oauth2 secret for the app + * @type {boolean} + */ + this.secret = data.secret; } /** @@ -70,6 +112,14 @@ class OAuth2Application { return new Date(this.createdTimestamp); } + /** + * Reset the app's secret and bot token + * @returns {OAuth2Application} + */ + reset() { + return this.client.rest.methods.resetApplication(this.id); + } + /** * When concatenated with a string, this automatically concatenates the app name rather than the app object. * @returns {string} diff --git a/src/util/Constants.js b/src/util/Constants.js index 76bb705df..873b881b8 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -158,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 From f01b3f922dd62ffdcc44ee3497ce27a778ed2724 Mon Sep 17 00:00:00 2001 From: Zack Campbell Date: Wed, 22 Feb 2017 14:09:49 -0600 Subject: [PATCH 23/24] Update typings submodule (#1206) --- typings | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typings b/typings index 0acef9491..997abfd2d 160000 --- a/typings +++ b/typings @@ -1 +1 @@ -Subproject commit 0acef94913b3d466a74fa581dbb2cc2b2d64f526 +Subproject commit 997abfd2d5b3fbc958f4cc11012a5fe41065aee8 From 566135d25b85209f98771adb0dd500f5a7c6a092 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Wed, 22 Feb 2017 14:11:11 -0600 Subject: [PATCH 24/24] move permission stuff to the resolver (#1185) --- src/client/ClientDataResolver.js | 14 ++++++++++++++ src/structures/Role.js | 12 +++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/client/ClientDataResolver.js b/src/client/ClientDataResolver.js index c6403afd4..ea6fe56f7 100644 --- a/src/client/ClientDataResolver.js +++ b/src/client/ClientDataResolver.js @@ -217,6 +217,20 @@ class ClientDataResolver { return bitfield; } + hasPermission(bitfield, name, explicit = false) { + const permission = this.resolvePermission(name); + if (!explicit && (bitfield & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true; + return (bitfield & permission) > 0; + } + + serializePermissions(bitfield) { + const serializedPermissions = {}; + for (const name in Constants.PermissionFlags) { + serializedPermissions[name] = this.hasPermission(bitfield, name); + } + return serializedPermissions; + } + /** * Data that can be resolved to give a string. This can be: * * A string diff --git a/src/structures/Role.js b/src/structures/Role.js index e0319d896..578406cc0 100644 --- a/src/structures/Role.js +++ b/src/structures/Role.js @@ -140,11 +140,7 @@ class Role { * console.log(role.serialize()); */ serialize() { - const serializedPermissions = {}; - for (const permissionName in Constants.PermissionFlags) { - serializedPermissions[permissionName] = this.hasPermission(permissionName); - } - return serializedPermissions; + return this.client.resolver.serializePermissions(this.permissions); } /** @@ -160,10 +156,8 @@ class Role { * console.log('This role can\'t ban members'); * } */ - hasPermission(permission, explicit = false) { - permission = this.client.resolver.resolvePermission(permission); - if (!explicit && (this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true; - return (this.permissions & permission) > 0; + hasPermission(permission, explicit) { + return this.client.resolver.hasPermission(this.permissions, permission, explicit); } /**