diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 000000000..2ea4a252b --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,16 @@ +module.exports = { + "extends": "airbnb", + "plugins": [ + "react", + "jsx-a11y", + "import" + ], + "rules" : { + "max-len": [2, 120, 2], + "no-underscore-dangle": 0, + "global-require": 0, + "guard-for-in": 0, + "no-restricted-syntax": 0, + "no-param-reassign": 0, + } +}; \ No newline at end of file diff --git a/package.json b/package.json index 36d8e745e..09f090512 100644 --- a/package.json +++ b/package.json @@ -1,55 +1,60 @@ -{ - "name": "discord.js", - "version": "7.0.0", - "description": "A way to interface with the Discord API", - "main": "./src/index", - "scripts": { - "test": "jscs src && node test/random" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/hydrabolt/discord.js.git" - }, - "keywords": [ - "discord", - "api", - "bot", - "client", - "node", - "discordapp" - ], - "author": "Amish Shah ", - "license": "Apache-2.0", - "bugs": { - "url": "https://github.com/hydrabolt/discord.js/issues" - }, - "homepage": "https://github.com/hydrabolt/discord.js#readme", - "dependencies": { - "babel-plugin-transform-runtime": "^6.6.0", - "object.values": "^1.0.3", - "superagent": "^1.5.0", - "unpipe": "^1.0.0", - "ws": "^0.8.1" - }, - "devDependencies": { - "babel-preset-es2015": "^6.6.0", - "babel-preset-stage-3": "^6.5.0", - "grunt": "^0.4.5", - "grunt-babel": "^6.0.0", - "grunt-browserify": "^4.0.1", - "grunt-contrib-uglify": "^0.11.0", - "grunt-jscs": "^2.8.0", - "jscs": "^2.11.0", - "load-grunt-tasks": "^3.3.0" - }, - "optionalDependencies": { - "node-opus": "^0.1.11" - }, - "engines": { - "node": ">=0.12.7" - }, - "browser": { - "./src/Util/TokenCacher.js": "./src/Util/TokenCacher-shim.js", - "./lib/Util/TokenCacher.js": "./lib/Util/TokenCacher-shim.js" - } -} +{ + "name": "discord.js", + "version": "7.0.0", + "description": "A way to interface with the Discord API", + "main": "./src/index", + "scripts": { + "test": "node test/random" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/hydrabolt/discord.js.git" + }, + "keywords": [ + "discord", + "api", + "bot", + "client", + "node", + "discordapp" + ], + "author": "Amish Shah ", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/hydrabolt/discord.js/issues" + }, + "homepage": "https://github.com/hydrabolt/discord.js#readme", + "dependencies": { + "babel-plugin-transform-runtime": "^6.6.0", + "object.values": "^1.0.3", + "superagent": "^1.5.0", + "unpipe": "^1.0.0", + "ws": "^0.8.1" + }, + "devDependencies": { + "babel-preset-es2015": "^6.6.0", + "babel-preset-stage-3": "^6.5.0", + "eslint": "^3.3.0", + "eslint-config-airbnb": "^10.0.1", + "eslint-plugin-import": "^1.13.0", + "eslint-plugin-jsx-a11y": "^2.1.0", + "eslint-plugin-react": "^6.0.0", + "grunt": "^0.4.5", + "grunt-babel": "^6.0.0", + "grunt-browserify": "^4.0.1", + "grunt-contrib-uglify": "^0.11.0", + "grunt-jscs": "^2.8.0", + "jscs": "^2.11.0", + "load-grunt-tasks": "^3.3.0" + }, + "optionalDependencies": { + "node-opus": "^0.1.11" + }, + "engines": { + "node": ">=0.12.7" + }, + "browser": { + "./src/Util/TokenCacher.js": "./src/Util/TokenCacher-shim.js", + "./lib/Util/TokenCacher.js": "./lib/Util/TokenCacher-shim.js" + } +} diff --git a/src/client/Client.js b/src/client/Client.js index b47fba0c7..52ed353a2 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -1,64 +1,61 @@ -'use strict'; - -const EventEmitter = require('events').EventEmitter; -const MergeDefault = require('../util/MergeDefault'); -const Constants = require('../util/Constants'); -const RESTManager = require('./rest/RestManager'); -const ClientDataStore = require('../structures/DataStore/ClientDataStore'); -const ClientManager = require('./ClientManager'); -const ClientDataResolver = require('./ClientDataResolver'); -const WebSocketManager = require('./websocket/WebSocketManager'); -const ActionsManager = require('./actions/ActionsManager'); - -/** - * Creates a new Discord Client - * ```js - * const Discord = require("discord.js"); - * const client = new Discord.Client(); - * ``` - */ -class Client extends EventEmitter{ - - constructor(options) { - super(); - this.options = MergeDefault(Constants.DefaultOptions, options); - this.rest = new RESTManager(this); - this.store = new ClientDataStore(this); - this.manager = new ClientManager(this); - this.ws = new WebSocketManager(this); - this.resolver = new ClientDataResolver(this); - this.actions = new ActionsManager(this); - } - - /** - * Logs the client in. If successful, resolves with the account's token. - * @param {string} emailOrToken The email or token used for the account. If it is an email, a password _must_ be - * provided. - * @param {string} [password] The password for the account, only needed if an email was provided. - * @return {Promise} - * @example - * client.login("token"); - * // or - * client.login("email", "password"); - */ - login(email, password) { - if (password) { - // login with email and password - return this.rest.methods.LoginEmailPassword(email, password); - } else { - // login with token - return this.rest.methods.LoginToken(email); - } - } - - /** - * The User of the logged in Client, only available after `READY` has been fired. - * @return {ClientUser} [description] - */ - get user() { - return this.store.user; - } - -} - -module.exports = Client; +const EventEmitter = require('events').EventEmitter; +const mergeDefault = require('../util/MergeDefault'); +const Constants = require('../util/Constants'); +const RESTManager = require('./rest/RESTManager'); +const ClientDataStore = require('../structures/datastore/ClientDataStore'); +const ClientManager = require('./ClientManager'); +const ClientDataResolver = require('./ClientDataResolver'); +const WebSocketManager = require('./websocket/WebSocketManager'); +const ActionsManager = require('./actions/ActionsManager'); + +/** + * Creates a new Discord Client + * ```js + * const Discord = require("discord.js"); + * const client = new Discord.Client(); + * ``` + */ +class Client extends EventEmitter { + + constructor(options) { + super(); + this.options = mergeDefault(Constants.DefaultOptions, options); + this.rest = new RESTManager(this); + this.store = new ClientDataStore(this); + this.manager = new ClientManager(this); + this.ws = new WebSocketManager(this); + this.resolver = new ClientDataResolver(this); + this.actions = new ActionsManager(this); + } + + /** + * Logs the client in. If successful, resolves with the account's token. + * @param {string} emailOrToken The email or token used for the account. If it is an email, a password _must_ be + * provided. + * @param {string} [password] The password for the account, only needed if an email was provided. + * @return {Promise} + * @example + * client.login("token"); + * // or + * client.login("email", "password"); + */ + login(email, password) { + if (password) { + // login with email and password + return this.rest.methods.loginEmailPassword(email, password); + } + // login with token + return this.rest.methods.loginToken(email); + } + + /** + * The User of the logged in Client, only available after `READY` has been fired. + * @return {ClientUser} [description] + */ + get user() { + return this.store.user; + } + +} + +module.exports = Client; diff --git a/src/client/ClientDataResolver.js b/src/client/ClientDataResolver.js index fc65010cf..e7003f468 100644 --- a/src/client/ClientDataResolver.js +++ b/src/client/ClientDataResolver.js @@ -1,90 +1,90 @@ -'use strict'; +const getStructure = name => require(`../structures/${name}`); -const Structure = name => require(`../structures/${name}`); - -const User = Structure('User'); -const Message = Structure('Message'); -const Guild = Structure('Guild'); -const Channel = Structure('Channel'); -const ServerChannel = Structure('ServerChannel'); -const TextChannel = Structure('TextChannel'); -const VoiceChannel = Structure('VoiceChannel'); -const GuildMember = Structure('GuildMember'); +const User = getStructure('User'); +const Message = getStructure('Message'); +const Guild = getStructure('Guild'); +const Channel = getStructure('Channel'); +const GuildMember = getStructure('GuildMember'); function $string(obj) { - return (typeof obj === 'string' || obj instanceof String); + return (typeof obj === 'string' || obj instanceof String); } class ClientDataResolver { - constructor(client) { - this.client = client; - } + constructor(client) { + this.client = client; + } - ResolveUser(user) { - if (user instanceof User) { - return user; - }else if ($string(user)) { - return this.client.store.get('users', user); - }else if (user instanceof Message) { - return user.author; - }else if (user instanceof Guild) { - return user.owner; - } + resolveUser(user) { + if (user instanceof User) { + return user; + } else if ($string(user)) { + return this.client.store.get('users', user); + } else if (user instanceof Message) { + return user.author; + } else if (user instanceof Guild) { + return user.owner; + } - return null; - } + return null; + } - ResolveGuild(guild) { - if (guild instanceof Guild) { - return guild; - } - } + resolveGuild(guild) { + if (guild instanceof Guild) { + return guild; + } + return null; + } - ResolveGuildMember(guild, user) { - if (user instanceof GuildMember) { - return user; - } + resolveGuildMember($guild, $user) { + let guild = $guild; + let user = $user; + if (user instanceof GuildMember) { + return user; + } - guild = this.ResolveGuild(guild); - user = this.ResolveUser(user); + guild = this.resolveGuild(guild); + user = this.resolveUser(user); - if (!guild || !user) { - return null; - } + if (!guild || !user) { + return null; + } - return guild.store.get('members', user.id); - } + return guild.store.get('members', user.id); + } - ResolveBase64(data) { - if (data instanceof Buffer) { - return 'data:image/jpg;base64,' + data.toString('base64'); - } + resolveBase64(data) { + if (data instanceof Buffer) { + return `data:image/jpg;base64,${data.toString('base64')}`; + } - return data; - } + return data; + } - ResolveChannel(channel) { - if (channel instanceof Channel) { - return channel; - } + resolveChannel(channel) { + if (channel instanceof Channel) { + return channel; + } - if ($string(channel)) { - return this.client.store.get('channels', channel); - } - } + if ($string(channel)) { + return this.client.store.get('channels', channel); + } - ResolveString(data) { - if (data instanceof String) { - return data; - } + return null; + } - if (data instanceof Array) { - return data.join('\n'); - } + resolveString(data) { + if (data instanceof String) { + return data; + } - return String(data); - } + if (data instanceof Array) { + return data.join('\n'); + } + + return String(data); + } } module.exports = ClientDataResolver; diff --git a/src/client/ClientManager.js b/src/client/ClientManager.js index 00ffc5c69..175e905ad 100644 --- a/src/client/ClientManager.js +++ b/src/client/ClientManager.js @@ -1,34 +1,32 @@ -'use strict'; - const Constants = require('../util/Constants'); class ClientManager { - constructor(client) { - this.client = client; - this.heartbeatInterval = null; - } + constructor(client) { + this.client = client; + this.heartbeatInterval = null; + } - connectToWebSocket(token, resolve, reject) { - this.client.store.token = token; - this.client.rest.methods.GetGateway() - .then(gateway => { - this.client.ws.connect(gateway); - this.client.once(Constants.Events.READY, () => resolve(token)); - }) - .catch(reject); + connectToWebSocket(token, resolve, reject) { + this.client.store.token = token; + this.client.rest.methods.getGateway() + .then(gateway => { + this.client.ws.connect(gateway); + this.client.once(Constants.Events.READY, () => resolve(token)); + }) + .catch(reject); - setTimeout(() => reject(Constants.Errors.TOOK_TOO_LONG), 1000 * 15); - } + setTimeout(() => reject(Constants.Errors.TOOK_TOO_LONG), 1000 * 15); + } - setupKeepAlive(time) { - this.heartbeatInterval = setInterval(() => { - this.client.ws.send({ - op: Constants.OPCodes.HEARTBEAT, - d: Date.now(), - }); - }, time); - } + setupKeepAlive(time) { + this.heartbeatInterval = setInterval(() => { + this.client.ws.send({ + op: Constants.OPCodes.HEARTBEAT, + d: Date.now(), + }); + }, time); + } } module.exports = ClientManager; diff --git a/src/client/actions/Action.js b/src/client/actions/Action.js index 795022b6d..a92fe45fc 100644 --- a/src/client/actions/Action.js +++ b/src/client/actions/Action.js @@ -1,5 +1,3 @@ -'use strict'; - /* ABOUT ACTIONS @@ -14,14 +12,14 @@ that WebSocket events don't clash with REST methods. class GenericAction { - constructor(client) { - this.client = client; - } + constructor(client) { + this.client = client; + } - handle(data) { + handle(data) { + return data; + } - } - -}; +} module.exports = GenericAction; diff --git a/src/client/actions/ActionsManager.js b/src/client/actions/ActionsManager.js index 850c052d1..68112c7d5 100644 --- a/src/client/actions/ActionsManager.js +++ b/src/client/actions/ActionsManager.js @@ -1,30 +1,28 @@ -'use strict'; - -const requireAction = name => require(`./${name}`); - -class ActionsManager { - constructor(client) { - this.client = client; - - this.register('MessageCreate'); - this.register('MessageDelete'); - this.register('MessageUpdate'); - this.register('ChannelCreate'); - this.register('ChannelDelete'); - this.register('ChannelUpdate'); - this.register('GuildDelete'); - this.register('GuildUpdate'); - this.register('GuildMemberRemove'); - this.register('GuildRoleCreate'); - this.register('GuildRoleDelete'); - this.register('GuildRoleUpdate'); - this.register('UserUpdate'); - } - - register(name) { - let Action = requireAction(name); - this[name] = new Action(this.client); - } -} - -module.exports = ActionsManager; +const requireAction = name => require(`./${name}`); + +class ActionsManager { + constructor(client) { + this.client = client; + + this.register('MessageCreate'); + this.register('MessageDelete'); + this.register('MessageUpdate'); + this.register('ChannelCreate'); + this.register('ChannelDelete'); + this.register('ChannelUpdate'); + this.register('GuildDelete'); + this.register('GuildUpdate'); + this.register('GuildMemberRemove'); + this.register('GuildRoleCreate'); + this.register('GuildRoleDelete'); + this.register('GuildRoleUpdate'); + this.register('UserUpdate'); + } + + register(name) { + const Action = requireAction(name); + this[name] = new Action(this.client); + } +} + +module.exports = ActionsManager; diff --git a/src/client/actions/ChannelCreate.js b/src/client/actions/ChannelCreate.js index 53c1d4325..09cbec54f 100644 --- a/src/client/actions/ChannelCreate.js +++ b/src/client/actions/ChannelCreate.js @@ -1,23 +1,15 @@ -'use strict'; - const Action = require('./Action'); -const Constants = require('../../util/Constants'); -const Message = require('../../structures/Message'); class ChannelCreateAction extends Action { - constructor(client) { - super(client); - } + handle(data) { + const client = this.client; + const channel = client.store.newChannel(data); - handle(data) { - let client = this.client; - let channel = client.store.NewChannel(data); - - return { - channel, - }; - } -}; + return { + channel, + }; + } +} module.exports = ChannelCreateAction; diff --git a/src/client/actions/ChannelDelete.js b/src/client/actions/ChannelDelete.js index 36b6d0231..5ecd5c03d 100644 --- a/src/client/actions/ChannelDelete.js +++ b/src/client/actions/ChannelDelete.js @@ -1,44 +1,36 @@ -'use strict'; - -const Action = require('./Action'); -const Constants = require('../../util/Constants'); -const Message = require('../../structures/Message'); - -class ChannelDeleteAction extends Action { - - constructor(client) { - super(client); - this.timeouts = []; - this.deleted = {}; - } - - handle(data) { - let client = this.client; - let channel = client.store.get('channels', data.id); - - if (channel) { - - client.store.KillChannel(channel); - this.deleted[channel.id] = channel; - this.scheduleForDeletion(channel.id); - - } else if (this.deleted[data.id]) { - - channel = this.deleted[data.id]; - - } - - return { - channel, - }; - } - - scheduleForDeletion(id) { - this.timeouts.push( - setTimeout(() => delete this.deleted[id], - this.client.options.rest_ws_bridge_timeout) - ); - } -}; - -module.exports = ChannelDeleteAction; +const Action = require('./Action'); + +class ChannelDeleteAction extends Action { + + constructor(client) { + super(client); + this.timeouts = []; + this.deleted = {}; + } + + handle(data) { + const client = this.client; + let channel = client.store.get('channels', data.id); + + if (channel) { + client.store.killChannel(channel); + this.deleted[channel.id] = channel; + this.scheduleForDeletion(channel.id); + } else if (this.deleted[data.id]) { + channel = this.deleted[data.id]; + } + + return { + channel, + }; + } + + scheduleForDeletion(id) { + this.timeouts.push( + setTimeout(() => delete this.deleted[id], + this.client.options.rest_ws_bridge_timeout) + ); + } +} + +module.exports = ChannelDeleteAction; diff --git a/src/client/actions/ChannelUpdate.js b/src/client/actions/ChannelUpdate.js index 78d02245e..f7a855b6b 100644 --- a/src/client/actions/ChannelUpdate.js +++ b/src/client/actions/ChannelUpdate.js @@ -1,39 +1,31 @@ -'use strict'; - const Action = require('./Action'); const Constants = require('../../util/Constants'); -const CloneObject = require('../../util/CloneObject'); -const Message = require('../../structures/Message'); +const cloneObject = require('../../util/CloneObject'); class ChannelUpdateAction extends Action { - constructor(client) { - super(client); - } + handle(data) { + const client = this.client; + const channel = client.store.get('channels', data.id); - handle(data) { + if (channel) { + const oldChannel = cloneObject(channel); + channel.setup(data); + if (!oldChannel.equals(data)) { + client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel); + } - let client = this.client; - let channel = client.store.get('channels', data.id); + return { + old: oldChannel, + updated: channel, + }; + } - if (channel) { - let oldChannel = CloneObject(channel); - channel.setup(data); - if (!oldChannel.equals(data)) { - client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel); - } - - return { - old: oldChannel, - updated: channel, - }; - } - - return { - old: null, - updated: null, - }; - } -}; + return { + old: null, + updated: null, + }; + } +} module.exports = ChannelUpdateAction; diff --git a/src/client/actions/GuildDelete.js b/src/client/actions/GuildDelete.js index 271cb7b52..0e040dc4a 100644 --- a/src/client/actions/GuildDelete.js +++ b/src/client/actions/GuildDelete.js @@ -1,54 +1,49 @@ -'use strict'; - const Action = require('./Action'); const Constants = require('../../util/Constants'); -const Message = require('../../structures/Message'); class GuildDeleteAction extends Action { - constructor(client) { - super(client); - this.deleted = {}; - this.timeouts = []; - } + constructor(client) { + super(client); + this.deleted = {}; + this.timeouts = []; + } - handle(data) { + handle(data) { + const client = this.client; + let guild = client.store.get('guilds', data.id); - let client = this.client; - let guild = client.store.get('guilds', data.id); + if (guild) { + if (guild.available && data.unavailable) { + // guild is unavailable + guild.available = false; + client.emit(Constants.Events.GUILD_UNAVAILABLE, guild); - if (guild) { - if (guild.available && data.unavailable) { - // guild is unavailable - guild.available = false; - client.emit(Constants.Events.GUILD_UNAVAILABLE, guild); + // stops the GuildDelete packet thinking a guild was actually deleted, + // handles emitting of event itself + return { + guild: null, + }; + } + // delete guild + client.store.remove('guilds', guild); + this.deleted[guild.id] = guild; + this.scheduleForDeletion(guild.id); + } else if (this.deleted[data.id]) { + guild = this.deleted[data.id]; + } - // stops the GuildDelete packet thinking a guild was actually deleted, - // handles emitting of event itself - return { - guild: null, - }; - } else { - // delete guild - client.store.remove('guilds', guild); - this.deleted[guild.id] = guild; - this.scheduleForDeletion(guild.id); - } - } else if (this.deleted[data.id]) { - guild = this.deleted[data.id]; - } + return { + guild, + }; + } - return { - guild, - }; - } - - scheduleForDeletion(id) { - this.timeouts.push( - setTimeout(() => delete this.deleted[id], - this.client.options.rest_ws_bridge_timeout) - ); - } -}; + scheduleForDeletion(id) { + this.timeouts.push( + setTimeout(() => delete this.deleted[id], + this.client.options.rest_ws_bridge_timeout) + ); + } +} module.exports = GuildDeleteAction; diff --git a/src/client/actions/GuildMemberRemove.js b/src/client/actions/GuildMemberRemove.js index 7ef912467..f5e965463 100644 --- a/src/client/actions/GuildMemberRemove.js +++ b/src/client/actions/GuildMemberRemove.js @@ -1,51 +1,48 @@ -'use strict'; - const Action = require('./Action'); const Constants = require('../../util/Constants'); -const Message = require('../../structures/Message'); class GuildMemberRemoveAction extends Action { - constructor(client) { - super(client); - this.timeouts = []; - this.deleted = {}; - } + constructor(client) { + super(client); + this.timeouts = []; + this.deleted = {}; + } - handle(data) { - let client = this.client; - let guild = client.store.get('guilds', data.guild_id); - if (guild) { - let member = guild.store.get('members', data.user.id); - if (member) { - guild._removeMember(member); - this.deleted[guild.id + data.user.id] = member; - client.emit(Constants.Events.GUILD_MEMBER_REMOVE, guild, member); - this.scheduleForDeletion(guild.id, data.user.id); - } + handle(data) { + const client = this.client; + const guild = client.store.get('guilds', data.guild_id); + if (guild) { + let member = guild.store.get('members', data.user.id); + if (member) { + guild._removeMember(member); + this.deleted[guild.id + data.user.id] = member; + client.emit(Constants.Events.GUILD_MEMBER_REMOVE, guild, member); + this.scheduleForDeletion(guild.id, data.user.id); + } - if (!member) { - member = this.deleted[guild.id + data.user.id]; - } + if (!member) { + member = this.deleted[guild.id + data.user.id]; + } - return { - g: guild, - m: member, - }; - } + return { + g: guild, + m: member, + }; + } - return { - g: guild, - m: null, - }; - } + return { + g: guild, + m: null, + }; + } - scheduleForDeletion(guildID, userID) { - this.timeouts.push( - setTimeout(() => delete this.deleted[guildID + userID], - this.client.options.rest_ws_bridge_timeout) - ); - } -}; + scheduleForDeletion(guildID, userID) { + this.timeouts.push( + setTimeout(() => delete this.deleted[guildID + userID], + this.client.options.rest_ws_bridge_timeout) + ); + } +} module.exports = GuildMemberRemoveAction; diff --git a/src/client/actions/GuildRoleCreate.js b/src/client/actions/GuildRoleCreate.js index 0a929d351..601e24266 100644 --- a/src/client/actions/GuildRoleCreate.js +++ b/src/client/actions/GuildRoleCreate.js @@ -1,38 +1,31 @@ -'use strict'; - const Action = require('./Action'); const Constants = require('../../util/Constants'); const Role = require('../../structures/Role'); class GuildRoleCreate extends Action { - constructor(client) { - super(client); - } + handle(data) { + const client = this.client; + const guild = client.store.get('guilds', data.guild_id); - handle(data) { + if (guild) { + const already = guild.store.get('roles', data.role.id); + const role = new Role(guild, data.role); + guild.store.add('roles', role); - let client = this.client; - let guild = client.store.get('guilds', data.guild_id); + if (!already) { + client.emit(Constants.Events.GUILD_ROLE_CREATE, guild, role); + } - if (guild) { - let already = guild.store.get('roles', data.role.id); - let role = new Role(guild, data.role); - guild.store.add('roles', role); + return { + role, + }; + } - if (!already) { - client.emit(Constants.Events.GUILD_ROLE_CREATE, guild, role); - } - - return { - role, - }; - } - - return { - role: null, - }; - } -}; + return { + role: null, + }; + } +} module.exports = GuildRoleCreate; diff --git a/src/client/actions/GuildRoleDelete.js b/src/client/actions/GuildRoleDelete.js index 687305b30..e27f81639 100644 --- a/src/client/actions/GuildRoleDelete.js +++ b/src/client/actions/GuildRoleDelete.js @@ -1,50 +1,47 @@ -'use strict'; - const Action = require('./Action'); const Constants = require('../../util/Constants'); -const Message = require('../../structures/Message'); class GuildRoleDeleteAction extends Action { - constructor(client) { - super(client); - this.timeouts = []; - this.deleted = {}; - } + constructor(client) { + super(client); + this.timeouts = []; + this.deleted = {}; + } - handle(data) { - let client = this.client; - let guild = client.store.get('guilds', data.guild_id); + handle(data) { + const client = this.client; + const guild = client.store.get('guilds', data.guild_id); - if (guild) { - let exists = guild.store.get('roles', data.role_id); - if (exists) { - guild.store.remove('roles', data.role_id); - this.deleted[guild.id + data.role_id] = exists; - this.scheduleForDeletion(guild.id, data.role_id); - client.emit(Constants.Events.GUILD_ROLE_DELETE, guild, exists); - } + if (guild) { + let exists = guild.store.get('roles', data.role_id); + if (exists) { + guild.store.remove('roles', data.role_id); + this.deleted[guild.id + data.role_id] = exists; + this.scheduleForDeletion(guild.id, data.role_id); + client.emit(Constants.Events.GUILD_ROLE_DELETE, guild, exists); + } - if (!exists) { - exists = this.deleted[guild.id + data.role_id]; - } + if (!exists) { + exists = this.deleted[guild.id + data.role_id]; + } - return { - role: exists, - }; - } + return { + role: exists, + }; + } - return { - role: null, - }; - } + return { + role: null, + }; + } - scheduleForDeletion(guildID, roleID) { - this.timeouts.push( - setTimeout(() => delete this.deleted[guildID + roleID], - this.client.options.rest_ws_bridge_timeout) - ); - } -}; + scheduleForDeletion(guildID, roleID) { + this.timeouts.push( + setTimeout(() => delete this.deleted[guildID + roleID], + this.client.options.rest_ws_bridge_timeout) + ); + } +} module.exports = GuildRoleDeleteAction; diff --git a/src/client/actions/GuildRoleUpdate.js b/src/client/actions/GuildRoleUpdate.js index 61b269240..14a896724 100644 --- a/src/client/actions/GuildRoleUpdate.js +++ b/src/client/actions/GuildRoleUpdate.js @@ -1,44 +1,36 @@ -'use strict'; - const Action = require('./Action'); const Constants = require('../../util/Constants'); -const CloneObject = require('../../util/CloneObject'); -const Message = require('../../structures/Message'); +const cloneObject = require('../../util/CloneObject'); class GuildRoleUpdateAction extends Action { - constructor(client) { - super(client); - } + handle(data) { + const client = this.client; + const guild = client.store.get('guilds', data.guild_id); - handle(data) { + const roleData = data.role; - let client = this.client; - let guild = client.store.get('guilds', data.guild_id); + if (guild) { + let oldRole; + const existingRole = guild.store.get('roles', roleData.id); + // exists and not the same + if (existingRole && !existingRole.equals(roleData)) { + oldRole = cloneObject(existingRole); + existingRole.setup(data.role); + client.emit(Constants.Events.GUILD_ROLE_UPDATE, guild, oldRole, existingRole); + } - let roleData = data.role; + return { + old: oldRole, + updated: existingRole, + }; + } - if (guild) { - let oldRole; - let existingRole = guild.store.get('roles', roleData.id); - // exists and not the same - if (existingRole && !existingRole.equals(roleData)) { - oldRole = CloneObject(existingRole); - existingRole.setup(data.role); - client.emit(Constants.Events.GUILD_ROLE_UPDATE, guild, oldRole, existingRole); - } - - return { - old: oldRole, - updated: existingRole, - }; - } - - return { - old: null, - updated: null, - }; - } -}; + return { + old: null, + updated: null, + }; + } +} module.exports = GuildRoleUpdateAction; diff --git a/src/client/actions/GuildUpdate.js b/src/client/actions/GuildUpdate.js index 96ab7565e..a7a2775de 100644 --- a/src/client/actions/GuildUpdate.js +++ b/src/client/actions/GuildUpdate.js @@ -1,42 +1,38 @@ -'use strict'; - const Action = require('./Action'); const Constants = require('../../util/Constants'); -const CloneObject = require('../../util/CloneObject'); -const Message = require('../../structures/Message'); +const cloneObject = require('../../util/CloneObject'); class GuildUpdateAction extends Action { - constructor(client) { - super(client); - this.deleted = {}; - this.timeouts = []; - } + constructor(client) { + super(client); + this.deleted = {}; + this.timeouts = []; + } - handle(data) { + handle(data) { + const client = this.client; + const guild = client.store.get('guilds', data.id); - let client = this.client; - let guild = client.store.get('guilds', data.id); + if (guild) { + const oldGuild = cloneObject(guild); + guild.setup(data); - if (guild) { - let oldGuild = CloneObject(guild); - guild.setup(data); + if (!oldGuild.equals(data)) { + client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild); + } - if (!oldGuild.equals(data)) { - client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild); - } + return { + old: oldGuild, + updated: guild, + }; + } - return { - old: oldGuild, - updated: guild, - }; - } - - return { - old: null, - updated: null, - }; - } -}; + return { + old: null, + updated: null, + }; + } +} module.exports = GuildUpdateAction; diff --git a/src/client/actions/MessageCreate.js b/src/client/actions/MessageCreate.js index 55bae8b4e..75f5e25f0 100644 --- a/src/client/actions/MessageCreate.js +++ b/src/client/actions/MessageCreate.js @@ -1,31 +1,23 @@ -'use strict'; - const Action = require('./Action'); -const Constants = require('../../util/Constants'); const Message = require('../../structures/Message'); class MessageCreateAction extends Action { - constructor(client) { - super(client); - } + handle(data) { + const client = this.client; + const channel = client.store.get('channels', data.channel_id); - handle(data) { + if (channel) { + const message = channel._cacheMessage(new Message(channel, data, client)); + return { + m: message, + }; + } - let client = this.client; - let channel = client.store.get('channels', data.channel_id); - - if (channel) { - let message = channel._cacheMessage(new Message(channel, data, client)); - return { - m: message, - }; - } - - return { - m: null, - }; - } -}; + return { + m: null, + }; + } +} module.exports = MessageCreateAction; diff --git a/src/client/actions/MessageDelete.js b/src/client/actions/MessageDelete.js index a8a2f8e04..bbd9e00e3 100644 --- a/src/client/actions/MessageDelete.js +++ b/src/client/actions/MessageDelete.js @@ -1,51 +1,43 @@ -'use strict'; - const Action = require('./Action'); -const Constants = require('../../util/Constants'); -const Message = require('../../structures/Message'); class MessageDeleteAction extends Action { - constructor(client) { - super(client); - this.timeouts = []; - this.deleted = {}; - } + constructor(client) { + super(client); + this.timeouts = []; + this.deleted = {}; + } - handle(data) { - let client = this.client; - let channel = client.store.get('channels', data.channel_id); - if (channel) { - let message = channel.store.get('messages', data.id); + handle(data) { + const client = this.client; + const channel = client.store.get('channels', data.channel_id); + if (channel) { + let message = channel.store.get('messages', data.id); - if (message) { + if (message) { + channel.store.remove('messages', message.id); + this.deleted[channel.id + message.id] = message; + this.scheduleForDeletion(channel.id, message.id); + } else if (this.deleted[channel.id + data.id]) { + message = this.deleted[channel.id + data.id]; + } - channel.store.remove('messages', message.id); - this.deleted[channel.id + message.id] = message; - this.scheduleForDeletion(channel.id, message.id); + return { + m: message, + }; + } - } else if (this.deleted[channel.id + data.id]) { + return { + m: null, + }; + } - message = this.deleted[channel.id + data.id]; - - } - - return { - m: message, - }; - } - - return { - m: null, - }; - } - - scheduleForDeletion(channelID, messageID) { - this.timeouts.push( - setTimeout(() => delete this.deleted[channelID + messageID], - this.client.options.rest_ws_bridge_timeout) - ); - } -}; + scheduleForDeletion(channelID, messageID) { + this.timeouts.push( + setTimeout(() => delete this.deleted[channelID + messageID], + this.client.options.rest_ws_bridge_timeout) + ); + } +} module.exports = MessageDeleteAction; diff --git a/src/client/actions/MessageUpdate.js b/src/client/actions/MessageUpdate.js index 67c1d79f6..3d5854ba9 100644 --- a/src/client/actions/MessageUpdate.js +++ b/src/client/actions/MessageUpdate.js @@ -1,44 +1,36 @@ -'use strict'; - const Action = require('./Action'); const Constants = require('../../util/Constants'); -const CloneObject = require('../../util/CloneObject'); -const Message = require('../../structures/Message'); +const cloneObject = require('../../util/CloneObject'); class MessageUpdateAction extends Action { - constructor(client) { - super(client); - } + handle(data) { + const client = this.client; + const channel = client.store.get('channels', data.channel_id); - handle(data) { + if (channel) { + const message = channel.store.get('messages', data.id); + if (message && !message.equals(data, true)) { + const oldMessage = cloneObject(message); + message.patch(data); + client.emit(Constants.Events.MESSAGE_UPDATE, oldMessage, message); + return { + old: oldMessage, + updated: message, + }; + } - let client = this.client; - let channel = client.store.get('channels', data.channel_id); + return { + old: message, + updated: message, + }; + } - if (channel) { - let message = channel.store.get('messages', data.id); - if (message && !message.equals(data, true)) { - let oldMessage = CloneObject(message); - message.patch(data); - return { - old: oldMessage, - updated: message, - }; - client.emit(Constants.Events.MESSAGE_UPDATE, oldMessage, message); - } - - return { - old: message, - updated: message, - }; - } - - return { - old: null, - updated: null, - }; - } -}; + return { + old: null, + updated: null, + }; + } +} module.exports = MessageUpdateAction; diff --git a/src/client/actions/UserUpdate.js b/src/client/actions/UserUpdate.js index ec3a42cb6..7c10a9297 100644 --- a/src/client/actions/UserUpdate.js +++ b/src/client/actions/UserUpdate.js @@ -1,44 +1,36 @@ -'use strict'; - const Action = require('./Action'); const Constants = require('../../util/Constants'); -const CloneObject = require('../../util/CloneObject'); -const Message = require('../../structures/Message'); +const cloneObject = require('../../util/CloneObject'); class UserUpdateAction extends Action { - constructor(client) { - super(client); - } + handle(data) { + const client = this.client; - handle(data) { + if (client.store.user) { + if (client.store.user.equals(data)) { + return { + old: client.store.user, + updated: client.store.user, + }; + } - let client = this.client; + const oldUser = cloneObject(client.store.user); + client.store.user.setup(data); - if (client.store.user) { - if (client.store.user.equals(data)) { - return { - old: client.store.user, - updated: client.store.user, - }; - } + client.emit(Constants.Events.USER_UPDATE, oldUser, client.store.user); - let oldUser = CloneObject(client.store.user); - client.store.user.setup(data); + return { + old: oldUser, + updated: client.store.user, + }; + } - client.emit(Constants.Events.USER_UPDATE, oldUser, client.store.user); - - return { - old: oldUser, - updated: client.store.user, - }; - } - - return { - old: null, - updated: null, - }; - } -}; + return { + old: null, + updated: null, + }; + } +} module.exports = UserUpdateAction; diff --git a/src/client/rest/RESTManager.js b/src/client/rest/RESTManager.js index 60c021a47..768367219 100644 --- a/src/client/rest/RESTManager.js +++ b/src/client/rest/RESTManager.js @@ -1,105 +1,110 @@ -'use strict'; - const request = require('superagent'); const Constants = require('../../util/Constants'); const UserAgentManager = require('./UserAgentManager'); const RESTMethods = require('./RESTMethods'); -class RESTManager{ +class RESTManager { - constructor(client) { - this.client = client; - this.queue = []; - this.userAgentManager = new UserAgentManager(this); - this.methods = new RESTMethods(this); - this.rateLimitedEndpoints = {}; - } + constructor(client) { + this.client = client; + this.queue = []; + this.userAgentManager = new UserAgentManager(this); + this.methods = new RESTMethods(this); + this.rateLimitedEndpoints = {}; + } - addRequestToQueue(method, url, auth, data, file, resolve, reject) { - let endpoint = url.replace(/\/[0-9]+/g, '/:id'); + addRequestToQueue(method, url, auth, data, file, resolve, reject) { + const endpoint = url.replace(/\/[0-9]+/g, '/:id'); - let rateLimitedEndpoint = this.rateLimitedEndpoints[endpoint]; + const rateLimitedEndpoint = this.rateLimitedEndpoints[endpoint]; - rateLimitedEndpoint.queue = rateLimitedEndpoint.queue || []; + rateLimitedEndpoint.queue = rateLimitedEndpoint.queue || []; - rateLimitedEndpoint.queue.push({ - method, url, auth, data, file, resolve, reject, - }); - } + rateLimitedEndpoint.queue.push({ + method, + url, + auth, + data, + file, + resolve, + reject, + }); + } - processQueue(endpoint) { - let rateLimitedEndpoint = this.rateLimitedEndpoints[endpoint]; + processQueue(endpoint) { + const rateLimitedEndpoint = this.rateLimitedEndpoints[endpoint]; - // prevent multiple queue processes - if (!rateLimitedEndpoint.timeout) { - return; - } + // prevent multiple queue processes + if (!rateLimitedEndpoint.timeout) { + return; + } - // lock the queue - clearTimeout(rateLimitedEndpoint.timeout); - rateLimitedEndpoint.timeout = null; + // lock the queue + clearTimeout(rateLimitedEndpoint.timeout); + rateLimitedEndpoint.timeout = null; - for (let item of rateLimitedEndpoint.queue) { - this.makeRequest(item.method, item.url, item.auth, item.data, item.file) - .then(item.resolve) - .catch(item.reject); - } + for (const item of rateLimitedEndpoint.queue) { + this.makeRequest(item.method, item.url, item.auth, item.data, item.file) + .then(item.resolve) + .catch(item.reject); + } - rateLimitedEndpoint.queue = []; - } + rateLimitedEndpoint.queue = []; + } - makeRequest(method, url, auth, data, file, fromQueue) { - /* - file is {file, name} - */ - let apiRequest = request[method](url); + makeRequest(method, url, auth, data, file) { + /* + file is {file, name} + */ + console.log(url); + const apiRequest = request[method](url); - let endpoint = url.replace(/\/[0-9]+/g, '/:id'); + const endpoint = url.replace(/\/[0-9]+/g, '/:id'); - if (this.rateLimitedEndpoints[endpoint] && this.rateLimitedEndpoints[endpoint].timeout) { - return new Promise((resolve, reject) => { - this.addRequestToQueue(method, url, auth, data, file, resolve, reject); - }); - } + if (this.rateLimitedEndpoints[endpoint] && this.rateLimitedEndpoints[endpoint].timeout) { + return new Promise((resolve, reject) => { + this.addRequestToQueue(method, url, auth, data, file, resolve, reject); + }); + } - if (auth) { - if (this.client.store.token) { - apiRequest.set('authorization', this.client.store.token); - } else { - throw Constants.Errors.NO_TOKEN; - } - } + if (auth) { + if (this.client.store.token) { + apiRequest.set('authorization', this.client.store.token); + } else { + throw Constants.Errors.NO_TOKEN; + } + } - if (data) { - apiRequest.send(data); - } + if (data) { + apiRequest.send(data); + } - if (file) { - apiRequest.attach('file', file.file, file.name); - } + if (file) { + apiRequest.attach('file', file.file, file.name); + } - apiRequest.set('User-Agent', this.userAgentManager.userAgent); + apiRequest.set('User-Agent', this.userAgentManager.userAgent); - return new Promise((resolve, reject) => { - apiRequest.end((err, res) => { - if (err) { - let retry = res.headers['retry-after'] || res.headers['Retry-After']; - if (retry) { - this.rateLimitedEndpoints[endpoint] = {}; - this.addRequestToQueue(method, url, auth, data, file, resolve, reject); - this.rateLimitedEndpoints[endpoint].timeout = setTimeout(() => { - this.processQueue(endpoint); - }, retry); - return; - } + return new Promise((resolve, reject) => { + apiRequest.end((err, res) => { + if (err) { + const retry = res.headers['retry-after'] || res.headers['Retry-After']; + if (retry) { + this.rateLimitedEndpoints[endpoint] = {}; + this.addRequestToQueue(method, url, auth, data, file, resolve, reject); + this.rateLimitedEndpoints[endpoint].timeout = setTimeout(() => { + this.processQueue(endpoint); + }, retry); + return; + } - reject(err); - } else { - resolve(res ? res.body || {} : {}); - } - }); - }); - } -}; + reject(err); + } else { + resolve(res ? res.body || {} : {}); + } + }); + }); + } +} module.exports = RESTManager; diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index 5ddf95198..9eff9d3d1 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -1,329 +1,328 @@ -'use strict'; - const Constants = require('../../util/Constants'); -const Structure = name => require('../../structures/' + name); -const User = Structure('User'); -const GuildMember = Structure('GuildMember'); -const Message = Structure('Message'); -class RESTMethods{ - constructor(restManager) { - this.rest = restManager; - } +const getStructure = name => require(`../../structures/${name}`); +const User = getStructure('User'); +const GuildMember = getStructure('GuildMember'); - LoginEmailPassword(email, password) { - return new Promise((resolve, reject) => { - this.rest.client.store.email = email; - this.rest.client.store.password = password; - this.rest.makeRequest('post', Constants.Endpoints.LOGIN, false, { email, password }) - .then(data => { - this.rest.client.manager.connectToWebSocket(data.token, resolve, reject); - }) - .catch(reject); +class RESTMethods { + constructor(restManager) { + this.rest = restManager; + } - }); - } + loginEmailPassword(email, password) { + return new Promise((resolve, reject) => { + this.rest.client.store.email = email; + this.rest.client.store.password = password; + this.rest.makeRequest('post', Constants.Endpoints.login, false, { email, password }) + .then(data => { + this.rest.client.manager.connectToWebSocket(data.token, resolve, reject); + }) + .catch(reject); + }); + } - LoginToken(token) { - return new Promise((resolve, reject) => { - this.rest.client.manager.connectToWebSocket(token, resolve, reject); - }); - } + loginToken(token) { + return new Promise((resolve, reject) => { + this.rest.client.manager.connectToWebSocket(token, resolve, reject); + }); + } - GetGateway() { - return new Promise((resolve, reject) => { - this.rest.makeRequest('get', Constants.Endpoints.GATEWAY, true) - .then(res => resolve(res.url)) - .catch(reject); - }); - } + getGateway() { + return new Promise((resolve, reject) => { + this.rest.makeRequest('get', Constants.Endpoints.gateway, true) + .then(res => resolve(res.url)) + .catch(reject); + }); + } - SendMessage(channel, content, tts, nonce) { - return new Promise((resolve, reject) => { + sendMessage($channel, content, tts, nonce) { + return new Promise((resolve, reject) => { + const $this = this; + let channel = $channel; - var _this = this; + function req() { + $this.rest.makeRequest('post', Constants.Endpoints.channelMessages(channel.id), true, { + content, tts, nonce, + }) + .then(data => resolve($this.rest.client.actions.MessageCreate.handle(data).m)) + .catch(reject); + } - if (channel instanceof User || channel instanceof GuildMember) { - this.CreateDM(channel).then(chan => { - channel = chan; - req(); - }) - .catch(reject); - } else { - req(); - } + if (channel instanceof User || channel instanceof GuildMember) { + this.createDM(channel).then(chan => { + channel = chan; + req(); + }) + .catch(reject); + } else { + req(); + } + }); + } - function req() { - _this.rest.makeRequest('post', Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, { - content, tts, nonce, - }) - .then(data => resolve(_this.rest.client.actions.MessageCreate.handle(data).m)) - .catch(reject); - } - }); - } + deleteMessage(message) { + return new Promise((resolve, reject) => { + this.rest.makeRequest('del', Constants.Endpoints.channelMessage(message.channel.id, message.id), true) + .then(() => { + resolve(this.rest.client.actions.MessageDelete.handle({ + id: message.id, + channel_id: message.channel.id, + }).m); + }) + .catch(reject); + }); + } - DeleteMessage(message) { - return new Promise((resolve, reject) => { - this.rest.makeRequest('del', Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true) - .then(data => { - resolve(this.rest.client.actions.MessageDelete.handle({ - id: message.id, - channel_id: message.channel.id, - }).m); - }) - .catch(reject); - }); - } + updateMessage(message, content) { + return new Promise((resolve, reject) => { + this.rest.makeRequest('patch', Constants.Endpoints.channelMessage(message.channel.id, message.id), true, { + content, + }) + .then(data => { + resolve(this.rest.client.actions.MessageUpdate.handle(data).updated); + }) + .catch(reject); + }); + } - UpdateMessage(message, content) { - return new Promise((resolve, reject) => { - this.rest.makeRequest('patch', Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true, { - content, - }) - .then(data => { - resolve(this.rest.client.actions.MessageUpdate.handle(data).updated); - }) - .catch(reject); - }); - } + createChannel(guild, channelName, channelType) { + return new Promise((resolve, reject) => { + this.rest.makeRequest('post', Constants.Endpoints.guildChannels(guild.id), true, { + name: channelName, + type: channelType, + }) + .then(data => { + resolve(this.rest.client.actions.ChannelCreate.handle(data).channel); + }) + .catch(reject); + }); + } - CreateChannel(guild, channelName, channelType) { - return new Promise((resolve, reject) => { - this.rest.makeRequest('post', Constants.Endpoints.GUILD_CHANNELS(guild.id), true, { - name: channelName, - type: channelType, - }) - .then(data => { - resolve(this.rest.client.actions.ChannelCreate.handle(data).channel); - }) - .catch(reject); - }); - } + getExistingDM(recipient) { + const dmChannel = this.rest.client.store.getAsArray('channels') + .filter(channel => channel.recipient) + .filter(channel => channel.recipient.id === recipient.id); - GetExistingDM(recipient) { - let dmChannel = this.rest.client.store.getAsArray('channels') - .filter(channel => channel.recipient) - .filter(channel => channel.recipient.id === recipient.id); + return dmChannel[0]; + } - return dmChannel[0]; - } + createDM(recipient) { + return new Promise((resolve, reject) => { + const dmChannel = this.getExistingDM(recipient); - CreateDM(recipient) { - return new Promise((resolve, reject) => { + if (dmChannel) { + return resolve(dmChannel); + } - let dmChannel = this.GetExistingDM(recipient); + return this.rest.makeRequest('post', Constants.Endpoints.userChannels(this.rest.client.store.user.id), true, { + recipient_id: recipient.id, + }) + .then(data => resolve(this.rest.client.actions.ChannelCreate.handle(data).channel)) + .catch(reject); + }); + } - if (dmChannel) { - return resolve(dmChannel); - } + deleteChannel($channel) { + let channel = $channel; + return new Promise((resolve, reject) => { + if (channel instanceof User || channel instanceof GuildMember) { + channel = this.getExistingDM(channel); + } - this.rest.makeRequest('post', Constants.Endpoints.USER_CHANNELS(this.rest.client.store.user.id), true, { - recipient_id: recipient.id, - }) - .then(data => resolve(this.rest.client.actions.ChannelCreate.handle(data).channel)) - .catch(reject); - }); - } + this.rest.makeRequest('del', Constants.Endpoints.channel(channel.id), true) + .then($data => { + const data = $data; + data.id = channel.id; + resolve(this.rest.client.actions.ChannelDelete.handle(data).channel); + }) + .catch(reject); + }); + } - DeleteChannel(channel) { - return new Promise((resolve, reject) => { - if (channel instanceof User || channel instanceof GuildMember) { - channel = this.GetExistingDM(channel); - } + updateChannel(channel, $data) { + const data = $data; + return new Promise((resolve, reject) => { + data.name = (data.name || channel.name).trim(); + data.topic = data.topic || channel.topic; + data.position = data.position || channel.position; + data.bitrate = data.bitrate || channel.bitrate; - this.rest.makeRequest('del', Constants.Endpoints.CHANNEL(channel.id), true) - .then(data => { - data.id = channel.id; - resolve(this.rest.client.actions.ChannelDelete.handle(data).channel); - }) - .catch(reject); - }); - } + this.rest.makeRequest('patch', Constants.Endpoints.channel(channel.id), true, data) + .then(newData => { + resolve(this.rest.client.actions.ChannelUpdate.handle(newData).updated); + }) + .catch(reject); + }); + } - UpdateChannel(channel, data) { - return new Promise((resolve, reject) => { - data.name = (data.name || channel.name).trim(); - data.topic = data.topic || channel.topic; - data.position = data.position || channel.position; - data.bitrate = data.bitrate || channel.bitrate; + leaveGuild(guild) { + return new Promise((resolve, reject) => { + this.rest.makeRequest('del', Constants.Endpoints.meGuild(guild.id), true) + .then(() => { + resolve(this.rest.client.actions.GuildDelete.handle({ id: guild.id }).guild); + }) + .catch(reject); + }); + } - this.rest.makeRequest('patch', Constants.Endpoints.CHANNEL(channel.id), true, data) - .then(data => { - resolve(this.rest.client.actions.ChannelUpdate.handle(data).updated); - }) - .catch(reject); - }); - } + // untested but probably will work + deleteGuild(guild) { + return new Promise((resolve, reject) => { + this.rest.makeRequest('del', Constants.Endpoints.guild(guild.id), true) + .then(() => { + resolve(this.rest.client.actions.GuildDelete.handle({ id: guild.id }).guild); + }) + .catch(reject); + }); + } - LeaveGuild(guild) { - return new Promise((resolve, reject) => { - this.rest.makeRequest('del', Constants.Endpoints.ME_GUILD(guild.id), true) - .then(() => { - resolve(this.rest.client.actions.GuildDelete.handle({ id:guild.id }).guild); - }) - .catch(reject); - }); - } + updateCurrentUser(_data) { + return new Promise((resolve, reject) => { + const user = this.rest.client.store.user; + const data = {}; - // untested but probably will work - DeleteGuild(guild) { - return new Promise((resolve, reject) => { - this.rest.makeRequest('del', Constants.Endpoints.GUILD(guild.id), true) - .then(() => { - resolve(this.rest.client.actions.GuildDelete.handle({ id:guild.id }).guild); - }) - .catch(reject); - }); - } + data.username = _data.username || user.username; + data.avatar = this.rest.client.resolver.resolveBase64(_data.avatar) || user.avatar; + if (!user.bot) { + data.password = this.rest.client.store.password; + data.email = _data.email || this.rest.client.store.email; + data.new_password = _data.newPassword; + } - UpdateCurrentUser(_data) { - return new Promise((resolve, reject) => { - let user = this.rest.client.store.user; - let data = {}; + this.rest.makeRequest('patch', Constants.Endpoints.me, true, data) + .then(newData => resolve(this.rest.client.actions.UserUpdate.handle(newData).updated)) + .catch(reject); + }); + } - data.username = _data.username || user.username; - data.avatar = this.rest.client.resolver.ResolveBase64(_data.avatar) || user.avatar; - if (!user.bot) { - data.password = this.rest.client.store.password; - data.email = _data.email || this.rest.client.store.email; - data.new_password = _data.newPassword; - } + updateGuild(guild, _data) { + return new Promise((resolve, reject) => { + /* + can contain: + name, region, verificationLevel, afkChannel, afkTimeout, icon, owner, splash + */ - this.rest.makeRequest('patch', Constants.Endpoints.ME, true, data) - .then(data => resolve(this.rest.client.actions.UserUpdate.handle(data).updated)) - .catch(reject); - }); - } + const data = {}; - UpdateGuild(guild, _data) { - return new Promise((resolve, reject) => { - /* - can contain: - name, region, verificationLevel, afkChannel, afkTimeout, icon, owner, splash - */ + if (_data.name) { + data.name = _data.name; + } - let data = {}; + if (_data.region) { + data.region = _data.region; + } - if (_data.name) { - data.name = _data.name; - } + if (_data.verificationLevel) { + data.verification_level = Number(_data.verificationLevel); + } - if (_data.region) { - data.region = _data.region; - } + if (_data.afkChannel) { + data.afk_channel_id = this.rest.client.resolver.resolveChannel(_data.afkChannel).id; + } - if (_data.verificationLevel) { - data.verification_level = Number(_data.verificationLevel); - } + if (_data.afkTimeout) { + data.afk_timeout = Number(_data.afkTimeout); + } - if (_data.afkChannel) { - data.afk_channel_id = this.rest.client.resolver.ResolveChannel(_data.afkChannel).id; - } + if (_data.icon) { + data.icon = this.rest.client.resolver.resolveBase64(_data.icon); + } - if (_data.afkTimeout) { - data.afk_timeout = Number(_data.afkTimeout); - } + if (_data.owner) { + data.owner_id = this.rest.client.resolver.resolveUser(_data.owner).id; + } - if (_data.icon) { - data.icon = this.rest.client.resolver.ResolveBase64(_data.icon); - } + if (_data.splash) { + data.splash = this.rest.client.resolver.resolveBase64(_data.splash); + } - if (_data.owner) { - data.owner_id = this.rest.client.resolver.ResolveUser(_data.owner).id; - } + this.rest.makeRequest('patch', Constants.Endpoints.guild(guild.id), true, data) + .then(newData => resolve(this.rest.client.actions.GuildUpdate.handle(newData).updated)) + .catch(reject); + }); + } - if (_data.splash) { - data.splash = this.rest.client.resolver.ResolveBase64(_data.splash); - } + kickGuildMember(guild, member) { + return new Promise((resolve, reject) => { + this.rest.makeRequest('del', Constants.Endpoints.guildMember(guild.id, member.id), true) + .then(() => { + resolve(this.rest.client.actions.GuildMemberRemove.handle({ + guild_id: guild.id, + user: member.user, + }).m); + }) + .catch(reject); + }); + } - this.rest.makeRequest('patch', Constants.Endpoints.GUILD(guild.id), true, data) - .then(data => resolve(this.rest.client.actions.GuildUpdate.handle(data).updated)) - .catch(reject); - }); - } + createGuildRole(guild) { + return new Promise((resolve, reject) => { + this.rest.makeRequest('post', Constants.Endpoints.guildRoles(guild.id), true) + .then(role => { + resolve(this.rest.client.actions.GuildRoleCreate.handle({ + guild_id: guild.id, + role, + }).role); + }) + .catch(reject); + }); + } - KickGuildMember(guild, member) { - return new Promise((resolve, reject) => { - this.rest.makeRequest('del', Constants.Endpoints.GUILD_MEMBER(guild.id, member.id), true) - .then(() => { - resolve(this.rest.client.actions.GuildMemberRemove.handle({ - guild_id: guild.id, - user: member.user, - }).m); - }) - .catch(reject); - }); - } + deleteGuildRole(role) { + return new Promise((resolve, reject) => { + this.rest.makeRequest('del', Constants.Endpoints.guildRole(role.guild.id, role.id), true) + .then(() => { + resolve(this.rest.client.actions.GuildRoleDelete.handle({ + guild_id: role.guild.id, + role_id: role.id, + }).role); + }) + .catch(reject); + }); + } - CreateGuildRole(guild) { - return new Promise((resolve, reject) => { - this.rest.makeRequest('post', Constants.Endpoints.GUILD_ROLES(guild.id), true) - .then(role => { - resolve(this.rest.client.actions.GuildRoleCreate.handle({ - guild_id: guild.id, - role, - }).role); - }) - .catch(reject); - }); - } + updateGuildRole(role, _data) { + return new Promise((resolve, reject) => { + /* + can contain: + name, position, permissions, color, hoist + */ - DeleteGuildRole(role) { - return new Promise((resolve, reject) => { - this.rest.makeRequest('del', Constants.Endpoints.GUILD_ROLE(role.guild.id, role.id), true) - .then(() => { - resolve(this.rest.client.actions.GuildRoleDelete.handle({ - guild_id: role.guild.id, - role_id: role.id, - }).role); - }) - .catch(reject); - }); - } + const data = {}; - UpdateGuildRole(role, _data) { - return new Promise((resolve, reject) => { - /* - can contain: - name, position, permissions, color, hoist - */ + data.name = _data.name || role.name; + data.position = _data.position || role.position; + data.color = _data.color || role.color; - let data = {}; + if (typeof _data.hoist !== 'undefined') { + data.hoist = _data.hoist; + } else { + data.hoist = role.hoist; + } - data.name = _data.name || role.name; - data.position = _data.position || role.position; - data.color = _data.color || role.color; + if (_data.permissions) { + let perms = 0; + for (let perm of _data.permissions) { + if (perm instanceof String || typeof perm === 'string') { + perm = Constants.PermissionFlags[perm]; + } + perms |= perm; + } + data.permissions = perms; + } else { + data.permissions = role.permissions; + } - if (typeof _data.hoist !== 'undefined') { - data.hoist = _data.hoist; - } else { - data.hoist = role.hoist; - } - - if (_data.permissions) { - let perms = 0; - for (let perm of _data.permissions) { - if (perm instanceof String || typeof perm === 'string') { - perm = Constants.PermissionFlags[perm]; - } - perms |= perm; - } - data.permissions = perms; - } else { - data.permissions = role.permissions; - } - console.log(data); - this.rest.makeRequest('patch', Constants.Endpoints.GUILD_ROLE(role.guild.id, role.id), true, data) - .then(_role => { - resolve(this.rest.client.actions.GuildRoleUpdate.handle({ - role: _role, - guild_id: role.guild.id, - }).updated); - }) - .catch(reject); - }); - } + this.rest.makeRequest('patch', Constants.Endpoints.guildRole(role.guild.id, role.id), true, data) + .then(_role => { + resolve(this.rest.client.actions.GuildRoleUpdate.handle({ + role: _role, + guild_id: role.guild.id, + }).updated); + }) + .catch(reject); + }); + } } module.exports = RESTMethods; diff --git a/src/client/rest/UserAgentManager.js b/src/client/rest/UserAgentManager.js index 9fb075fca..12393ffd1 100644 --- a/src/client/rest/UserAgentManager.js +++ b/src/client/rest/UserAgentManager.js @@ -1,24 +1,22 @@ -'use strict'; - const Constants = require('../../util/Constants'); -class UserAgentManager{ - constructor(restManager) { - this.restManager = restManager; - this._userAgent = { - url: 'https://github.com/hydrabolt/discord.js', - version: Constants.Package.version, - }; - } +class UserAgentManager { + constructor(restManager) { + this.restManager = restManager; + this._userAgent = { + url: 'https://github.com/hydrabolt/discord.js', + version: Constants.Package.version, + }; + } - set(info) { - this._userAgent.url = info.url || 'https://github.com/hydrabolt/discord.js'; - this._userAgent.version = info.version || Constants.Package.version; - } + set(info) { + this._userAgent.url = info.url || 'https://github.com/hydrabolt/discord.js'; + this._userAgent.version = info.version || Constants.Package.version; + } - get userAgent() { - return `DiscordBot (${this._userAgent.url}, ${this._userAgent.version})`; - } + get userAgent() { + return `DiscordBot (${this._userAgent.url}, ${this._userAgent.version})`; + } } module.exports = UserAgentManager; diff --git a/src/client/websocket/WebSocketManager.js b/src/client/websocket/WebSocketManager.js index a3d7f08ca..0b30d627d 100644 --- a/src/client/websocket/WebSocketManager.js +++ b/src/client/websocket/WebSocketManager.js @@ -1,5 +1,3 @@ -'use strict'; - const WebSocket = require('ws'); const Constants = require('../../util/Constants'); const zlib = require('zlib'); @@ -8,111 +6,111 @@ const WebSocketManagerDataStore = require('../../structures/datastore/WebSocketM class WebSocketManager { - constructor(client) { - this.client = client; - this.ws = null; - this.packetManager = new PacketManager(this); - this.store = new WebSocketManagerDataStore(); - this.status = Constants.Status.IDLE; - } + constructor(client) { + this.client = client; + this.ws = null; + this.packetManager = new PacketManager(this); + this.store = new WebSocketManagerDataStore(); + this.status = Constants.Status.IDLE; + } - connect(gateway) { - this.status = Constants.Status.CONNECTING; - this.store.gateway = gateway; - gateway += `/?v=${this.client.options.protocol_version}`; - this.ws = new WebSocket(gateway); - this.ws.onopen = () => this.EventOpen(); - this.ws.onclose = () => this.EventClose(); - this.ws.onmessage = (e) => this.EventMessage(e); - this.ws.onerror = (e) => this.EventError(e); - } + connect(gateway) { + this.status = Constants.Status.CONNECTING; + this.store.gateway = `${gateway}/?v=${this.client.options.protocol_version}`; + this.ws = new WebSocket(gateway); + this.ws.onopen = () => this.eventOpen(); + this.ws.onclose = () => this.eventClose(); + this.ws.onmessage = (e) => this.eventMessage(e); + this.ws.onerror = (e) => this.eventError(e); + } - send(data) { - if (this.ws.readyState === WebSocket.OPEN) { - this.ws.send(JSON.stringify(data)); - } - } + send(data) { + if (this.ws.readyState === WebSocket.OPEN) { + this.ws.send(JSON.stringify(data)); + } + } - EventOpen() { - if (this.reconnecting) { - this._sendResume(); - } else { - this._sendNewIdentify(); - } - } + eventOpen() { + if (this.reconnecting) { + this._sendResume(); + } else { + this._sendNewIdentify(); + } + } - _sendResume() { - let payload = { - token: this.client.store.token, - session_id: this.store.sessionID, - seq: this.store.sequence, - }; + _sendResume() { + const payload = { + token: this.client.store.token, + session_id: this.store.sessionID, + seq: this.store.sequence, + }; - this.send({ - op: Constants.OPCodes.RESUME, - d: payload, - }); - } + this.send({ + op: Constants.OPCodes.RESUME, + d: payload, + }); + } - _sendNewIdentify() { - this.reconnecting = false; - let payload = this.client.options.ws; - payload.token = this.client.store.token; + _sendNewIdentify() { + this.reconnecting = false; + const payload = this.client.options.ws; + payload.token = this.client.store.token; - this.send({ - op: Constants.OPCodes.IDENTIFY, - d: payload, - }); - } + this.send({ + op: Constants.OPCodes.IDENTIFY, + d: payload, + }); + } - EventClose() { - if (!this.reconnecting) { - this.tryReconnect(); - } - } + eventClose() { + if (!this.reconnecting) { + this.tryReconnect(); + } + } - EventMessage(event) { - let packet; - try { - if (event.binary) { - event.data = zlib.inflateSync(event.data).toString(); - } + eventMessage($event) { + let packet; + const event = $event; + try { + if (event.binary) { + event.data = zlib.inflateSync(event.data).toString(); + } - packet = JSON.parse(event.data); - } catch (e) { - return this.EventError(Constants.Errors.BAD_WS_MESSAGE); - } + packet = JSON.parse(event.data); + } catch (e) { + return this.eventError(Constants.Errors.BAD_WS_MESSAGE); + } - this.packetManager.handle(packet); - } + return this.packetManager.handle(packet); + } - EventError(e) { - this.tryReconnect(); - } + EventError() { + this.tryReconnect(); + } - checkIfReady() { - if (this.status !== Constants.Status.READY) { - let unavailableCount = 0; + checkIfReady() { + if (this.status !== Constants.Status.READY) { + let unavailableCount = 0; - for (let guildID in this.client.store.data.guilds) { - unavailableCount += this.client.store.data.guilds[guildID].available ? 0 : 1; - } + for (const guildID in this.client.store.data.guilds) { + unavailableCount += this.client.store.data.guilds[guildID].available ? 0 : 1; + } - if (unavailableCount === 0) { - this.status = Constants.Status.READY; - this.client.emit(Constants.Events.READY); - this.packetManager.handleQueue(); - } - } - } + if (unavailableCount === 0) { + this.status = Constants.Status.READY; + this.client.emit(Constants.Events.READY); + this.packetManager.handleQueue(); + } + } + } - tryReconnect() { - this.status = Constants.Status.RECONNECTING; - this.ws.close(); - this.packetManager.handleQueue(); - this.client.emit(Constants.Events.RECONNECTING); - this.connect(this.store.gateway); - } + tryReconnect() { + this.status = Constants.Status.RECONNECTING; + this.ws.close(); + this.packetManager.handleQueue(); + this.client.emit(Constants.Events.RECONNECTING); + this.connect(this.store.gateway); + } } module.exports = WebSocketManager; diff --git a/src/client/websocket/packets/WebSocketPacketManager.js b/src/client/websocket/packets/WebSocketPacketManager.js index b4e123033..dd8615637 100644 --- a/src/client/websocket/packets/WebSocketPacketManager.js +++ b/src/client/websocket/packets/WebSocketPacketManager.js @@ -1,100 +1,97 @@ -'use strict'; - const Constants = require('../../../util/Constants'); const BeforeReadyWhitelist = [ - Constants.WSEvents.READY, - Constants.WSEvents.GUILD_CREATE, - Constants.WSEvents.GUILD_DELETE, + Constants.WSEvents.READY, + Constants.WSEvents.GUILD_CREATE, + Constants.WSEvents.GUILD_DELETE, ]; class WebSocketPacketManager { - constructor(websocketManager) { - this.ws = websocketManager; - this.handlers = {}; - this.queue = []; + constructor(websocketManager) { + this.ws = websocketManager; + this.handlers = {}; + this.queue = []; - this.register(Constants.WSEvents.READY, 'Ready'); - this.register(Constants.WSEvents.GUILD_CREATE, 'GuildCreate'); - this.register(Constants.WSEvents.GUILD_DELETE, 'GuildDelete'); - this.register(Constants.WSEvents.GUILD_UPDATE, 'GuildUpdate'); - this.register(Constants.WSEvents.GUILD_BAN_ADD, 'GuildBanAdd'); - this.register(Constants.WSEvents.GUILD_BAN_REMOVE, 'GuildBanRemove'); - this.register(Constants.WSEvents.GUILD_MEMBER_ADD, 'GuildMemberAdd'); - this.register(Constants.WSEvents.GUILD_MEMBER_REMOVE, 'GuildMemberRemove'); - this.register(Constants.WSEvents.GUILD_MEMBER_UPDATE, 'GuildMemberUpdate'); - this.register(Constants.WSEvents.GUILD_ROLE_CREATE, 'GuildRoleCreate'); - this.register(Constants.WSEvents.GUILD_ROLE_DELETE, 'GuildRoleDelete'); - this.register(Constants.WSEvents.GUILD_ROLE_UPDATE, 'GuildRoleUpdate'); - this.register(Constants.WSEvents.GUILD_MEMBERS_CHUNK, 'GuildMembersChunk'); - this.register(Constants.WSEvents.CHANNEL_CREATE, 'ChannelCreate'); - this.register(Constants.WSEvents.CHANNEL_DELETE, 'ChannelDelete'); - this.register(Constants.WSEvents.CHANNEL_UPDATE, 'ChannelUpdate'); - this.register(Constants.WSEvents.PRESENCE_UPDATE, 'PresenceUpdate'); - this.register(Constants.WSEvents.USER_UPDATE, 'UserUpdate'); - this.register(Constants.WSEvents.VOICE_STATE_UPDATE, 'VoiceStateUpdate'); - this.register(Constants.WSEvents.TYPING_START, 'TypingStart'); - this.register(Constants.WSEvents.MESSAGE_CREATE, 'MessageCreate'); - this.register(Constants.WSEvents.MESSAGE_DELETE, 'MessageDelete'); - this.register(Constants.WSEvents.MESSAGE_UPDATE, 'MessageUpdate'); - } + this.register(Constants.WSEvents.READY, 'Ready'); + this.register(Constants.WSEvents.GUILD_CREATE, 'GuildCreate'); + this.register(Constants.WSEvents.GUILD_DELETE, 'GuildDelete'); + this.register(Constants.WSEvents.GUILD_UPDATE, 'GuildUpdate'); + this.register(Constants.WSEvents.GUILD_BAN_ADD, 'GuildBanAdd'); + this.register(Constants.WSEvents.GUILD_BAN_REMOVE, 'GuildBanRemove'); + this.register(Constants.WSEvents.GUILD_MEMBER_ADD, 'GuildMemberAdd'); + this.register(Constants.WSEvents.GUILD_MEMBER_REMOVE, 'GuildMemberRemove'); + this.register(Constants.WSEvents.GUILD_MEMBER_UPDATE, 'GuildMemberUpdate'); + this.register(Constants.WSEvents.GUILD_ROLE_CREATE, 'GuildRoleCreate'); + this.register(Constants.WSEvents.GUILD_ROLE_DELETE, 'GuildRoleDelete'); + this.register(Constants.WSEvents.GUILD_ROLE_UPDATE, 'GuildRoleUpdate'); + this.register(Constants.WSEvents.GUILD_MEMBERS_CHUNK, 'GuildMembersChunk'); + this.register(Constants.WSEvents.CHANNEL_CREATE, 'ChannelCreate'); + this.register(Constants.WSEvents.CHANNEL_DELETE, 'ChannelDelete'); + this.register(Constants.WSEvents.CHANNEL_UPDATE, 'ChannelUpdate'); + this.register(Constants.WSEvents.PRESENCE_UPDATE, 'PresenceUpdate'); + this.register(Constants.WSEvents.USER_UPDATE, 'UserUpdate'); + this.register(Constants.WSEvents.VOICE_STATE_UPDATE, 'VoiceStateUpdate'); + this.register(Constants.WSEvents.TYPING_START, 'TypingStart'); + this.register(Constants.WSEvents.MESSAGE_CREATE, 'MessageCreate'); + this.register(Constants.WSEvents.MESSAGE_DELETE, 'MessageDelete'); + this.register(Constants.WSEvents.MESSAGE_UPDATE, 'MessageUpdate'); + } - get client() { - return this.ws.client; - } + get client() { + return this.ws.client; + } - register(event, handle) { - let Handler = require(`./handlers/${handle}`); - this.handlers[event] = new Handler(this); - } + register(event, handle) { + const Handler = require(`./handlers/${handle}`); + this.handlers[event] = new Handler(this); + } - handleQueue() { - for (let packetIndex in this.queue) { - this.handle(this.queue[packetIndex]); - this.queue.splice(packetIndex, 1); - } - } + handleQueue() { + this.queue.forEach((element, index) => { + this.handle(this.queue[index]); + this.queue.splice(index, 1); + }); + } - setSequence(s) { - if (s && s > this.ws.store.sequence) { - this.ws.store.sequence = s; - } - } + setSequence(s) { + if (s && s > this.ws.store.sequence) { + this.ws.store.sequence = s; + } + } - handle(packet) { + handle(packet) { + if (packet.op === Constants.OPCodes.RECONNECT) { + this.setSequence(packet.s); + this.ws.tryReconnect(); + return false; + } - if (packet.op === Constants.OPCodes.RECONNECT) { - this.setSequence(packet.s); - this.ws.tryReconnect(); - return; - } + if (packet.op === Constants.OPCodes.INVALID_SESSION) { + this.ws._sendNewIdentify(); + return false; + } - if (packet.op === Constants.OPCodes.INVALID_SESSION) { - this.ws._sendNewIdentify(); - return; - } + if (this.ws.reconnecting) { + this.ws.reconnecting = false; + this.ws.checkIfReady(); + } - if (this.ws.reconnecting) { - this.ws.reconnecting = false; - this.ws.checkIfReady(); - } + this.setSequence(packet.s); - this.setSequence(packet.s); + if (this.ws.status !== Constants.Status.READY) { + if (BeforeReadyWhitelist.indexOf(packet.t) === -1) { + this.queue.push(packet); + return false; + } + } - if (this.ws.status !== Constants.Status.READY) { - if (BeforeReadyWhitelist.indexOf(packet.t) === -1) { - this.queue.push(packet); - return; - } - } + if (this.handlers[packet.t]) { + return this.handlers[packet.t].handle(packet); + } - if (this.handlers[packet.t]) { - return this.handlers[packet.t].handle(packet); - } - - return false; - } + return false; + } } diff --git a/src/client/websocket/packets/handlers/AbstractHandler.js b/src/client/websocket/packets/handlers/AbstractHandler.js index 9ab096dd6..ec744c487 100644 --- a/src/client/websocket/packets/handlers/AbstractHandler.js +++ b/src/client/websocket/packets/handlers/AbstractHandler.js @@ -1,14 +1,12 @@ -'use strict'; - class AbstractHandler { - constructor(packetManager) { - this.packetManager = packetManager; - } + constructor(packetManager) { + this.packetManager = packetManager; + } - handle(packet) { - - } + handle(packet) { + return packet; + } } module.exports = AbstractHandler; diff --git a/src/client/websocket/packets/handlers/ChannelCreate.js b/src/client/websocket/packets/handlers/ChannelCreate.js index 100b02785..bf81ee6ce 100644 --- a/src/client/websocket/packets/handlers/ChannelCreate.js +++ b/src/client/websocket/packets/handlers/ChannelCreate.js @@ -1,32 +1,20 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); - -const ClientUser = Structure('ClientUser'); -const Guild = Structure('Guild'); -const DMChannel = Structure('DMChannel'); const Constants = require('../../../../util/Constants'); class ChannelCreateHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + const response = client.actions.ChannelCreate.handle(data); - let response = client.actions.ChannelCreate.handle(data); + if (response.channel) { + client.emit(Constants.Events.CHANNEL_CREATE, response.channel); + } + } - if (response.channel) { - client.emit(Constants.Events.CHANNEL_CREATE, response.channel); - } - - } - -}; +} module.exports = ChannelCreateHandler; diff --git a/src/client/websocket/packets/handlers/ChannelDelete.js b/src/client/websocket/packets/handlers/ChannelDelete.js index 82a6eed6f..fc47f1858 100644 --- a/src/client/websocket/packets/handlers/ChannelDelete.js +++ b/src/client/websocket/packets/handlers/ChannelDelete.js @@ -1,31 +1,20 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); - -const ClientUser = Structure('ClientUser'); -const Guild = Structure('Guild'); -const ServerChannel = Structure('ServerChannel'); const Constants = require('../../../../util/Constants'); class ChannelDeleteHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + const response = client.actions.ChannelDelete.handle(data); - let response = client.actions.ChannelDelete.handle(data); + if (response.channel) { + client.emit(Constants.Events.CHANNEL_DELETE, response.channel); + } + } - if (response.channel) { - client.emit(Constants.Events.CHANNEL_DELETE, response.channel); - } - } - -}; +} module.exports = ChannelDeleteHandler; diff --git a/src/client/websocket/packets/handlers/ChannelUpdate.js b/src/client/websocket/packets/handlers/ChannelUpdate.js index 41491a103..070cdcd24 100644 --- a/src/client/websocket/packets/handlers/ChannelUpdate.js +++ b/src/client/websocket/packets/handlers/ChannelUpdate.js @@ -1,28 +1,14 @@ -'use strict'; - -const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); - -const ClientUser = Structure('ClientUser'); -const Guild = Structure('Guild'); -const ServerChannel = Structure('ServerChannel'); - -const Constants = require('../../../../util/Constants'); -const CloneObject = require('../../../../util/CloneObject'); - -class ChannelUpdateHandler extends AbstractHandler { - - constructor(packetManager) { - super(packetManager); - } - - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; - - client.actions.ChannelUpdate.handle(data); - } - -}; - -module.exports = ChannelUpdateHandler; +const AbstractHandler = require('./AbstractHandler'); + +class ChannelUpdateHandler extends AbstractHandler { + + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; + + client.actions.ChannelUpdate.handle(data); + } + +} + +module.exports = ChannelUpdateHandler; diff --git a/src/client/websocket/packets/handlers/GuildBanAdd.js b/src/client/websocket/packets/handlers/GuildBanAdd.js index 7294b2fca..11b336961 100644 --- a/src/client/websocket/packets/handlers/GuildBanAdd.js +++ b/src/client/websocket/packets/handlers/GuildBanAdd.js @@ -1,33 +1,22 @@ -'use strict'; - // ##untested handler## const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); - -const ClientUser = Structure('ClientUser'); -const Guild = Structure('Guild'); - const Constants = require('../../../../util/Constants'); class GuildBanAddHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + const guild = client.store.get('guilds', data.guild_id); + const user = client.store.get('users', data.user.id); - let guild = client.store.get('guilds', data.guild_id); - let user = client.store.get('users', data.user.id); + if (guild && user) { + client.emit(Constants.Events.GUILD_BAN_ADD, guild, user); + } + } - if (guild && user) { - client.emit(Constants.Events.GUILD_BAN_ADD, guild, user); - } - } - -}; +} module.exports = GuildBanAddHandler; diff --git a/src/client/websocket/packets/handlers/GuildBanRemove.js b/src/client/websocket/packets/handlers/GuildBanRemove.js index ec792f1bf..1a41f777a 100644 --- a/src/client/websocket/packets/handlers/GuildBanRemove.js +++ b/src/client/websocket/packets/handlers/GuildBanRemove.js @@ -1,33 +1,23 @@ -'use strict'; - // ##untested handler## const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); - -const ClientUser = Structure('ClientUser'); -const Guild = Structure('Guild'); const Constants = require('../../../../util/Constants'); class GuildBanRemoveHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + const guild = client.store.get('guilds', data.guild_id); + const user = client.store.get('users', data.user.id); - let guild = client.store.get('guilds', data.guild_id); - let user = client.store.get('users', data.user.id); + if (guild && user) { + client.emit(Constants.Events.GUILD_BAN_REMOVE, guild, user); + } + } - if (guild && user) { - client.emit(Constants.Events.GUILD_BAN_REMOVE, guild, user); - } - } - -}; +} module.exports = GuildBanRemoveHandler; diff --git a/src/client/websocket/packets/handlers/GuildCreate.js b/src/client/websocket/packets/handlers/GuildCreate.js index 96777a79d..f39b21302 100644 --- a/src/client/websocket/packets/handlers/GuildCreate.js +++ b/src/client/websocket/packets/handlers/GuildCreate.js @@ -1,38 +1,25 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); - -const ClientUser = Structure('ClientUser'); -const Guild = Structure('Guild'); - -const Constants = require('../../../../util/Constants'); class GuildCreateHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + const guild = client.store.get('guilds', data.id); - let guild = client.store.get('guilds', data.id); + if (guild) { + if (!guild.available && !data.unavailable) { + // a newly available guild + guild.setup(data); + this.packetManager.ws.checkIfReady(); + } + } else { + // a new guild + client.store.newGuild(data); + } + } - if (guild) { - if (!guild.available && !data.unavailable) { - // a newly available guild - guild.setup(data); - this.packetManager.ws.checkIfReady(); - } - } else { - // a new guild - client.store.NewGuild(data); - } - - } - -}; +} module.exports = GuildCreateHandler; diff --git a/src/client/websocket/packets/handlers/GuildDelete.js b/src/client/websocket/packets/handlers/GuildDelete.js index 05319a5cb..a86dbd2db 100644 --- a/src/client/websocket/packets/handlers/GuildDelete.js +++ b/src/client/websocket/packets/handlers/GuildDelete.js @@ -1,31 +1,19 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); - -const ClientUser = Structure('ClientUser'); -const Guild = Structure('Guild'); - const Constants = require('../../../../util/Constants'); class GuildDeleteHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + const response = client.actions.GuildDelete.handle(data); - let response = client.actions.GuildDelete.handle(data); + if (response.guild) { + client.emit(Constants.Events.GUILD_DELETE, response.guild); + } + } - if (response.guild) { - client.emit(Constants.Events.GUILD_DELETE, response.guild); - } - - } - -}; +} module.exports = GuildDeleteHandler; diff --git a/src/client/websocket/packets/handlers/GuildMemberAdd.js b/src/client/websocket/packets/handlers/GuildMemberAdd.js index 23a475f96..8f9101fb8 100644 --- a/src/client/websocket/packets/handlers/GuildMemberAdd.js +++ b/src/client/websocket/packets/handlers/GuildMemberAdd.js @@ -1,32 +1,20 @@ -'use strict'; - // ##untested handler## const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); - -const ClientUser = Structure('ClientUser'); -const Guild = Structure('Guild'); - -const Constants = require('../../../../util/Constants'); class GuildMemberAddHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + const guild = client.store.get('guilds', data.guild_id); - let guild = client.store.get('guilds', data.guild_id); + if (guild) { + guild._addMember(data); + } + } - if (guild) { - guild._addMember(data); - } - } - -}; +} module.exports = GuildMemberAddHandler; diff --git a/src/client/websocket/packets/handlers/GuildMemberRemove.js b/src/client/websocket/packets/handlers/GuildMemberRemove.js index cf61c816b..2f8571e58 100644 --- a/src/client/websocket/packets/handlers/GuildMemberRemove.js +++ b/src/client/websocket/packets/handlers/GuildMemberRemove.js @@ -1,28 +1,16 @@ -'use strict'; - // ##untested handler## const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); - -const ClientUser = Structure('ClientUser'); -const Guild = Structure('Guild'); - -const Constants = require('../../../../util/Constants'); class GuildMemberRemoveHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + client.actions.GuildMemberRemove.handle(data); + } - let response = client.actions.GuildMemberRemove.handle(data); - } - -}; +} module.exports = GuildMemberRemoveHandler; diff --git a/src/client/websocket/packets/handlers/GuildMemberUpdate.js b/src/client/websocket/packets/handlers/GuildMemberUpdate.js index 20455a216..d5f6f7145 100644 --- a/src/client/websocket/packets/handlers/GuildMemberUpdate.js +++ b/src/client/websocket/packets/handlers/GuildMemberUpdate.js @@ -1,35 +1,23 @@ -'use strict'; - // ##untested handler## const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); - -const ClientUser = Structure('ClientUser'); -const Guild = Structure('Guild'); - -const Constants = require('../../../../util/Constants'); class GuildMemberUpdateHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + const guild = client.store.get('guilds', data.guild_id); - let guild = client.store.get('guilds', data.guild_id); + if (guild) { + const member = guild.store.get('members', data.user.id); + if (member) { + guild._updateMember(member, data); + } + } + } - if (guild) { - let member = guild.store.get('members', data.user.id); - if (member) { - guild._updateMember(member, data); - } - } - } - -}; +} module.exports = GuildMemberUpdateHandler; diff --git a/src/client/websocket/packets/handlers/GuildMembersChunk.js b/src/client/websocket/packets/handlers/GuildMembersChunk.js index 3cd2ba516..5949580dd 100644 --- a/src/client/websocket/packets/handlers/GuildMembersChunk.js +++ b/src/client/websocket/packets/handlers/GuildMembersChunk.js @@ -1,33 +1,25 @@ -'use strict'; - // ##untested## const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); const Constants = require('../../../../util/Constants'); -const CloneObject = require('../../../../util/CloneObject'); class GuildMembersChunkHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; + const guild = client.store.get('guilds', data.guild_id); - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; - let guild = client.store.get('guilds', data.guild_id); + const members = []; + if (guild) { + for (const member of guild.members) { + members.push(guild._addMember(member, true)); + } + } - let members = []; - if (guild) { - for (let member of guild.members) { - members.push(guild._addMember(member, true)); - } - } + client.emit(Constants.Events.GUILD_MEMBERS_CHUNK, guild, members); + } - client.emit(Constants.Events.GUILD_MEMBERS_CHUNK, guild, members); - } - -}; +} module.exports = GuildMembersChunkHandler; diff --git a/src/client/websocket/packets/handlers/GuildRoleCreate.js b/src/client/websocket/packets/handlers/GuildRoleCreate.js index c2ffdbb43..6b791bfb3 100644 --- a/src/client/websocket/packets/handlers/GuildRoleCreate.js +++ b/src/client/websocket/packets/handlers/GuildRoleCreate.js @@ -1,25 +1,14 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); -const Constants = require('../../../../util/Constants'); - -const Role = Structure('Role'); -const Guild = Structure('Guild'); class GuildRoleCreateHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + client.actions.GuildRoleCreate.handle(data); + } - let response = client.actions.GuildRoleCreate.handle(data); - } - -}; +} module.exports = GuildRoleCreateHandler; diff --git a/src/client/websocket/packets/handlers/GuildRoleDelete.js b/src/client/websocket/packets/handlers/GuildRoleDelete.js index 2214a7982..17135e20a 100644 --- a/src/client/websocket/packets/handlers/GuildRoleDelete.js +++ b/src/client/websocket/packets/handlers/GuildRoleDelete.js @@ -1,25 +1,14 @@ -'use strict'; - -const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); -const Constants = require('../../../../util/Constants'); - -const Role = Structure('Role'); -const Guild = Structure('Guild'); - -class GuildRoleDeleteHandler extends AbstractHandler { - - constructor(packetManager) { - super(packetManager); - } - - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; - - let response = client.actions.GuildRoleDelete.handle(data); - } - -}; - -module.exports = GuildRoleDeleteHandler; +const AbstractHandler = require('./AbstractHandler'); + +class GuildRoleDeleteHandler extends AbstractHandler { + + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; + + client.actions.GuildRoleDelete.handle(data); + } + +} + +module.exports = GuildRoleDeleteHandler; diff --git a/src/client/websocket/packets/handlers/GuildRoleUpdate.js b/src/client/websocket/packets/handlers/GuildRoleUpdate.js index 98005bc02..8fea7271f 100644 --- a/src/client/websocket/packets/handlers/GuildRoleUpdate.js +++ b/src/client/websocket/packets/handlers/GuildRoleUpdate.js @@ -1,26 +1,14 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); -const Constants = require('../../../../util/Constants'); -const CloneObject = require('../../../../util/CloneObject'); - -const Role = Structure('Role'); -const Guild = Structure('Guild'); class GuildRoleUpdateHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + client.actions.GuildRoleUpdate.handle(data); + } - let response = client.actions.GuildRoleUpdate.handle(data); - } - -}; +} module.exports = GuildRoleUpdateHandler; diff --git a/src/client/websocket/packets/handlers/GuildUpdate.js b/src/client/websocket/packets/handlers/GuildUpdate.js index 7a44eaabc..b022fb688 100644 --- a/src/client/websocket/packets/handlers/GuildUpdate.js +++ b/src/client/websocket/packets/handlers/GuildUpdate.js @@ -1,28 +1,14 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); - -const ClientUser = Structure('ClientUser'); -const Guild = Structure('Guild'); - -const Constants = require('../../../../util/Constants'); -const CloneObject = require('../../../../util/CloneObject'); class GuildUpdateHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + client.actions.GuildUpdate.handle(data); + } - let response = client.actions.GuildUpdate.handle(data); - - } - -}; +} module.exports = GuildUpdateHandler; diff --git a/src/client/websocket/packets/handlers/MessageCreate.js b/src/client/websocket/packets/handlers/MessageCreate.js index 76241e37e..fcbd8028c 100644 --- a/src/client/websocket/packets/handlers/MessageCreate.js +++ b/src/client/websocket/packets/handlers/MessageCreate.js @@ -1,29 +1,19 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); const Constants = require('../../../../util/Constants'); -const Message = Structure('Message'); -const Guild = Structure('Guild'); - class MessageCreateHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + const response = client.actions.MessageCreate.handle(data); - let response = client.actions.MessageCreate.handle(data); + if (response.m) { + client.emit(Constants.Events.MESSAGE_CREATE, response.m); + } + } - if (response.m) { - client.emit(Constants.Events.MESSAGE_CREATE, response.m); - } - } - -}; +} module.exports = MessageCreateHandler; diff --git a/src/client/websocket/packets/handlers/MessageDelete.js b/src/client/websocket/packets/handlers/MessageDelete.js index c3457f920..e29b149d4 100644 --- a/src/client/websocket/packets/handlers/MessageDelete.js +++ b/src/client/websocket/packets/handlers/MessageDelete.js @@ -1,29 +1,19 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); const Constants = require('../../../../util/Constants'); -const Message = Structure('Message'); -const Guild = Structure('Guild'); - class MessageDeleteHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + const response = client.actions.MessageDelete.handle(data); - let response = client.actions.MessageDelete.handle(data); + if (response.m) { + client.emit(Constants.Events.MESSAGE_DELETE, response.m); + } + } - if (response.m) { - client.emit(Constants.Events.MESSAGE_DELETE, response.m); - } - } - -}; +} module.exports = MessageDeleteHandler; diff --git a/src/client/websocket/packets/handlers/MessageUpdate.js b/src/client/websocket/packets/handlers/MessageUpdate.js index 69ac2aa3a..56b8992b4 100644 --- a/src/client/websocket/packets/handlers/MessageUpdate.js +++ b/src/client/websocket/packets/handlers/MessageUpdate.js @@ -1,27 +1,14 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); -const Constants = require('../../../../util/Constants'); -const CloneObject = require('../../../../util/CloneObject'); - -const Message = Structure('Message'); -const Guild = Structure('Guild'); class MessageUpdateHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + client.actions.MessageUpdate.handle(data); + } - let response = client.actions.MessageUpdate.handle(data); - - } - -}; +} module.exports = MessageUpdateHandler; diff --git a/src/client/websocket/packets/handlers/PresenceUpdate.js b/src/client/websocket/packets/handlers/PresenceUpdate.js index e3c5e4f1a..51732c726 100644 --- a/src/client/websocket/packets/handlers/PresenceUpdate.js +++ b/src/client/websocket/packets/handlers/PresenceUpdate.js @@ -1,75 +1,66 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); const Constants = require('../../../../util/Constants'); -const CloneObject = require('../../../../util/CloneObject'); - -const Role = Structure('User'); +const cloneObject = require('../../../../util/CloneObject'); class PresenceUpdateHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; + let user = client.store.get('users', data.user.id); + const guild = client.store.get('guilds', data.guild_id); - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; - let user = client.store.get('users', data.user.id); - let guild = client.store.get('guilds', data.guild_id); + function makeUser(newUser) { + return client.store.newUser(newUser); + } - function makeUser(user) { - return client.store.NewUser(user); - } + // step 1 + if (!user) { + if (data.user.username) { + user = makeUser(data.user); + } else { + return; + } + } - // step 1 - if (!user) { - if (data.user.username) { - user = makeUser(data.user); - }else { - return; - } - } + if (guild) { + const memberInGuild = guild.store.get('members', user.id); + if (!memberInGuild) { + const member = guild._addMember({ + user, + roles: data.roles, + deaf: false, + mute: false, + }, true); + client.emit(Constants.Events.GUILD_MEMBER_AVAILABLE, guild, member); + } + } - if (guild) { - let memberInGuild = guild.store.get('members', user.id); - if (!memberInGuild) { - let member = guild._addMember({ - user, - roles: data.roles, - deaf: false, - mute: false, - }, true); - client.emit(Constants.Events.GUILD_MEMBER_AVAILABLE, guild, member); - } - } + data.user.username = data.user.username || user.username; + data.user.id = data.user.id || user.id; + data.user.discriminator = data.user.discriminator || user.discriminator; - data.user.username = data.user.username || user.username; - data.user.id = data.user.id || user.id; - data.user.discriminator = data.user.discriminator || user.discriminator; + // comment out avatar patching as it causes bugs (see #297) + // data.user.avatar = data.user.avatar || user.avatar; + data.user.status = data.status || user.status; + data.user.game = data.game; - // comment out avatar patching as it causes bugs (see #297) - // data.user.avatar = data.user.avatar || user.avatar; - data.user.status = data.status || user.status; - data.user.game = data.game; + const same = ( + data.user.username === user.username && + data.user.id === user.id && + data.user.discriminator === user.discriminator && + data.user.avatar === user.avatar && + data.user.status === user.status && + JSON.stringify(data.user.game) === JSON.stringify(user.game) + ); - let same = ( - data.user.username === user.username && - data.user.id === user.id && - data.user.discriminator === user.discriminator && - data.user.avatar === user.avatar && - data.user.status === user.status && - JSON.stringify(data.user.game) === JSON.stringify(user.game) - ); + if (!same) { + const oldUser = cloneObject(user); + user.setup(data.user); + client.emit(Constants.Events.PRESENCE_UPDATE, oldUser, user); + } + } - if (!same) { - let oldUser = CloneObject(user); - user.setup(data.user); - client.emit(Constants.Events.PRESENCE_UPDATE, oldUser, user); - } - } - -}; +} module.exports = PresenceUpdateHandler; diff --git a/src/client/websocket/packets/handlers/Ready.js b/src/client/websocket/packets/handlers/Ready.js index ffd4cd0cb..e37d0a758 100644 --- a/src/client/websocket/packets/handlers/Ready.js +++ b/src/client/websocket/packets/handlers/Ready.js @@ -1,39 +1,30 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); -const ClientUser = Structure('ClientUser'); -const Guild = Structure('Guild'); -const DMChannel = Structure('DMChannel'); +const getStructure = name => require(`../../../../structures/${name}`); +const ClientUser = getStructure('ClientUser'); class ReadyHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; + client.manager.setupKeepAlive(data.heartbeat_interval); - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; - client.manager.setupKeepAlive(data.heartbeat_interval); + client.store.user = client.store.add('users', new ClientUser(client, data.user)); - client.store.user = client.store.add('users', new ClientUser(client, data.user)); + for (const guild of data.guilds) { + client.store.newGuild(guild); + } - for (let guild of data.guilds) { - client.store.NewGuild(guild); - } + for (const privateDM of data.private_channels) { + client.store.newChannel(privateDM); + } - for (let privateDM of data.private_channels) { - client.store.NewChannel(privateDM); - } + this.packetManager.ws.store.sessionID = data.session_id; - this.packetManager.ws.store.sessionID = data.session_id; + this.packetManager.ws.checkIfReady(); + } - this.packetManager.ws.checkIfReady(); - - } - -}; +} module.exports = ReadyHandler; diff --git a/src/client/websocket/packets/handlers/TypingStart.js b/src/client/websocket/packets/handlers/TypingStart.js index c64ff705a..421cba9b0 100644 --- a/src/client/websocket/packets/handlers/TypingStart.js +++ b/src/client/websocket/packets/handlers/TypingStart.js @@ -1,60 +1,52 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); const Constants = require('../../../../util/Constants'); -const CloneObject = require('../../../../util/CloneObject'); class TypingData { - constructor(since, lastTimestamp, _timeout) { - this.since = since; - this.lastTimestamp = lastTimestamp; - this._timeout = _timeout; - } + constructor(since, lastTimestamp, _timeout) { + this.since = since; + this.lastTimestamp = lastTimestamp; + this._timeout = _timeout; + } - resetTimeout(_timeout) { - clearTimeout(this._timeout); - this._timeout = _timeout; - } + resetTimeout(_timeout) { + clearTimeout(this._timeout); + this._timeout = _timeout; + } - get elapsedTime() { - return Date.now() - this.since; - } + get elapsedTime() { + return Date.now() - this.since; + } } class TypingStartHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; + const channel = client.store.get('channels', data.channel_id); + const user = client.store.get('users', data.user_id); + const timestamp = new Date(data.timestamp * 1000); - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; - let channel = client.store.get('channels', data.channel_id); - let user = client.store.get('users', data.user_id); - let timestamp = new Date(data.timestamp * 1000); + function tooLate() { + return setTimeout(() => { + client.emit(Constants.Events.TYPING_STOP, channel, user, channel.typingMap[user.id]); + delete channel.typingMap[user.id]; + }, 6000); + } - if (channel && user) { - if (channel.typingMap[user.id]) { - // already typing, renew - let mapping = channel.typingMap[user.id]; - mapping.lastTimestamp = timestamp; - mapping.resetTimeout(tooLate()); - } else { - channel.typingMap[user.id] = new TypingData(timestamp, timestamp, tooLate()); - client.emit(Constants.Events.TYPING_START, channel, user); - } - } + if (channel && user) { + if (channel.typingMap[user.id]) { + // already typing, renew + const mapping = channel.typingMap[user.id]; + mapping.lastTimestamp = timestamp; + mapping.resetTimeout(tooLate()); + } else { + channel.typingMap[user.id] = new TypingData(timestamp, timestamp, tooLate()); + client.emit(Constants.Events.TYPING_START, channel, user); + } + } + } - function tooLate() { - return setTimeout(() => { - client.emit(Constants.Events.TYPING_STOP, channel, user, channel.typingMap[user.id]); - delete channel.typingMap[user.id]; - }, 6000); - } - } - -}; +} module.exports = TypingStartHandler; diff --git a/src/client/websocket/packets/handlers/UserUpdate.js b/src/client/websocket/packets/handlers/UserUpdate.js index 8cefe8fcd..f1d6db65c 100644 --- a/src/client/websocket/packets/handlers/UserUpdate.js +++ b/src/client/websocket/packets/handlers/UserUpdate.js @@ -1,28 +1,14 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); -const CloneObject = name => require(`../../../../util/CloneObject`); -const Constants = require(`../../../../util/Constants`); - -const ClientUser = Structure('ClientUser'); -const Guild = Structure('Guild'); -const DMChannel = Structure('DMChannel'); class UserUpdateHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; + client.actions.UserUpdate.handle(data); + } - let response = client.actions.UserUpdate.handle(data); - - } - -}; +} module.exports = UserUpdateHandler; diff --git a/src/client/websocket/packets/handlers/VoiceStateUpdate.js b/src/client/websocket/packets/handlers/VoiceStateUpdate.js index 3838b7007..de7ac687a 100644 --- a/src/client/websocket/packets/handlers/VoiceStateUpdate.js +++ b/src/client/websocket/packets/handlers/VoiceStateUpdate.js @@ -1,43 +1,34 @@ -'use strict'; - const AbstractHandler = require('./AbstractHandler'); -const Structure = name => require(`../../../../structures/${name}`); -const Constants = require('../../../../util/Constants'); -const CloneObject = require('../../../../util/CloneObject'); -const Role = Structure('User'); +const Constants = require('../../../../util/Constants'); +const cloneObject = require('../../../../util/CloneObject'); class VoiceStateUpdateHandler extends AbstractHandler { - constructor(packetManager) { - super(packetManager); - } + handle(packet) { + const data = packet.d; + const client = this.packetManager.client; + const guild = client.store.get('guilds', data.guild_id); - handle(packet) { - let data = packet.d; - let client = this.packetManager.client; - let guild = client.store.get('guilds', data.guild_id); + if (guild) { + const member = guild.store.get('members', data.user_id); + if (member) { + const oldVoiceChannelMember = cloneObject(member); + if (member.voiceChannel && member.voiceChannel.id !== data.channel_id) { + member.voiceChannel.store.remove('members', oldVoiceChannelMember); + } - if (guild) { - let member = guild.store.get('members', data.user_id); - let channel = guild.store.get('channels', data.channel_id); - if (member) { - let oldVoiceChannelMember = CloneObject(member); - if (member.voiceChannel && member.voiceChannel.id !== data.channel_id) { - member.voiceChannel.store.remove('members', oldVoiceChannelMember); - } + member.serverMute = data.mute; + member.serverDeaf = data.deaf; + member.selfMute = data.self_mute; + member.selfDeaf = data.self_deaf; + member.voiceSessionID = data.session_id; + member.voiceChannelID = data.channel_id; + client.emit(Constants.Events.VOICE_STATE_UPDATE, oldVoiceChannelMember, member); + } + } + } - member.serverMute = data.mute; - member.serverDeaf = data.deaf; - member.selfMute = data.self_mute; - member.selfDeaf = data.self_deaf; - member.voiceSessionID = data.session_id; - member.voiceChannelID = data.channel_id; - client.emit(Constants.Events.VOICE_STATE_UPDATE, oldVoiceChannelMember, member); - } - } - } - -}; +} module.exports = VoiceStateUpdateHandler; diff --git a/src/index.js b/src/index.js index 6dc2c55cb..e5f018673 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,8 @@ -'use strict'; - const values = require('object.values'); const Client = require('./client/Client'); if (!Object.values) { - values.shim(); + values.shim(); } exports.Client = Client; diff --git a/src/structures/Channel.js b/src/structures/Channel.js index d6c3a9822..c9bed1141 100644 --- a/src/structures/Channel.js +++ b/src/structures/Channel.js @@ -1,26 +1,24 @@ -'use strict'; - class Channel { - constructor(client, data, guild) { - this.client = client; - this.typingMap = {}; - this.typingTimeouts = []; - if (guild) { - this.guild = guild; - } + constructor(client, data, guild) { + this.client = client; + this.typingMap = {}; + this.typingTimeouts = []; + if (guild) { + this.guild = guild; + } - if (data) { - this.setup(data); - } - } + if (data) { + this.setup(data); + } + } - setup(data) { - this.id = data.id; - } + setup(data) { + this.id = data.id; + } - delete() { - return this.client.rest.methods.DeleteChannel(this); - } + delete() { + return this.client.rest.methods.deleteChannel(this); + } } module.exports = Channel; diff --git a/src/structures/ClientUser.js b/src/structures/ClientUser.js index 923c571dd..412587785 100644 --- a/src/structures/ClientUser.js +++ b/src/structures/ClientUser.js @@ -1,37 +1,31 @@ -'use strict'; - const User = require('./User'); class ClientUser extends User { - constructor(client, data) { - super(client, data); - } + setup(data) { + super.setup(data); + this.verified = data.verified; + this.email = data.email; + } - setup(data) { - super.setup(data); - this.verified = data.verified; - this.email = data.email; - } + setUsername(username) { + return this.client.rest.methods.updateCurrentUser({ username }); + } - setUsername(username) { - return this.client.rest.methods.UpdateCurrentUser({ username, }); - } + setEmail(email) { + return this.client.rest.methods.updateCurrentUser({ email }); + } - setEmail(email) { - return this.client.rest.methods.UpdateCurrentUser({ email, }); - } + setPassword(password) { + return this.client.rest.methods.updateCurrentUser({ password }); + } - setPassword(password) { - return this.client.rest.methods.UpdateCurrentUser({ password, }); - } + setAvatar(avatar) { + return this.client.rest.methods.updateCurrentUser({ avatar }); + } - setAvatar(avatar) { - return this.client.rest.methods.UpdateCurrentUser({ avatar, }); - } - - edit(data) { - return this.client.rest.methods.UpdateCurrentUser(data); - } + edit(data) { + return this.client.rest.methods.updateCurrentUser(data); + } } module.exports = ClientUser; diff --git a/src/structures/DMChannel.js b/src/structures/DMChannel.js index f0b7523b2..53d0be758 100644 --- a/src/structures/DMChannel.js +++ b/src/structures/DMChannel.js @@ -1,40 +1,38 @@ -'use strict'; - const Channel = require('./Channel'); const TextBasedChannel = require('./interface/TextBasedChannel'); const User = require('./User'); const TextChannelDataStore = require('./datastore/TextChannelDataStore'); -class DMChannel extends Channel{ - constructor(client, data) { - super(client, data); - this.store = new TextChannelDataStore(); - } +class DMChannel extends Channel { + constructor(client, data) { + super(client, data); + this.store = new TextChannelDataStore(); + } - _cacheMessage(message) { - let maxSize = this.client.options.max_message_cache; - if (maxSize === 0) { - // saves on performance - return; - } + _cacheMessage(message) { + const maxSize = this.client.options.max_message_cache; + if (maxSize === 0) { + // saves on performance + return; + } - let storeKeys = Object.keys(this.store); - if (storeKeys.length >= maxSize) { - this.store.remove(storeKeys[0]); - } + const storeKeys = Object.keys(this.store); + if (storeKeys.length >= maxSize) { + this.store.remove(storeKeys[0]); + } - this.store.add('messages', message); - } + this.store.add('messages', message); + } - setup(data) { - super.setup(data); - this.recipient = this.client.store.add('users', new User(this.client, data.recipient)); - this.lastMessageID = data.last_message_id; - } + setup(data) { + super.setup(data); + this.recipient = this.client.store.add('users', new User(this.client, data.recipient)); + this.lastMessageID = data.last_message_id; + } - toString() { - return this.recipient.toString(); - } + toString() { + return this.recipient.toString(); + } } TextBasedChannel.applyToClass(DMChannel); diff --git a/src/structures/EvaluatedPermissions.js b/src/structures/EvaluatedPermissions.js index 30ef07844..32ac3557d 100644 --- a/src/structures/EvaluatedPermissions.js +++ b/src/structures/EvaluatedPermissions.js @@ -1,39 +1,37 @@ -'use strict'; - -const Constants = require('../Util/Constants'); +const Constants = require('../util/Constants'); class EvaluatedPermissions { - constructor(member, permissions) { - this.member = member; - this.permissions = permissions; - } + constructor(member, permissions) { + this.member = member; + this.permissions = permissions; + } - serialize() { - let serializedPermissions = {}; - for (let permissionName in Constants.PermissionFlags) { - serializedPermissions[permissionName] = this.hasPermission(permissionName); - } + serialize() { + const serializedPermissions = {}; + for (const permissionName in Constants.PermissionFlags) { + serializedPermissions[permissionName] = this.hasPermission(permissionName); + } - return serializedPermissions; - } + return serializedPermissions; + } - hasPermission(permission, explicit) { - if (permission instanceof String || typeof permission === 'string') { - permission = Constants.PermissionFlags[permission]; - } + hasPermission(permission, explicit) { + if (permission instanceof String || typeof permission === 'string') { + permission = Constants.PermissionFlags[permission]; + } - if (!permission) { - throw Constants.Errors.NOT_A_PERMISSION; - } + if (!permission) { + throw Constants.Errors.NOT_A_PERMISSION; + } - if (!explicit) { - if ((this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) { - return true; - } - } + if (!explicit) { + if ((this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) { + return true; + } + } - return ((this.permissions & permission) > 0); - } + return ((this.permissions & permission) > 0); + } } module.exports = EvaluatedPermissions; diff --git a/src/structures/Guild.js b/src/structures/Guild.js index b5bd9f5b3..596d9bf7a 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -1,244 +1,240 @@ -'use strict'; - const User = require('./User'); const GuildMember = require('./GuildMember'); const GuildDataStore = require('./datastore/GuildDataStore'); -const TextChannel = require('./TextChannel'); -const VoiceChannel = require('./VoiceChannel'); -const Constants = require('../Util/Constants'); +const Constants = require('../util/Constants'); const Role = require('./Role'); function arraysEqual(a, b) { - if (a === b) return true; - if (a.length !== b.length) return false; + if (a === b) return true; + if (a.length !== b.length) return false; - for (let itemInd in a) { - let item = a[itemInd]; - let ind = b.indexOf(item); - if (ind) { - b.splice(ind, 1); - } - } + for (const itemInd in a) { + const item = a[itemInd]; + const ind = b.indexOf(item); + if (ind) { + b.splice(ind, 1); + } + } - return b.length === 0; + return b.length === 0; } class Guild { - constructor(client, data) { - this.client = client; - this.store = new GuildDataStore(); + constructor(client, data) { + this.client = client; + this.store = new GuildDataStore(); - if (!data) { - return; - } + if (!data) { + return; + } - if (data.unavailable) { - this.available = false; - this.id = data.id; - } else { - this.available = true; - this.setup(data); - } - } + if (data.unavailable) { + this.available = false; + this.id = data.id; + } else { + this.available = true; + this.setup(data); + } + } - _addMember(guildUser, noEvent) { - if (!(guildUser.user instanceof User)) { - guildUser.user = this.client.store.NewUser(guildUser.user); - } + _addMember(guildUser, noEvent) { + if (!(guildUser.user instanceof User)) { + guildUser.user = this.client.store.newUser(guildUser.user); + } - guildUser.joined_at = guildUser.joined_at || 0; - let member = this.store.add('members', new GuildMember(this, guildUser)); - if (this.client.ws.status === Constants.Status.READY && !noEvent) { - this.client.emit(Constants.Events.GUILD_MEMBER_ADD, this, member); - } + guildUser.joined_at = guildUser.joined_at || 0; + const member = this.store.add('members', new GuildMember(this, guildUser)); + if (this.client.ws.status === Constants.Status.READY && !noEvent) { + this.client.emit(Constants.Events.GUILD_MEMBER_ADD, this, member); + } - return member; - } + return member; + } - _updateMember(member, data) { - let oldRoles = member.roles; + _updateMember(member, data) { + const oldRoles = member.roles; - member._roles = data.roles; - if (this.client.ws.status === Constants.Status.READY) { - this.client.emit(Constants.Events.GUILD_MEMBER_ROLES_UPDATE, this, oldRoles, member.roles); - } - } + member._roles = data.roles; + if (this.client.ws.status === Constants.Status.READY) { + this.client.emit(Constants.Events.GUILD_MEMBER_ROLES_UPDATE, this, oldRoles, member.roles); + } + } - _removeMember(guildMember) { - this.store.remove('members', guildMember); - } + _removeMember(guildMember) { + this.store.remove('members', guildMember); + } - toString() { - return this.name; - } + toString() { + return this.name; + } - kick(member) { - return this.member(member).kick(); - } + kick(member) { + return this.member(member).kick(); + } - member(user) { - return this.client.resolver.ResolveGuildMember(this, user); - } + member(user) { + return this.client.resolver.resolveGuildMember(this, user); + } - equals(data) { - let base = - this.id === data.id && - this.available === !data.unavailable && - this.splash === data.splash && - this.region === data.region && - this.name === data.name && - this.memberCount === data.member_count && - this.large === data.large && - this.icon === data.icon && - arraysEqual(this.features, data.features) && - this.owner.id === data.owner_id && - this.verificationLevel === data.verification_level && - this.embedEnabled === data.embed_enabled; + equals(data) { + let base = + this.id === data.id && + this.available === !data.unavailable && + this.splash === data.splash && + this.region === data.region && + this.name === data.name && + this.memberCount === data.member_count && + this.large === data.large && + this.icon === data.icon && + arraysEqual(this.features, data.features) && + this.owner.id === data.owner_id && + this.verificationLevel === data.verification_level && + this.embedEnabled === data.embed_enabled; - if (base) { - if (this.embedChannel) { - if (this.embedChannel.id !== data.embed_channel_id) { - base = false; - } - } else if (data.embed_channel_id) { - base = false; - } - } + if (base) { + if (this.embedChannel) { + if (this.embedChannel.id !== data.embed_channel_id) { + base = false; + } + } else if (data.embed_channel_id) { + base = false; + } + } - return base; - } + return base; + } - setup(data) { - this.id = data.id; - this.available = !data.unavailable; - this.splash = data.splash; - this.region = data.region; - this.name = data.name; - this.memberCount = data.member_count; - this.large = data.large; - this.joinDate = new Date(data.joined_at); - this.icon = data.icon; - this.features = data.features; - this.emojis = data.emojis; - this.afkTimeout = data.afk_timeout; - this.afkChannelID = data.afk_channel_id; - this.embedEnabled = data.embed_enabled; - this.verificationLevel = data.verification_level; - this.features = data.features || []; + setup(data) { + this.id = data.id; + this.available = !data.unavailable; + this.splash = data.splash; + this.region = data.region; + this.name = data.name; + this.memberCount = data.member_count; + this.large = data.large; + this.joinDate = new Date(data.joined_at); + this.icon = data.icon; + this.features = data.features; + this.emojis = data.emojis; + this.afkTimeout = data.afk_timeout; + this.afkChannelID = data.afk_channel_id; + this.embedEnabled = data.embed_enabled; + this.verificationLevel = data.verification_level; + this.features = data.features || []; - if (data.members) { - this.store.clear('members'); - for (let guildUser of data.members) { - this._addMember(guildUser); - } - } + if (data.members) { + this.store.clear('members'); + for (const guildUser of data.members) { + this._addMember(guildUser); + } + } - this.owner = this.store.get('members', data.owner_id); + this.owner = this.store.get('members', data.owner_id); - if (data.channels) { - this.store.clear('channels'); - for (let channel of data.channels) { - this.client.store.NewChannel(channel, this); - } - } + if (data.channels) { + this.store.clear('channels'); + for (const channel of data.channels) { + this.client.store.newChannel(channel, this); + } + } - this.embedChannel = this.store.get('channels', data.embed_channel_id); + this.embedChannel = this.store.get('channels', data.embed_channel_id); - if (data.roles) { - this.store.clear('roles'); - for (let role of data.roles) { - this.store.add('roles', new Role(this, role)); - } - } + if (data.roles) { + this.store.clear('roles'); + for (const role of data.roles) { + this.store.add('roles', new Role(this, role)); + } + } - if (data.presences) { - for (let presence of data.presences) { - let user = this.client.store.get('users', presence.user.id); - if (user) { - user.status = presence.status; - user.game = presence.game; - } - } - } + if (data.presences) { + for (const presence of data.presences) { + const user = this.client.store.get('users', presence.user.id); + if (user) { + user.status = presence.status; + user.game = presence.game; + } + } + } - if (data.voice_states) { - for (let voiceState of data.voice_states) { - let member = this.store.get('members', voiceState.user_id); - if (member) { - member.serverMute = voiceState.mute; - member.serverDeaf = voiceState.deaf; - member.selfMute = voiceState.self_mute; - member.selfDeaf = voiceState.self_deaf; - member.voiceSessionID = voiceState.session_id; - member.voiceChannelID = voiceState.channel_id; - } - } - } - } + if (data.voice_states) { + for (const voiceState of data.voice_states) { + const member = this.store.get('members', voiceState.user_id); + if (member) { + member.serverMute = voiceState.mute; + member.serverDeaf = voiceState.deaf; + member.selfMute = voiceState.self_mute; + member.selfDeaf = voiceState.self_deaf; + member.voiceSessionID = voiceState.session_id; + member.voiceChannelID = voiceState.channel_id; + } + } + } + } - createChannel(name, type) { - return this.client.rest.methods.CreateChannel(this, name, type); - } + createChannel(name, type) { + return this.client.rest.methods.createChannel(this, name, type); + } - createRole() { - return this.client.rest.methods.CreateGuildRole(this); - } + createRole() { + return this.client.rest.methods.createGuildRole(this); + } - leave() { - return this.client.rest.methods.LeaveGuild(this); - } + leave() { + return this.client.rest.methods.leaveGuild(this); + } - delete() { - return this.client.rest.methods.DeleteGuild(this); - } + delete() { + return this.client.rest.methods.deleteGuild(this); + } - edit(data) { - return this.client.rest.methods.UpdateGuild(this, data); - } + edit(data) { + return this.client.rest.methods.updateGuild(this, data); + } - setName(name) { - return this.edit({ name, }); - } + setName(name) { + return this.edit({ name }); + } - setRegion(region) { - return this.edit({ region, }); - } + setRegion(region) { + return this.edit({ region }); + } - setVerificationLevel(verificationLevel) { - return this.edit({ verificationLevel, }); - } + setVerificationLevel(verificationLevel) { + return this.edit({ verificationLevel }); + } - setAFKChannel(afkchannel) { - return this.edit({ afkChannel, }); - } + setAFKChannel(afkChannel) { + return this.edit({ afkChannel }); + } - setAFKTimeout(afkTimeout) { - return this.edit({ afkTimeout, }); - } + setAFKTimeout(afkTimeout) { + return this.edit({ afkTimeout }); + } - setIcon(icon) { - return this.edit({ icon, }); - } + setIcon(icon) { + return this.edit({ icon }); + } - setOwner(owner) { - return this.edit({ owner, }); - } + setOwner(owner) { + return this.edit({ owner }); + } - setSplash(splash) { - return this.edit({ splash, }); - } + setSplash(splash) { + return this.edit({ splash }); + } - get channels() { return this.store.getAsArray('channels'); } + get channels() { return this.store.getAsArray('channels'); } - get $channels() { return this.store.data.channels; } + get $channels() { return this.store.data.channels; } - get roles() { return this.store.getAsArray('roles'); } + get roles() { return this.store.getAsArray('roles'); } - get $roles() { return this.store.data.roles; } + get $roles() { return this.store.data.roles; } - get members() { return this.store.getAsArray('members'); } + get members() { return this.store.getAsArray('members'); } - get $members() { return this.store.data.members; } + get $members() { return this.store.data.members; } } module.exports = Guild; diff --git a/src/structures/GuildMember.js b/src/structures/GuildMember.js index d35cc163d..6c1b11afc 100644 --- a/src/structures/GuildMember.js +++ b/src/structures/GuildMember.js @@ -1,71 +1,69 @@ -'use strict'; - const TextBasedChannel = require('./interface/TextBasedChannel'); class GuildMember { - constructor(guild, data) { - this.client = guild.client; - this.guild = guild; - this.user = {}; - this._roles = []; - if (data) { - this.setup(data); - } - } + constructor(guild, data) { + this.client = guild.client; + this.guild = guild; + this.user = {}; + this._roles = []; + if (data) { + this.setup(data); + } + } - setup(data) { - this.user = data.user; - this.serverDeaf = data.deaf; - this.serverMute = data.mute; - this.selfMute = data.self_mute; - this.selfDeaf = data.self_deaf; - this.voiceSessionID = data.session_id; - this.voiceChannelID = data.channel_id; - this.joinDate = new Date(data.joined_at); - this._roles = data.roles; - } + setup(data) { + this.user = data.user; + this.serverDeaf = data.deaf; + this.serverMute = data.mute; + this.selfMute = data.self_mute; + this.selfDeaf = data.self_deaf; + this.voiceSessionID = data.session_id; + this.voiceChannelID = data.channel_id; + this.joinDate = new Date(data.joined_at); + this._roles = data.roles; + } - get roles() { - let list = []; - let everyoneRole = this.guild.store.get('roles', this.guild.id); + get roles() { + const list = []; + const everyoneRole = this.guild.store.get('roles', this.guild.id); - if (everyoneRole) { - list.push(everyoneRole); - } + if (everyoneRole) { + list.push(everyoneRole); + } - for (let roleID of this._roles) { - let role = this.guild.store.get('roles', roleID); - if (role) { - list.push(role); - } - } + for (const roleID of this._roles) { + const role = this.guild.store.get('roles', roleID); + if (role) { + list.push(role); + } + } - return list; - } + return list; + } - get mute() { - return this.selfMute || this.serverMute; - } + get mute() { + return this.selfMute || this.serverMute; + } - get deaf() { - return this.selfDeaf || this.serverDeaf; - } + get deaf() { + return this.selfDeaf || this.serverDeaf; + } - get voiceChannel() { - return this.guild.store.get('channels', this.voiceChannelID); - } + get voiceChannel() { + return this.guild.store.get('channels', this.voiceChannelID); + } - get id() { - return this.user.id; - } + get id() { + return this.user.id; + } - deleteDM() { - return this.client.rest.methods.DeleteChannel(this); - } + deleteDM() { + return this.client.rest.methods.deleteChannel(this); + } - kick() { - return this.client.rest.methods.KickGuildMember(this.guild, this); - } + kick() { + return this.client.rest.methods.kickGuildMember(this.guild, this); + } } TextBasedChannel.applyToClass(GuildMember); diff --git a/src/structures/Message.js b/src/structures/Message.js index d5118ca70..b565737ce 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -1,111 +1,117 @@ -'use strict'; - class Message { - constructor(channel, data, client) { - this.channel = channel; + constructor(channel, data, client) { + this.channel = channel; - if (channel.guild) { - this.guild = channel.guild; - } + if (channel.guild) { + this.guild = channel.guild; + } - this.client = client; - if (data) { - this.setup(data); - } - } + this.client = client; + if (data) { + this.setup(data); + } + } - setup(data) { - this.author = this.client.store.NewUser(data.author); - this.content = data.content; - this.timestamp = new Date(data.timestamp); - this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp) : null; - this.tts = data.tts; - this.mentionEveryone = data.mention_everyone; - this.nonce = data.nonce; - this.embeds = data.embeds; - this.attachments = data.attachments; - this.mentions = []; - this.id = data.id; - for (let mention of data.mentions) { - let user = this.client.store.get('users', mention.id); - if (user) { - this.mentions.push(user); - } else { - user = this.client.store.NewUser(mention); - this.mentions.push(user); - } - } - } + setup(data) { + this.author = this.client.store.newUser(data.author); + this.content = data.content; + this.timestamp = new Date(data.timestamp); + this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp) : null; + this.tts = data.tts; + this.mentionEveryone = data.mention_everyone; + this.nonce = data.nonce; + this.embeds = data.embeds; + this.attachments = data.attachments; + this.mentions = []; + this.id = data.id; + for (const mention of data.mentions) { + let user = this.client.store.get('users', mention.id); + if (user) { + this.mentions.push(user); + } else { + user = this.client.store.newUser(mention); + this.mentions.push(user); + } + } + } - patch(data) { - if (data.author) - this.author = this.client.store.get('users', data.author.id); - if (data.content) - this.content = data.content; - if (data.timestamp) - this.timestamp = new Date(data.timestamp); - if (data.edited_timestamp) - this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp) : null; - if (data.tts) - this.tts = data.tts; - if (data.mention_everyone) - this.mentionEveryone = data.mention_everyone; - if (data.nonce) - this.nonce = data.nonce; - if (data.embeds) - this.embeds = data.embeds; - if (data.attachments) - this.attachments = data.attachments; - if (data.mentions) { - for (let mention of data.mentions) { - let user = this.client.store.get('users', mention.id); - if (user) { - this.mentions.push(user); - } else { - user = this.client.store.NewUser(mention); - this.mentions.push(user); - } - } - } + patch(data) { + if (data.author) { + this.author = this.client.store.get('users', data.author.id); + } + if (data.content) { + this.content = data.content; + } + if (data.timestamp) { + this.timestamp = new Date(data.timestamp); + } + if (data.edited_timestamp) { + this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp) : null; + } + if (data.tts) { + this.tts = data.tts; + } + if (data.mention_everyone) { + this.mentionEveryone = data.mention_everyone; + } + if (data.nonce) { + this.nonce = data.nonce; + } + if (data.embeds) { + this.embeds = data.embeds; + } + if (data.attachments) { + this.attachments = data.attachments; + } + if (data.mentions) { + for (const mention of data.mentions) { + let user = this.client.store.get('users', mention.id); + if (user) { + this.mentions.push(user); + } else { + user = this.client.store.newUser(mention); + this.mentions.push(user); + } + } + } - if (data.id) - this.id = data.id; - } + if (data.id) { + this.id = data.id; + } + } - equals(message, rawData) { + equals(message, rawData) { + const embedUpdate = !message.author && !message.attachments; - let embedUpdate = !message.author && !message.attachments; + if (embedUpdate) { + const base = this.id === message.id && + this.embeds.length === message.embeds.length; + return base; + } + let base = this.id === message.id && + this.author.id === message.author.id && + this.content === message.content && + this.tts === message.tts && + this.nonce === message.nonce && + this.embeds.length === message.embeds.length && + this.attachments.length === message.attachments.length; - if (embedUpdate) { - let base = this.id === message.id && - this.embeds.length === message.embeds.length; - return base; - } else { - let base = this.id === message.id && - this.author.id === message.author.id && - this.content === message.content && - this.tts === message.tts && - this.nonce === message.nonce && - this.embeds.length === message.embeds.length && - this.attachments.length === message.attachments.length; + if (base && rawData) { + base = this.mentionEveryone === message.mentionEveryone && + this.timestamp.getTime() === new Date(rawData.timestamp).getTime() && + this.editedTimestamp === new Date(rawData.edited_timestamp).getTime(); + } - if (base && rawData) { - base = this.mentionEveryone === message.mentionEveryone && - this.timestamp.getTime() === new Date(data.timestamp).getTime() && - this.editedTimestamp === new Date(data.edited_timestamp).getTime(); - } + return base; + } - return base; - } - } + delete() { + return this.client.rest.methods.deleteMessage(this); + } - delete() { - return this.client.rest.methods.DeleteMessage(this); - } - - edit(content) { - return this.client.rest.methods.UpdateMessage(this, content); - } + edit(content) { + return this.client.rest.methods.updateMessage(this, content); + } } module.exports = Message; diff --git a/src/structures/PermissionOverwrites.js b/src/structures/PermissionOverwrites.js index 717c94d46..47177d3fc 100644 --- a/src/structures/PermissionOverwrites.js +++ b/src/structures/PermissionOverwrites.js @@ -1,19 +1,17 @@ -'use strict'; - class PermissionOverwrites { - constructor(serverChannel, data) { - this.channel = serverChannel; - if (data) { - this.setup(data); - } - } + constructor(serverChannel, data) { + this.channel = serverChannel; + if (data) { + this.setup(data); + } + } - setup(data) { - this.type = data.type; - this.id = data.id; - this.denyData = data.deny; - this.allowData = data.allow; - } + setup(data) { + this.type = data.type; + this.id = data.id; + this.denyData = data.deny; + this.allowData = data.allow; + } } module.exports = PermissionOverwrites; diff --git a/src/structures/Role.js b/src/structures/Role.js index f163ff98e..9d14c9074 100644 --- a/src/structures/Role.js +++ b/src/structures/Role.js @@ -1,92 +1,90 @@ -'use strict'; - -const Constants = require('../Util/Constants'); +const Constants = require('../util/Constants'); class Role { - constructor(guild, data) { - this.guild = guild; - this.client = guild.client; - if (data) { - this.setup(data); - } - } + constructor(guild, data) { + this.guild = guild; + this.client = guild.client; + if (data) { + this.setup(data); + } + } - equals(role) { - return ( - this.id === role.id && - this.name === role.name && - this.color === role.color && - this.hoist === role.hoist && - this.position === role.position && - this.permissions === role.permissions && - this.managed === role.managed - ); - } + equals(role) { + return ( + this.id === role.id && + this.name === role.name && + this.color === role.color && + this.hoist === role.hoist && + this.position === role.position && + this.permissions === role.permissions && + this.managed === role.managed + ); + } - setup(data) { - this.id = data.id; - this.name = data.name; - this.color = data.color; - this.hoist = data.hoist; - this.position = data.position; - this.permissions = data.permissions; - this.managed = data.managed; - } + setup(data) { + this.id = data.id; + this.name = data.name; + this.color = data.color; + this.hoist = data.hoist; + this.position = data.position; + this.permissions = data.permissions; + this.managed = data.managed; + } - delete() { - return this.client.rest.methods.DeleteGuildRole(this); - } + delete() { + return this.client.rest.methods.deleteGuildRole(this); + } - edit(data) { - return this.client.rest.methods.UpdateGuildRole(this, data); - } + edit(data) { + return this.client.rest.methods.updateGuildRole(this, data); + } - setName(name) { - return this.client.rest.methods.UpdateGuildRole(this, {name,}); - } + setName(name) { + return this.client.rest.methods.updateGuildRole(this, { name }); + } - setColor(color) { - return this.client.rest.methods.UpdateGuildRole(this, {color,}); - } + setColor(color) { + return this.client.rest.methods.updateGuildRole(this, { color }); + } - setHoist(hoist) { - return this.client.rest.methods.UpdateGuildRole(this, {hoist,}); - } + setHoist(hoist) { + return this.client.rest.methods.updateGuildRole(this, { hoist }); + } - setPosition(position) { - return this.client.rest.methods.UpdateGuildRole(this, {position,}); - } + setPosition(position) { + return this.client.rest.methods.updateGuildRole(this, { position }); + } - setPermissions(permissions) { - return this.client.rest.methods.UpdateGuildRole(this, {permissions,}); - } + setPermissions(permissions) { + return this.client.rest.methods.updateGuildRole(this, { permissions }); + } - serialize() { - let serializedPermissions = {}; - for (let permissionName in Constants.PermissionFlags) { - serializedPermissions[permissionName] = this.hasPermission(permissionName); - } + serialize() { + const serializedPermissions = {}; + for (const permissionName in Constants.PermissionFlags) { + serializedPermissions[permissionName] = this.hasPermission(permissionName); + } - return serializedPermissions; - } + return serializedPermissions; + } - hasPermission(permission, explicit) { - if (permission instanceof String || typeof permission === 'string') { - permission = Constants.PermissionFlags[permission]; - } + hasPermission(permission, explicit) { + if (permission instanceof String || typeof permission === 'string') { + permission = Constants.PermissionFlags[permission]; + } - if (!permission) { - throw Constants.Errors.NOT_A_PERMISSION; - } + if (!permission) { + throw Constants.Errors.NOT_A_PERMISSION; + } - if (!explicit) { - if ((this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) { - return true; - } - } + if (!explicit) { + if ((this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) { + return true; + } + } - return ((this.permissions & permission) > 0); - } + return ((this.permissions & permission) > 0); + } } module.exports = Role; diff --git a/src/structures/ServerChannel.js b/src/structures/ServerChannel.js index 3ff271477..2e7ee4f93 100644 --- a/src/structures/ServerChannel.js +++ b/src/structures/ServerChannel.js @@ -1,151 +1,149 @@ -'use strict'; - const Channel = require('./Channel'); const PermissionOverwrites = require('./PermissionOverwrites'); const EvaluatedPermissions = require('./EvaluatedPermissions'); const Constants = require('../util/Constants'); function arraysEqual(a, b) { - if (a === b) return true; - if (a.length !== b.length) return false; + if (a === b) return true; + if (a.length !== b.length) return false; - for (let itemInd in a) { - let item = a[itemInd]; - let ind = b.indexOf(item); - if (ind) { - b.splice(ind, 1); - } - } + for (const itemInd in a) { + const item = a[itemInd]; + const ind = b.indexOf(item); + if (ind) { + b.splice(ind, 1); + } + } - return b.length === 0; + return b.length === 0; } -class ServerChannel extends Channel{ - constructor(guild, data) { - super(guild.client, data, guild); - } +class ServerChannel extends Channel { + constructor(guild, data) { + super(guild.client, data, guild); + } - setup(data) { - super.setup(data); - this.type = data.type; - this.topic = data.topic; - this.position = data.position; - this.name = data.name; - this.lastMessageID = data.last_message_id; - this.ow = data.permission_overwrites; - this.permissionOverwrites = []; - if (data.permission_overwrites) { - for (let overwrite of data.permission_overwrites) { - this.permissionOverwrites.push(new PermissionOverwrites(this, overwrite)); - } - } - } + setup(data) { + super.setup(data); + this.type = data.type; + this.topic = data.topic; + this.position = data.position; + this.name = data.name; + this.lastMessageID = data.last_message_id; + this.ow = data.permission_overwrites; + this.permissionOverwrites = []; + if (data.permission_overwrites) { + for (const overwrite of data.permission_overwrites) { + this.permissionOverwrites.push(new PermissionOverwrites(this, overwrite)); + } + } + } - equals(other) { - let base = ( - this.type === other.type && - this.topic === other.topic && - this.position === other.position && - this.name === other.name && - this.id === other.id - ); + equals(other) { + let base = ( + this.type === other.type && + this.topic === other.topic && + this.position === other.position && + this.name === other.name && + this.id === other.id + ); - if (base) { - if (other.permission_overwrites) { - let thisIDSet = this.permissionOverwrites.map(overwrite => overwrite.id); - let otherIDSet = other.permission_overwrites.map(overwrite => overwrite.id); - if (arraysEqual(thisIDSet, otherIDSet)) { - base = true; - } else { - base = false; - } - } else { - base = false; - } - } + if (base) { + if (other.permission_overwrites) { + const thisIDSet = this.permissionOverwrites.map(overwrite => overwrite.id); + const otherIDSet = other.permission_overwrites.map(overwrite => overwrite.id); + if (arraysEqual(thisIDSet, otherIDSet)) { + base = true; + } else { + base = false; + } + } else { + base = false; + } + } - return base; - } + return base; + } - permissionsFor(member) { - member = this.client.resolver.ResolveGuildMember(this.guild, member); - if (member) { - if (this.guild.owner.id === member.id) { - return new EvaluatedPermissions(member, Constants.ALL_PERMISSIONS); - } + permissionsFor(member) { + member = this.client.resolver.resolveGuildMember(this.guild, member); + if (member) { + if (this.guild.owner.id === member.id) { + return new EvaluatedPermissions(member, Constants.ALL_PERMISSIONS); + } - let roles = member.roles; - let permissions = 0; - let overwrites = this.overwritesFor(member, true); + const roles = member.roles; + let permissions = 0; + const overwrites = this.overwritesFor(member, true); - for (let role of roles) { - permissions |= role.permissions; - } + for (const role of roles) { + permissions |= role.permissions; + } - for (let overwrite of overwrites.role.concat(overwrites.member)) { - permissions = permissions & ~overwrite.denyData; - permissions = permissions | overwrite.allowData; - } + for (const overwrite of overwrites.role.concat(overwrites.member)) { + permissions &= ~overwrite.denyData; + permissions |= overwrite.allowData; + } - if (!!(permissions & (Constants.PermissionFlags.MANAGE_ROLES))) { - permissions = Constants.ALL_PERMISSIONS; - } + const admin = Boolean(permissions & (Constants.PermissionFlags.MANAGE_ROLES)); + if (admin) { + permissions = Constants.ALL_PERMISSIONS; + } - return new EvaluatedPermissions(member, permissions); - } - } + return new EvaluatedPermissions(member, permissions); + } + return null; + } - overwritesFor(member, verified) { - // for speed - if (!verified) - member = this.client.resolver.ResolveGuildMember(this.guild, member); - if (member) { - let found = []; - let memberRoles = member._roles; + overwritesFor(member, verified) { + // for speed + if (!verified) member = this.client.resolver.resolveGuildMember(this.guild, member); + if (member) { + const memberRoles = member._roles; - let roleOverwrites = []; - let memberOverwrites = []; + const roleOverwrites = []; + const memberOverwrites = []; - for (let overwrite of this.permissionOverwrites) { - if (overwrite.id === member.id) { - memberOverwrites.push(overwrite); - } else if (memberRoles.indexOf(overwrite.id) > -1) { - roleOverwrites.push(overwrite); - } - } + for (const overwrite of this.permissionOverwrites) { + if (overwrite.id === member.id) { + memberOverwrites.push(overwrite); + } else if (memberRoles.indexOf(overwrite.id) > -1) { + roleOverwrites.push(overwrite); + } + } - return { - role: roleOverwrites, - member: memberOverwrites, - }; - } + return { + role: roleOverwrites, + member: memberOverwrites, + }; + } - return []; - } + return []; + } - edit(data) { - return this.client.rest.methods.UpdateChannel(this, data); - } + edit(data) { + return this.client.rest.methods.updateChannel(this, data); + } - setName(name) { - return this.client.rest.methods.UpdateChannel(this, { name, }); - } + setName(name) { + return this.client.rest.methods.updateChannel(this, { name }); + } - setPosition(position) { - return this.rest.client.rest.methods.UpdateChannel(this, { position, }); - } + setPosition(position) { + return this.rest.client.rest.methods.updateChannel(this, { position }); + } - setTopic(topic) { - return this.rest.client.rest.methods.UpdateChannel(this, { topic, }); - } + setTopic(topic) { + return this.rest.client.rest.methods.updateChannel(this, { topic }); + } - setBitrate() { - return this.rest.client.rest.methods.UpdateChannel(this, { bitrate, }); - } + setBitrate(bitrate) { + return this.rest.client.rest.methods.updateChannel(this, { bitrate }); + } - toString() { - return this.name; - } + toString() { + return this.name; + } } module.exports = ServerChannel; diff --git a/src/structures/TextChannel.js b/src/structures/TextChannel.js index 73986118c..c87b27ed4 100644 --- a/src/structures/TextChannel.js +++ b/src/structures/TextChannel.js @@ -1,30 +1,28 @@ -'use strict'; - const ServerChannel = require('./ServerChannel'); const TextChannelDataStore = require('./datastore/TextChannelDataStore'); const TextBasedChannel = require('./interface/TextBasedChannel'); class TextChannel extends ServerChannel { - constructor(guild, data) { - super(guild, data); - this.store = new TextChannelDataStore(); - } + constructor(guild, data) { + super(guild, data); + this.store = new TextChannelDataStore(); + } - _cacheMessage(message) { - let maxSize = this.client.options.max_message_cache; - if (maxSize === 0) { - // saves on performance - return; - } + _cacheMessage(message) { + const maxSize = this.client.options.max_message_cache; + if (maxSize === 0) { + // saves on performance + return null; + } - let storeKeys = Object.keys(this.store); - if (storeKeys.length >= maxSize) { - this.store.remove(storeKeys[0]); - } + const storeKeys = Object.keys(this.store); + if (storeKeys.length >= maxSize) { + this.store.remove(storeKeys[0]); + } - return this.store.add('messages', message); - } + return this.store.add('messages', message); + } } TextBasedChannel.applyToClass(TextChannel); diff --git a/src/structures/User.js b/src/structures/User.js index 2563fccb2..823c8597d 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -1,89 +1,87 @@ -'use strict'; - const TextBasedChannel = require('./interface/TextBasedChannel'); /** * Represents a User on Discord. */ class User { - constructor(client, data) { - this.client = client; - if (data) { - this.setup(data); - } - } + constructor(client, data) { + this.client = client; + if (data) { + this.setup(data); + } + } - setup(data) { - /** - * The username of the User - * @type {String} - */ - this.username = data.username; - /** - * The ID of the User - * @type {String} - */ - this.id = data.id; - /** - * A discriminator based on username for the User - * @type {String} - */ - this.discriminator = data.discriminator; - /** - * The ID of the user's avatar - * @type {String} - */ - this.avatar = data.avatar; - /** - * Whether or not the User is a Bot. - * @type {Boolean} - */ - this.bot = Boolean(data.bot); - /** - * The status of the user: - * - * * **`online`** - user is online - * * **`offline`** - user is offline - * * **`idle`** - user is AFK - * @type {String} - */ - this.status = data.status || this.status || 'offline'; - this.game = data.game || this.game; - } + setup(data) { + /** + * The username of the User + * @type {String} + */ + this.username = data.username; + /** + * The ID of the User + * @type {String} + */ + this.id = data.id; + /** + * A discriminator based on username for the User + * @type {String} + */ + this.discriminator = data.discriminator; + /** + * The ID of the user's avatar + * @type {String} + */ + this.avatar = data.avatar; + /** + * Whether or not the User is a Bot. + * @type {Boolean} + */ + this.bot = Boolean(data.bot); + /** + * The status of the user: + * + * * **`online`** - user is online + * * **`offline`** - user is offline + * * **`idle`** - user is AFK + * @type {String} + */ + this.status = data.status || this.status || 'offline'; + this.game = data.game || this.game; + } - toString() { - return `<@${this.id}>`; - } + toString() { + return `<@${this.id}>`; + } - /** - * Deletes a DM Channel (if one exists) between the Client and the User. Resolves with the Channel if successful. - * @return {Promise} - */ - deleteDM() { - return this.client.rest.methods.DeleteChannel(this); - } + /** + * Deletes a DM Channel (if one exists) between the Client and the User. Resolves with the Channel if successful. + * @return {Promise} + */ + deleteDM() { + return this.client.rest.methods.deleteChannel(this); + } - equals(user) { - let base = ( - this.username === user.username && - this.id === user.id && - this.discriminator === user.discriminator && - this.avatar === user.avatar && - this.bot === Boolean(user.bot) - ); + equals(user) { + let base = ( + this.username === user.username && + this.id === user.id && + this.discriminator === user.discriminator && + this.avatar === user.avatar && + this.bot === Boolean(user.bot) + ); - if (base) { - if (user.status) { - base = this.status === user.status; - } + if (base) { + if (user.status) { + base = this.status === user.status; + } - if (user.game) { - base = this.game === user.game; - } - } + if (user.game) { + base = this.game === user.game; + } + } - return base; - } + return base; + } } TextBasedChannel.applyToClass(User); diff --git a/src/structures/VoiceChannel.js b/src/structures/VoiceChannel.js index fb54df361..9c643820d 100644 --- a/src/structures/VoiceChannel.js +++ b/src/structures/VoiceChannel.js @@ -1,18 +1,16 @@ -'use strict'; - const ServerChannel = require('./ServerChannel'); const VoiceChannelDataStore = require('./datastore/VoiceChannelDataStore'); class VoiceChannel extends ServerChannel { - constructor(guild, data) { - super(guild, data); - this.store = new VoiceChannelDataStore(); - } + constructor(guild, data) { + super(guild, data); + this.store = new VoiceChannelDataStore(); + } - setup(data) { - super.setup(data); - this.bitrate = data.bitrate; - } + setup(data) { + super.setup(data); + this.bitrate = data.bitrate; + } } module.exports = VoiceChannel; diff --git a/src/structures/datastore/AbstractDataStore.js b/src/structures/datastore/AbstractDataStore.js index 24bfec497..5ec83ecad 100644 --- a/src/structures/datastore/AbstractDataStore.js +++ b/src/structures/datastore/AbstractDataStore.js @@ -1,43 +1,40 @@ -'use strict'; +class AbstractDataStore { + constructor() { + this.data = {}; + } -class AbstractDataStore{ - constructor() { - this.data = {}; - } + register(name) { + this.data[name] = {}; + } - register(name) { - this.data[name] = {}; - } + add(location, object) { + if (this.data[location][object.id]) { + return this.data[location][object.id]; + } + this.data[location][object.id] = object; + return object; + } - add(location, object) { - if (this.data[location][object.id]) { - return this.data[location][object.id]; - } else { - return this.data[location][object.id] = object; - } - } + clear(location) { + this.data[location] = {}; + } - clear(location) { - this.data[location] = {}; - } + remove(location, object) { + const id = (typeof object === 'string' || object instanceof String) ? object : object.id; + if (this.data[location][id]) { + delete this.data[location][id]; + return true; + } + return false; + } - remove(location, object) { - let id = (typeof object === 'string' || object instanceof String) ? object : object.id; - if (this.data[location][id]) { - delete this.data[location][id]; - return true; - } else { - return false; - } - } + get(location, value) { + return this.data[location][value]; + } - get(location, value) { - return this.data[location][value]; - } - - getAsArray(location) { - return Object.values(this.data[location]); - } + getAsArray(location) { + return Object.values(this.data[location]); + } } module.exports = AbstractDataStore; diff --git a/src/structures/datastore/ClientDataStore.js b/src/structures/datastore/ClientDataStore.js index a7b39c19d..4d0dd0a87 100644 --- a/src/structures/datastore/ClientDataStore.js +++ b/src/structures/datastore/ClientDataStore.js @@ -1,8 +1,6 @@ -'use strict'; - const AbstractDataStore = require('./AbstractDataStore'); const Constants = require('../../util/Constants'); -const CloneObject = require('../../util/CloneObject'); +const cloneObject = require('../../util/CloneObject'); const Guild = require('../Guild'); const User = require('../User'); const DMChannel = require('../DMChannel'); @@ -10,98 +8,99 @@ const TextChannel = require('../TextChannel'); const VoiceChannel = require('../VoiceChannel'); const ServerChannel = require('../ServerChannel'); -class ClientDataStore extends AbstractDataStore{ - constructor(client) { - super(); +class ClientDataStore extends AbstractDataStore { + constructor(client) { + super(); - this.client = client; - this.token = null; - this.session = null; - this.user = null; - this.email = null; - this.password = null; + this.client = client; + this.token = null; + this.session = null; + this.user = null; + this.email = null; + this.password = null; - this.register('users'); - this.register('guilds'); - this.register('channels'); - } + this.register('users'); + this.register('guilds'); + this.register('channels'); + } - get pastReady() { - return this.client.ws.status === Constants.Status.READY; - } + get pastReady() { + return this.client.ws.status === Constants.Status.READY; + } - NewGuild(data) { - let already = this.get('guilds', data.id); - let guild = this.add('guilds', new Guild(this.client, data)); - if (this.pastReady && !already) { - this.client.emit(Constants.Events.GUILD_CREATE, guild); - } + newGuild(data) { + const already = this.get('guilds', data.id); + const guild = this.add('guilds', new Guild(this.client, data)); + if (this.pastReady && !already) { + this.client.emit(Constants.Events.GUILD_CREATE, guild); + } - return guild; - } + return guild; + } - NewUser(data) { - return this.add('users', new User(this.client, data)); - } + newUser(data) { + return this.add('users', new User(this.client, data)); + } - NewChannel(data, guild) { - let already = this.get('channels', data.id); - let channel; - if (data.is_private) { - channel = new DMChannel(this.client, data); - }else { - guild = guild || this.get('guilds', data.guild_id); - if (guild) { - if (data.type === 'text') { - channel = new TextChannel(guild, data); - guild.store.add('channels', channel); - }else if (data.type === 'voice') { - channel = new VoiceChannel(guild, data); - guild.store.add('channels', channel); - } - } - } + newChannel(data, $guild) { + let guild = $guild; + const already = this.get('channels', data.id); + let channel; + if (data.is_private) { + channel = new DMChannel(this.client, data); + } else { + guild = guild || this.get('guilds', data.guild_id); + if (guild) { + if (data.type === 'text') { + channel = new TextChannel(guild, data); + guild.store.add('channels', channel); + } else if (data.type === 'voice') { + channel = new VoiceChannel(guild, data); + guild.store.add('channels', channel); + } + } + } - if (channel) { - if (this.pastReady && !already) { - this.client.emit(Constants.Events.CHANNEL_CREATE, channel); - } + if (channel) { + if (this.pastReady && !already) { + this.client.emit(Constants.Events.CHANNEL_CREATE, channel); + } - return this.add('channels', channel); - } - } + return this.add('channels', channel); + } + return null; + } - KillGuild(guild) { - let already = this.get('guilds', guilds.id); - this.remove('guilds', guild); - if (already && this.pastReady) { - this.client.emit(Constants.Events.GUILD_DELETE, guild); - } - } + killGuild(guild) { + const already = this.get('guilds', guild.id); + this.remove('guilds', guild); + if (already && this.pastReady) { + this.client.emit(Constants.Events.GUILD_DELETE, guild); + } + } - KillUser(user) { - this.remove('users', user); - } + killUser(user) { + this.remove('users', user); + } - KillChannel(channel) { - this.remove('channels', channel); - if (channel instanceof ServerChannel) { - channel.guild.store.remove('channels', channel); - } - } + killChannel(channel) { + this.remove('channels', channel); + if (channel instanceof ServerChannel) { + channel.guild.store.remove('channels', channel); + } + } - UpdateGuild(currentGuild, newData) { - let oldGuild = CloneObject(currentGuild); - currentGuild.setup(newData); - if (this.pastReady) { - this.client.emit(Constants.Events.GUILD_UPDATE, oldGuild, currentGuild); - } - } + updateGuild(currentGuild, newData) { + const oldGuild = cloneObject(currentGuild); + currentGuild.setup(newData); + if (this.pastReady) { + this.client.emit(Constants.Events.GUILD_UPDATE, oldGuild, currentGuild); + } + } - UpdateChannel(currentChannel, newData) { - let oldChannel = CloneObject(currentChannel); - currentChannel.setup(newData); - } + updateChannel(currentChannel, newData) { + currentChannel.setup(newData); + } } module.exports = ClientDataStore; diff --git a/src/structures/datastore/GuildDataStore.js b/src/structures/datastore/GuildDataStore.js index 68f8fa5d7..bea556765 100644 --- a/src/structures/datastore/GuildDataStore.js +++ b/src/structures/datastore/GuildDataStore.js @@ -1,14 +1,12 @@ -'use strict'; - const AbstractDataStore = require('./AbstractDataStore'); -class GuildDataStore extends AbstractDataStore{ - constructor() { - super(); +class GuildDataStore extends AbstractDataStore { + constructor() { + super(); - this.register('members'); - this.register('channels'); - } + this.register('members'); + this.register('channels'); + } } module.exports = GuildDataStore; diff --git a/src/structures/datastore/TextChannelDataStore.js b/src/structures/datastore/TextChannelDataStore.js index 08316431b..1cf54a825 100644 --- a/src/structures/datastore/TextChannelDataStore.js +++ b/src/structures/datastore/TextChannelDataStore.js @@ -1,12 +1,10 @@ -'use strict'; - const AbstractDataStore = require('./AbstractDataStore'); -class TextChannelDataStore extends AbstractDataStore{ - constructor() { - super(); - this.register('messages'); - } +class TextChannelDataStore extends AbstractDataStore { + constructor() { + super(); + this.register('messages'); + } } module.exports = TextChannelDataStore; diff --git a/src/structures/datastore/VoiceChannelDataStore.js b/src/structures/datastore/VoiceChannelDataStore.js index 88b9da7bb..9705d5c77 100644 --- a/src/structures/datastore/VoiceChannelDataStore.js +++ b/src/structures/datastore/VoiceChannelDataStore.js @@ -1,12 +1,10 @@ -'use strict'; - const AbstractDataStore = require('./AbstractDataStore'); -class VoiceChannelDataStore extends AbstractDataStore{ - constructor() { - super(); - this.register('members'); - } +class VoiceChannelDataStore extends AbstractDataStore { + constructor() { + super(); + this.register('members'); + } } module.exports = VoiceChannelDataStore; diff --git a/src/structures/datastore/WebSocketManagerDataStore.js b/src/structures/datastore/WebSocketManagerDataStore.js index a63d41369..82f8d94c4 100644 --- a/src/structures/datastore/WebSocketManagerDataStore.js +++ b/src/structures/datastore/WebSocketManagerDataStore.js @@ -1,14 +1,12 @@ -'use strict'; - const AbstractDataStore = require('./AbstractDataStore'); -class WebSocketManagerDataStore extends AbstractDataStore{ - constructor() { - super(); - this.sessionID = null; - this.sequence = -1; - this.gateway = null; - } +class WebSocketManagerDataStore extends AbstractDataStore { + constructor() { + super(); + this.sessionID = null; + this.sequence = -1; + this.gateway = null; + } } module.exports = WebSocketManagerDataStore; diff --git a/src/structures/interface/TextBasedChannel.js b/src/structures/interface/TextBasedChannel.js index a76a855a5..1eb44ca90 100644 --- a/src/structures/interface/TextBasedChannel.js +++ b/src/structures/interface/TextBasedChannel.js @@ -1,15 +1,12 @@ -'use strict'; - -function sendMessage(content, options) { - options = options || {}; - return this.client.rest.methods.SendMessage(this, content, options.tts); +function sendMessage(content, options = {}) { + return this.client.rest.methods.sendMessage(this, content, options.tts); } -function sendTTSMessage(content, options) { - options = options || {}; - return this.client.rest.methods.SendMessage(this, content, true); +function sendTTSMessage(content) { + return this.client.rest.methods.sendMessage(this, content, true); } exports.applyToClass = structure => { - structure.prototype.sendMessage = sendMessage; + structure.prototype.sendMessage = sendMessage; + structure.prototype.sendTTSMessage = sendTTSMessage; }; diff --git a/src/util/CloneObject.js b/src/util/CloneObject.js index 158f9d189..726712fb6 100644 --- a/src/util/CloneObject.js +++ b/src/util/CloneObject.js @@ -1,7 +1,6 @@ -'use strict'; -module.exports = function CloneObject(obj) { - var cloned = Object.create(obj); - Object.assign(cloned, obj); +module.exports = function cloneObject(obj) { + const cloned = Object.create(obj); + Object.assign(cloned, obj); - return cloned; + return cloned; }; diff --git a/src/util/Constants.js b/src/util/Constants.js index daf082231..f958c3a7f 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -1,184 +1,182 @@ -'use strict'; - -const DefaultOptions = exports.DefaultOptions = { - ws: { - large_threshold: 250, - compress: true, - properties: { - $os: process ? process.platform : 'discord.js', - $browser: 'discord.js', - $device: 'discord.js', - $referrer: '', - $referring_domain: '', - }, - }, - protocol_version: 4, - max_message_cache: 200, - rest_ws_bridge_timeout: 5000, +exports.DefaultOptions = { + ws: { + large_threshold: 250, + compress: true, + properties: { + $os: process ? process.platform : 'discord.js', + $browser: 'discord.js', + $device: 'discord.js', + $referrer: '', + $referring_domain: '', + }, + }, + protocol_version: 4, + max_message_cache: 200, + rest_ws_bridge_timeout: 5000, }; -const Status = exports.Status = { - READY: 0, - CONNECTING: 1, - RECONNECTING: 2, - IDLE: 3, +exports.Status = { + READY: 0, + CONNECTING: 1, + RECONNECTING: 2, + IDLE: 3, }; -const Package = exports.Package = require('../../package.json'); +exports.Package = require('../../package.json'); -const Errors = exports.Errors = { - NO_TOKEN: new Error('request to use token, but token was unavailable to the client'), - NO_BOT_ACCOUNT: new Error('you should ideally be using a bot account!'), - BAD_WS_MESSAGE: new Error('a bad message was received from the websocket - bad compression or not json'), - TOOK_TOO_LONG: new Error('something took too long to do'), - NOT_A_PERMISSION: new Error('that is not a valid permission number'), +exports.Errors = { + NO_TOKEN: new Error('request to use token, but token was unavailable to the client'), + NO_BOT_ACCOUNT: new Error('you should ideally be using a bot account!'), + BAD_WS_MESSAGE: new Error('a bad message was received from the websocket - bad compression or not json'), + TOOK_TOO_LONG: new Error('something took too long to do'), + NOT_A_PERMISSION: new Error('that is not a valid permission number'), }; const API = 'https://discordapp.com/api'; const Endpoints = exports.Endpoints = { - // general endpoints - LOGIN: `${API}/auth/login`, - LOGOUT: `${API}/auth/logout`, - ME: `${API}/users/@me`, - ME_GUILD: (guildID) => `${Endpoints.ME}/guilds/${guildID}`, - GATEWAY: `${API}/gateway`, - USER_CHANNELS: (userID) => `${API}/users/${userID}/channels`, - AVATAR: (userID, avatar) => `${API}/users/${userID}/avatars/${avatar}.jpg`, - INVITE: (id) => `${API}/invite/${id}`, + // general endpoints + login: `${API}/auth/login`, + logout: `${API}/auth/logout`, + me: `${API}/users/@me`, + meGuild: (guildID) => `${Endpoints.me}/guilds/${guildID}`, + gateway: `${API}/gateway`, + userChannels: (userID) => `${API}/users/${userID}/channels`, + avatar: (userID, avatar) => `${API}/users/${userID}/avatars/${avatar}.jpg`, + invite: (id) => `${API}/invite/${id}`, - // guilds - GUILDS: `${API}/guilds`, - GUILD: (guildID) => `${Endpoints.GUILDS}/${guildID}`, - GUILD_ICON: (guildID, hash) => `${Endpoints.GUILD(guildID)}/icons/${hash}.jpg`, - GUILD_PRUNE: (guildID) => `${Endpoints.GUILD(guildID)}/prune`, - GUILD_EMBED: (guildID) => `${Endpoints.GUILD(guildID)}/embed`, - GUILD_INVITES: (guildID) => `${Endpoints.GUILD(guildID)}/invites`, - GUILD_ROLES: (guildID) => `${Endpoints.GUILD(guildID)}/roles`, - GUILD_ROLE: (guildID, roleID) => `${Endpoints.GUILD_ROLES(guildID)}/${roleID}`, - GUILD_BANS: (guildID) => `${Endpoints.GUILD(guildID)}/bans`, - GUILD_INTEGRATIONS: (guildID) => `${Endpoints.GUILD(guildID)}/integrations`, - GUILD_MEMBERS: (guildID) => `${Endpoints.GUILD(guildID)}/members`, - GUILD_MEMBER: (guildID, memberID) => `${Endpoints.GUILD_MEMBERS(guildID)}/${memberID}`, - GUILD_CHANNELS: (guildID) => `${Endpoints.GUILD(guildID)}/channels`, + // guilds + guilds: `${API}/guilds`, + guild: (guildID) => `${Endpoints.guilds}/${guildID}`, + guildIcon: (guildID, hash) => `${Endpoints.guild(guildID)}/icons/${hash}.jpg`, + guildPrune: (guildID) => `${Endpoints.guild(guildID)}/prune`, + guildEmbed: (guildID) => `${Endpoints.guild(guildID)}/embed`, + guildInvites: (guildID) => `${Endpoints.guild(guildID)}/invites`, + guildRoles: (guildID) => `${Endpoints.guild(guildID)}/roles`, + guildRole: (guildID, roleID) => `${Endpoints.guildRoles(guildID)}/${roleID}`, + guildBans: (guildID) => `${Endpoints.guild(guildID)}/bans`, + guildIntegrations: (guildID) => `${Endpoints.guild(guildID)}/integrations`, + guildMembers: (guildID) => `${Endpoints.guild(guildID)}/members`, + guildMember: (guildID, memberID) => `${Endpoints.guildMembers(guildID)}/${memberID}`, + guildChannels: (guildID) => `${Endpoints.guild(guildID)}/channels`, - // channels - CHANNELS: `${API}/channels`, - CHANNEL: (channelID) => `${Endpoints.CHANNELS}/${channelID}`, - CHANNEL_MESSAGES: (channelID) => `${Endpoints.CHANNEL(channelID)}/messages`, - CHANNEL_INVITES: (channelID) => `${Endpoints.CHANNEL(channelID)}/invites`, - CHANNEL_TYPING: (channelID) => `${Endpoints.CHANNEL(channelID)}/typing`, - CHANNEL_PERMISSIONS: (channelID) => `${Endpoints.CHANNEL(channelID)}/permissions`, - CHANNEL_MESSAGE: (channelID, messageID) => `${Endpoints.CHANNEL_MESSAGES(channelID)}/${messageID}`, + // channels + channels: `${API}/channels`, + channel: (channelID) => `${Endpoints.channels}/${channelID}`, + channelMessages: (channelID) => `${Endpoints.channel(channelID)}/messages`, + channelInvites: (channelID) => `${Endpoints.channel(channelID)}/invites`, + channelTyping: (channelID) => `${Endpoints.channel(channelID)}/typing`, + channelPermissions: (channelID) => `${Endpoints.channel(channelID)}/permissions`, + channelMessage: (channelID, messageID) => `${Endpoints.channelMessage(channelID)}/${messageID}`, }; -const OPCodes = exports.OPCodes = { - DISPATCH: 0, - HEARTBEAT: 1, - IDENTIFY: 2, - STATUS_UPDATE: 3, - VOICE_STATE_UPDATE: 4, - VOICE_GUILD_PING: 5, - RESUME: 6, - RECONNECT: 7, - REQUEST_GUILD_MEMBERS: 8, - INVALID_SESSION: 9, +exports.OPCodes = { + DISPATCH: 0, + HEARTBEAT: 1, + IDENTIFY: 2, + STATUS_UPDATE: 3, + VOICE_STATE_UPDATE: 4, + VOICE_GUILD_PING: 5, + RESUME: 6, + RECONNECT: 7, + REQUEST_GUILD_MEMBERS: 8, + INVALID_SESSION: 9, }; -const Events = exports.Events = { - READY: 'ready', - GUILD_CREATE: 'guildCreate', - GUILD_DELETE: 'guildDelete', - GUILD_UNAVAILABLE: 'guildUnavailable', - GUILD_AVAILABLE: 'guildAvailable', - GUILD_UPDATE: 'guildUpdate', - GUILD_BAN_ADD: 'guildBanAdd', - GUILD_BAN_REMOVE: 'guildBanRemove', - GUILD_MEMBER_ADD: 'guildMemberAdd', - GUILD_MEMBER_REMOVE: 'guildMemberRemove', - GUILD_MEMBER_ROLES_UPDATE: 'guildMemberRolesUpdate', - GUILD_ROLE_CREATE: 'guildRoleCreate', - GUILD_ROLE_DELETE: 'guildRoleDelete', - GUILD_ROLE_UPDATE: 'guildRoleUpdate', - GUILD_MEMBER_AVAILABLE: 'guildMemberAvailable', - CHANNEL_CREATE: 'channelCreate', - CHANNEL_DELETE: 'channelDelete', - CHANNEL_UPDATE: 'channelUpdate', - PRESENCE_UPDATE: 'presenceUpdate', - USER_UPDATE: 'userUpdate', - VOICE_STATE_UPDATE: 'voiceStateUpdate', - TYPING_START: 'typingStart', - TYPING_STOP: 'typingStop', - WARN: 'warn', - GUILD_MEMBERS_CHUNK: 'guildMembersChunk', - MESSAGE_CREATE: 'message', - MESSAGE_DELETE: 'messageDelete', - MESSAGE_UPDATE: 'messageUpdate', +exports.Events = { + READY: 'ready', + GUILD_CREATE: 'guildCreate', + GUILD_DELETE: 'guildDelete', + GUILD_UNAVAILABLE: 'guildUnavailable', + GUILD_AVAILABLE: 'guildAvailable', + GUILD_UPDATE: 'guildUpdate', + GUILD_BAN_ADD: 'guildBanAdd', + GUILD_BAN_REMOVE: 'guildBanRemove', + GUILD_MEMBER_ADD: 'guildMemberAdd', + GUILD_MEMBER_REMOVE: 'guildMemberRemove', + GUILD_MEMBER_ROLES_UPDATE: 'guildMemberRolesUpdate', + GUILD_ROLE_CREATE: 'guildRoleCreate', + GUILD_ROLE_DELETE: 'guildRoleDelete', + GUILD_ROLE_UPDATE: 'guildRoleUpdate', + GUILD_MEMBER_AVAILABLE: 'guildMemberAvailable', + CHANNEL_CREATE: 'channelCreate', + CHANNEL_DELETE: 'channelDelete', + CHANNEL_UPDATE: 'channelUpdate', + PRESENCE_UPDATE: 'presenceUpdate', + USER_UPDATE: 'userUpdate', + VOICE_STATE_UPDATE: 'voiceStateUpdate', + TYPING_START: 'typingStart', + TYPING_STOP: 'typingStop', + WARN: 'warn', + GUILD_MEMBERS_CHUNK: 'guildMembersChunk', + MESSAGE_CREATE: 'message', + MESSAGE_DELETE: 'messageDelete', + MESSAGE_UPDATE: 'messageUpdate', }; -const WSEvents = exports.WSEvents = { - CHANNEL_CREATE: 'CHANNEL_CREATE', - CHANNEL_DELETE: 'CHANNEL_DELETE', - CHANNEL_UPDATE: 'CHANNEL_UPDATE', - MESSAGE_CREATE: 'MESSAGE_CREATE', - MESSAGE_DELETE: 'MESSAGE_DELETE', - MESSAGE_UPDATE: 'MESSAGE_UPDATE', - PRESENCE_UPDATE: 'PRESENCE_UPDATE', - READY: 'READY', - GUILD_BAN_ADD: 'GUILD_BAN_ADD', - GUILD_BAN_REMOVE: 'GUILD_BAN_REMOVE', - GUILD_CREATE: 'GUILD_CREATE', - GUILD_DELETE: 'GUILD_DELETE', - GUILD_MEMBER_ADD: 'GUILD_MEMBER_ADD', - GUILD_MEMBER_REMOVE: 'GUILD_MEMBER_REMOVE', - GUILD_MEMBER_UPDATE: 'GUILD_MEMBER_UPDATE', - GUILD_MEMBERS_CHUNK: 'GUILD_MEMBERS_CHUNK', - GUILD_ROLE_CREATE: 'GUILD_ROLE_CREATE', - GUILD_ROLE_DELETE: 'GUILD_ROLE_DELETE', - GUILD_ROLE_UPDATE: 'GUILD_ROLE_UPDATE', - GUILD_UPDATE: 'GUILD_UPDATE', - TYPING_START: 'TYPING_START', - USER_UPDATE: 'USER_UPDATE', - VOICE_STATE_UPDATE: 'VOICE_STATE_UPDATE', - FRIEND_ADD: 'RELATIONSHIP_ADD', - FRIEND_REMOVE: 'RELATIONSHIP_REMOVE', +exports.WSEvents = { + CHANNEL_CREATE: 'CHANNEL_CREATE', + CHANNEL_DELETE: 'CHANNEL_DELETE', + CHANNEL_UPDATE: 'CHANNEL_UPDATE', + MESSAGE_CREATE: 'MESSAGE_CREATE', + MESSAGE_DELETE: 'MESSAGE_DELETE', + MESSAGE_UPDATE: 'MESSAGE_UPDATE', + PRESENCE_UPDATE: 'PRESENCE_UPDATE', + READY: 'READY', + GUILD_BAN_ADD: 'GUILD_BAN_ADD', + GUILD_BAN_REMOVE: 'GUILD_BAN_REMOVE', + GUILD_CREATE: 'GUILD_CREATE', + GUILD_DELETE: 'GUILD_DELETE', + GUILD_MEMBER_ADD: 'GUILD_MEMBER_ADD', + GUILD_MEMBER_REMOVE: 'GUILD_MEMBER_REMOVE', + GUILD_MEMBER_UPDATE: 'GUILD_MEMBER_UPDATE', + GUILD_MEMBERS_CHUNK: 'GUILD_MEMBERS_CHUNK', + GUILD_ROLE_CREATE: 'GUILD_ROLE_CREATE', + GUILD_ROLE_DELETE: 'GUILD_ROLE_DELETE', + GUILD_ROLE_UPDATE: 'GUILD_ROLE_UPDATE', + GUILD_UPDATE: 'GUILD_UPDATE', + TYPING_START: 'TYPING_START', + USER_UPDATE: 'USER_UPDATE', + VOICE_STATE_UPDATE: 'VOICE_STATE_UPDATE', + FRIEND_ADD: 'RELATIONSHIP_ADD', + FRIEND_REMOVE: 'RELATIONSHIP_REMOVE', }; const PermissionFlags = exports.PermissionFlags = { - CREATE_INSTANT_INVITE: 1 << 0, - KICK_MEMBERS: 1 << 1, - BAN_MEMBERS: 1 << 2, - ADMINISTRATOR: 1 << 3, - MANAGE_CHANNELS: 1 << 4, - MANAGE_GUILD: 1 << 5, + CREATE_INSTANT_INVITE: 1 << 0, + KICK_MEMBERS: 1 << 1, + BAN_MEMBERS: 1 << 2, + ADMINISTRATOR: 1 << 3, + MANAGE_CHANNELS: 1 << 4, + MANAGE_GUILD: 1 << 5, - READ_MESSAGES: 1 << 10, - SEND_MESSAGES: 1 << 11, - SEND_TTS_MESSAGES: 1 << 12, - MANAGE_MESSAGES: 1 << 13, - EMBED_LINKS: 1 << 14, - ATTACH_FILES: 1 << 15, - READ_MESSAGE_HISTORY: 1 << 16, - MENTION_EVERYONE: 1 << 17, + READ_MESSAGES: 1 << 10, + SEND_MESSAGES: 1 << 11, + SEND_TTS_MESSAGES: 1 << 12, + MANAGE_MESSAGES: 1 << 13, + EMBED_LINKS: 1 << 14, + ATTACH_FILES: 1 << 15, + READ_MESSAGE_HISTORY: 1 << 16, + MENTION_EVERYONE: 1 << 17, - CONNECT: 1 << 20, - SPEAK: 1 << 21, - MUTE_MEMBERS: 1 << 22, - DEAFEN_MEMBERS: 1 << 23, - MOVE_MEMBERS: 1 << 24, - USE_VAD: 1 << 25, + CONNECT: 1 << 20, + SPEAK: 1 << 21, + MUTE_MEMBERS: 1 << 22, + DEAFEN_MEMBERS: 1 << 23, + MOVE_MEMBERS: 1 << 24, + USE_VAD: 1 << 25, - CHANGE_NICKNAME: 1 << 26, - MANAGE_NICKNAMES: 1 << 27, - MANAGE_ROLES_OR_PERMISSIONS: 1 << 28, + CHANGE_NICKNAME: 1 << 26, + MANAGE_NICKNAMES: 1 << 27, + MANAGE_ROLES_OR_PERMISSIONS: 1 << 28, }; let _ALL_PERMISSIONS = 0; -for (let key in PermissionFlags) { - _ALL_PERMISSIONS |= PermissionFlags[key]; +for (const key in PermissionFlags) { + _ALL_PERMISSIONS |= PermissionFlags[key]; } -const ALL_PERMISSIONS = exports.ALL_PERMISSIONS = _ALL_PERMISSIONS; +exports.ALL_PERMISSIONS = _ALL_PERMISSIONS; -const DEFAULT_PERMISSIONS = exports.DEFAULT_PERMISSIONS = 36953089; +exports.DEFAULT_PERMISSIONS = 36953089; diff --git a/src/util/MergeDefault.js b/src/util/MergeDefault.js index 3da11d278..d07044cdc 100644 --- a/src/util/MergeDefault.js +++ b/src/util/MergeDefault.js @@ -1,19 +1,17 @@ -'use strict'; - module.exports = function merge(def, given) { - if (!given) { - return def; - } + if (!given) { + return def; + } - given = given || {}; + given = given || {}; - for (var key in def) { - if (!given.hasOwnProperty(key)) { - given[key] = def[key]; - } else if (given[key] === Object(given[key])) { - given[key] = merge(def[key], given[key]); - } - } + 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; + return given; }; diff --git a/test/random.js b/test/random.js index a1de4025f..54516b2be 100644 --- a/test/random.js +++ b/test/random.js @@ -3,172 +3,172 @@ const Discord = require('../'); const request = require('superagent'); -let client = new Discord.Client(); +const client = new Discord.Client(); client.login(require('./auth.json').token).then(token => console.log('logged in with token ' + token)).catch(console.log); client.on('ready', () => { - console.log('ready!'); + console.log('ready!'); }); client.on('guildCreate', (guild) => { - console.log(guild); + console.log(guild); }); client.on('guildDelete', (guild) => { - console.log('guilddel', guild.name); + console.log('guilddel', guild.name); }); client.on('guildUpdate', (old, guild) => { - console.log('guildupdate', old.name, guild.name); + console.log('guildupdate', old.name, guild.name); }); client.on('channelCreate', channel => { - // console.log(channel); + // console.log(channel); }); client.on('channelDelete', channel => { - console.log('channDel', channel.name); + console.log('channDel', channel.name); }); client.on('channelUpdate', (old, chan) => { - console.log('chan update', old.name, chan.name); + console.log('chan update', old.name, chan.name); }); client.on('guildMemberAdd', (guild, user) => { - console.log('new guild member', user.user.username, 'in', guild.name); + console.log('new guild member', user.user.username, 'in', guild.name); }); client.on('guildMemberRemove', (guild, user) => { - console.log('dead guild member', user.user.username, 'in', guild.name); + console.log('dead guild member', user.user.username, 'in', guild.name); }); client.on('guildRoleCreate', (guild, role) => { - console.log('new role', role.name, 'in', guild.name); - role.edit({ - permissions: ['DEAFEN_MEMBERS'], - name: 'deafen' - }).then(role2 => { - console.log('role replace from ' + role.name + ' to ' + role2.name); - }).catch(console.log) + console.log('new role', role.name, 'in', guild.name); + role.edit({ + permissions: ['DEAFEN_MEMBERS'], + name: 'deafen', + }).then(role2 => { + console.log('role replace from ' + role.name + ' to ' + role2.name); + }).catch(console.log); }); client.on('guildRoleDelete', (guild, role) => { - console.log('dead role', role.name, 'in', guild.name); + console.log('dead role', role.name, 'in', guild.name); }); client.on('guildRoleUpdate', (guild, old, newRole) => { - console.log('updated role', old.name, 'to', newRole.name, 'in', guild.name); + console.log('updated role', old.name, 'to', newRole.name, 'in', guild.name); }); client.on('presenceUpdate', (oldUser, newUser) => { - // console.log('presence from', oldUser.username, 'to', newUser.username); + // console.log('presence from', oldUser.username, 'to', newUser.username); }); client.on('voiceStateUpdate', (oldMember, newMember) => { - console.log('voiceState', oldMember.user.username, oldMember.voiceChannel + '', newMember.voiceChannel + ''); + console.log('voiceState', oldMember.user.username, oldMember.voiceChannel + '', newMember.voiceChannel + ''); }); client.on('typingStart.', (channel, user) => { - if (user.username === 'hydrabolt') - console.log(user.username, 'started typing in', channel.name); + if (user.username === 'hydrabolt') + console.log(user.username, 'started typing in', channel.name); }); client.on('typingStop.', (channel, user, data) => { - if (user.username === 'hydrabolt') - console.log(user.username, 'stopped typing in', channel.name, 'after', data.elapsedTime + 'ms'); + if (user.username === 'hydrabolt') + console.log(user.username, 'stopped typing in', channel.name, 'after', data.elapsedTime + 'ms'); }); client.on('message', message => { - if (true) { - if (message.content === 'makechann') { - if (message.channel.guild) { - message.channel.guild.createChannel('hi', 'text').then(console.log); - } - } + if (true) { + if (message.content === 'makechann') { + if (message.channel.guild) { + message.channel.guild.createChannel('hi', 'text').then(console.log); + } + } - if (message.content === 'myperms?') { - message.channel.sendMessage('Your permissions are:\n' + - JSON.stringify(message.channel.permissionsFor(message.author).serialize(), null, 4)); - } + if (message.content === 'myperms?') { + message.channel.sendMessage('Your permissions are:\n' + + JSON.stringify(message.channel.permissionsFor(message.author).serialize(), null, 4)); + } - if (message.content === 'delchann') { - message.channel.delete().then(chan => console.log('selfDelChann', chan.name)); - } + if (message.content === 'delchann') { + message.channel.delete().then(chan => console.log('selfDelChann', chan.name)); + } - if (message.content.startsWith('setname')) { - message.channel.setName(message.content.substr(8)); - } + if (message.content.startsWith('setname')) { + message.channel.setName(message.content.substr(8)); + } - if (message.content.startsWith('botname')) { - client.user.setUsername(message.content.substr(8)); - } + if (message.content.startsWith('botname')) { + client.user.setUsername(message.content.substr(8)); + } - if (message.content.startsWith('botavatar')) { - request - .get('url') - .end((err, res) => { - client.user.setAvatar(res.body).catch(console.log) - .then(user => message.channel.sendMessage('Done!')); - }); - } + if (message.content.startsWith('botavatar')) { + request + .get('url') + .end((err, res) => { + client.user.setAvatar(res.body).catch(console.log) + .then(user => message.channel.sendMessage('Done!')); + }); + } - if (message.content.startsWith('gn')) { - message.guild.setName(message.content.substr(3)) - .then(guild => console.log('guild updated to', guild.name)) - .catch(console.log); - } + if (message.content.startsWith('gn')) { + message.guild.setName(message.content.substr(3)) + .then(guild => console.log('guild updated to', guild.name)) + .catch(console.log); + } - if (message.content === 'leave') { - message.guild.leave().then(guild => console.log('left guild', guild.name)).catch(console.log); - } + if (message.content === 'leave') { + message.guild.leave().then(guild => console.log('left guild', guild.name)).catch(console.log); + } - if (message.content === 'stats') { - let m = ''; - m += `I am aware of ${message.guild.channels.length} channels\n`; - m += `I am aware of ${message.guild.members.length} members`; - message.channel.sendMessage(m); - } + if (message.content === 'stats') { + let m = ''; + m += `I am aware of ${message.guild.channels.length} channels\n`; + m += `I am aware of ${message.guild.members.length} members`; + message.channel.sendMessage(m); + } - if (message.content === 'messageme!') { - message.author.sendMessage('oh, hi there!').catch(e => console.log(e.stack)); - } + if (message.content === 'messageme!') { + message.author.sendMessage('oh, hi there!').catch(e => console.log(e.stack)); + } - if (message.content === 'don\'t dm me') { - message.author.deleteDM(); - } + if (message.content === 'don\'t dm me') { + message.author.deleteDM(); + } - if (message.content.startsWith('kick')) { - message.guild.member(message.mentions[0]).kick().then(member => { - console.log(member); - message.channel.sendMessage('Kicked!' + member.user.username); - }).catch(console.log); - } + if (message.content.startsWith('kick')) { + message.guild.member(message.mentions[0]).kick().then(member => { + console.log(member); + message.channel.sendMessage('Kicked!' + member.user.username); + }).catch(console.log); + } - if (message.content === 'makerole') { - message.guild.createRole().then(role => { - message.channel.sendMessage(`Made role ${role.name}`); - }).catch(console.log); - } - } + if (message.content === 'makerole') { + message.guild.createRole().then(role => { + message.channel.sendMessage(`Made role ${role.name}`); + }).catch(console.log); + } + } }); function nameLoop(user) { - // user.setUsername(user.username + 'a').then(nameLoop).catch(console.log); + // user.setUsername(user.username + 'a').then(nameLoop).catch(console.log); } function chanLoop(channel) { - channel.setName(channel.name + 'a').then(chanLoop).catch(console.log); + channel.setName(channel.name + 'a').then(chanLoop).catch(console.log); } client.on('messageDelete', message => { - console.log('Message deleted by', message.author.username); + console.log('Message deleted by', message.author.username); }); client.on('messageUpdate', (old, message) => { - if (message.author.username === 'hydrabolt') - console.log('Message updated from', old.content, 'to', message.content); + if (message.author.username === 'hydrabolt') + console.log('Message updated from', old.content, 'to', message.content); }); client.on('message', message => { - if (message.content === '?perms?') { - console.log(message.author.username, 'asked for perms in', message.channel.name, ':'); - console.log(message.channel.permissionsFor(message.author).serialize()); - } + if (message.content === '?perms?') { + console.log(message.author.username, 'asked for perms in', message.channel.name, ':'); + console.log(message.channel.permissionsFor(message.author).serialize()); + } });