From 7232531eb1a60d89a3a5ce2ba660aee5ce17ac62 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Wed, 15 Feb 2017 22:25:18 -0500 Subject: [PATCH] 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;