From ac2398e47c860eb22d3bdb0e2c551175fd55ba41 Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Fri, 4 Dec 2015 20:30:11 +0000 Subject: [PATCH] started working on bug fixes and test scripts --- .eslintrc | 6 + lib/Client/Client.js | 2 +- lib/Client/ConnectionState.js | 2 +- lib/Client/InternalClient.js | 9 +- lib/Client/Resolver/Resolver.js | 2 +- lib/Constants.js | 2 +- lib/Structures/Channel.js | 2 +- lib/Structures/ChannelPermissions.js | 2 +- lib/Structures/Invite.js | 2 +- lib/Structures/Message.js | 2 +- lib/Structures/PMChannel.js | 2 +- lib/Structures/PermissionOverwrite.js | 2 +- lib/Structures/Role.js | 2 +- lib/Structures/Server.js | 2 +- lib/Structures/ServerChannel.js | 2 +- lib/Structures/TextChannel.js | 2 +- lib/Structures/User.js | 2 +- lib/Structures/VoiceChannel.js | 2 +- lib/Util/ArgumentRegulariser.js | 2 +- lib/Util/Cache.js | 6 +- lib/Util/Equality.js | 2 +- lib/Voice/AudioEncoder.js | 2 +- lib/Voice/StreamIntent.js | 2 +- lib/Voice/VoiceConnection.js | 2 +- lib/Voice/VoicePacket.js | 2 +- lib/index.js | 2 +- package.json | 8 +- src/Client/InternalClient.js | 7 +- src/Util/Cache.js | 4 +- test/lib-test.js | 163 ++++++++++++++++++++++++++ 30 files changed, 209 insertions(+), 40 deletions(-) create mode 100644 .eslintrc create mode 100644 test/lib-test.js diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 000000000..02dafaa06 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,6 @@ +{ + "env": { + "es6": true, + "node": true + } +} \ No newline at end of file diff --git a/lib/Client/Client.js b/lib/Client/Client.js index 030b972c5..01bae25d4 100644 --- a/lib/Client/Client.js +++ b/lib/Client/Client.js @@ -622,4 +622,4 @@ var Client = (function (_EventEmitter) { })(_events2["default"]); exports["default"] = Client; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Client/ConnectionState.js b/lib/Client/ConnectionState.js index b17e17a5e..e876542d8 100644 --- a/lib/Client/ConnectionState.js +++ b/lib/Client/ConnectionState.js @@ -8,4 +8,4 @@ exports["default"] = { READY: 3, DISCONNECTED: 4 }; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index 1a1329c07..ab70d325a 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -242,7 +242,7 @@ var InternalClient = (function () { return _superagent2["default"].post(_Constants.Endpoints.SERVERS).set("authorization", this.token).send({ name: name, region: region }).end().then(function (res) { // valid server, wait until it is cached return waitFor(function () { - return _this4.servers.get("id", res.body.guild.id); + return _this4.servers.get("id", res.body.id); }); }); }; @@ -521,8 +521,9 @@ var InternalClient = (function () { var channel; if (res.body.type === "text") { channel = new _StructuresTextChannel2["default"](res.body, _this16.client, server); + } else { + channel = new _StructuresVoiceChannel2["default"](res.body, _this16.client, server); } - channel = new _StructuresVoiceChannel2["default"](res.body, _this16.client, server); return server.channels.add(_this16.channels.add(channel)); }); }; @@ -1011,7 +1012,6 @@ var InternalClient = (function () { }; this.websocket.onmessage = function (e) { - if (e.type === "Binary") { if (!zlib) zlib = require("zlib"); e.data = zlib.inflateSync(e.data).toString(); @@ -1027,7 +1027,6 @@ var InternalClient = (function () { } client.emit("raw", packet); - switch (packet.t) { case _Constants.PacketType.READY: @@ -1416,4 +1415,4 @@ var InternalClient = (function () { })(); exports["default"] = InternalClient; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Client/Resolver/Resolver.js b/lib/Client/Resolver/Resolver.js index b05ac0363..c602e4949 100644 --- a/lib/Client/Resolver/Resolver.js +++ b/lib/Client/Resolver/Resolver.js @@ -245,4 +245,4 @@ var Resolver = (function () { })(); exports["default"] = Resolver; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Constants.js b/lib/Constants.js index 13b465ae1..3381d3c5b 100644 --- a/lib/Constants.js +++ b/lib/Constants.js @@ -127,4 +127,4 @@ var PacketType = { SERVER_BAN_ADD: "GUILD_BAN_ADD", SERVER_BAN_REMOVE: "GUILD_BAN_REMOVE" }; -exports.PacketType = PacketType; +exports.PacketType = PacketType; \ No newline at end of file diff --git a/lib/Structures/Channel.js b/lib/Structures/Channel.js index c573a3a10..887420059 100644 --- a/lib/Structures/Channel.js +++ b/lib/Structures/Channel.js @@ -42,4 +42,4 @@ var Channel = (function (_Equality) { })(_UtilEquality2["default"]); exports["default"] = Channel; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Structures/ChannelPermissions.js b/lib/Structures/ChannelPermissions.js index 81773a75e..8f2fccab3 100644 --- a/lib/Structures/ChannelPermissions.js +++ b/lib/Structures/ChannelPermissions.js @@ -75,4 +75,4 @@ var ChannelPermissions = (function () { })(); exports["default"] = ChannelPermissions; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Structures/Invite.js b/lib/Structures/Invite.js index 27d470b9c..12d349cae 100644 --- a/lib/Structures/Invite.js +++ b/lib/Structures/Invite.js @@ -29,4 +29,4 @@ var Invite = (function () { })(); exports["default"] = Invite; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Structures/Message.js b/lib/Structures/Message.js index 7e0942514..475815b10 100644 --- a/lib/Structures/Message.js +++ b/lib/Structures/Message.js @@ -98,4 +98,4 @@ var Message = (function (_Equality) { })(_UtilEquality2["default"]); exports["default"] = Message; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Structures/PMChannel.js b/lib/Structures/PMChannel.js index 61a77e465..03bf59251 100644 --- a/lib/Structures/PMChannel.js +++ b/lib/Structures/PMChannel.js @@ -63,4 +63,4 @@ var PMChannel = (function (_Channel) { })(_Channel3["default"]); exports["default"] = PMChannel; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Structures/PermissionOverwrite.js b/lib/Structures/PermissionOverwrite.js index 6682c168a..ac539d4d5 100644 --- a/lib/Structures/PermissionOverwrite.js +++ b/lib/Structures/PermissionOverwrite.js @@ -86,4 +86,4 @@ var PermissionOverwrite = (function () { })(); exports["default"] = PermissionOverwrite; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Structures/Role.js b/lib/Structures/Role.js index 1aa4ce25b..a9c796639 100644 --- a/lib/Structures/Role.js +++ b/lib/Structures/Role.js @@ -139,4 +139,4 @@ var Role = (function () { })(); exports["default"] = Role; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Structures/Server.js b/lib/Structures/Server.js index 39eb03ef2..a1c1ed602 100644 --- a/lib/Structures/Server.js +++ b/lib/Structures/Server.js @@ -189,4 +189,4 @@ var Server = (function (_Equality) { })(_UtilEquality2["default"]); exports["default"] = Server; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Structures/ServerChannel.js b/lib/Structures/ServerChannel.js index 57ad3f21f..333f09f4e 100644 --- a/lib/Structures/ServerChannel.js +++ b/lib/Structures/ServerChannel.js @@ -131,4 +131,4 @@ var ServerChannel = (function (_Channel) { })(_Channel3["default"]); exports["default"] = ServerChannel; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Structures/TextChannel.js b/lib/Structures/TextChannel.js index 92fa7566f..e0b059b3f 100644 --- a/lib/Structures/TextChannel.js +++ b/lib/Structures/TextChannel.js @@ -66,4 +66,4 @@ var TextChannel = (function (_ServerChannel) { })(_ServerChannel3["default"]); exports["default"] = TextChannel; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Structures/User.js b/lib/Structures/User.js index 259da1efc..6f1c50f61 100644 --- a/lib/Structures/User.js +++ b/lib/Structures/User.js @@ -67,4 +67,4 @@ var User = (function (_Equality) { })(_UtilEquality2["default"]); exports["default"] = User; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Structures/VoiceChannel.js b/lib/Structures/VoiceChannel.js index f73bbfade..7dd54aa5c 100644 --- a/lib/Structures/VoiceChannel.js +++ b/lib/Structures/VoiceChannel.js @@ -25,4 +25,4 @@ var VoiceChannel = (function (_ServerChannel) { })(_ServerChannel3["default"]); exports["default"] = VoiceChannel; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Util/ArgumentRegulariser.js b/lib/Util/ArgumentRegulariser.js index cee9d5a57..e51909e5f 100644 --- a/lib/Util/ArgumentRegulariser.js +++ b/lib/Util/ArgumentRegulariser.js @@ -5,4 +5,4 @@ exports.reg = reg; function reg(c, a) { return [c].concat(Array.prototype.slice.call(a)); -} +} \ No newline at end of file diff --git a/lib/Util/Cache.js b/lib/Util/Cache.js index 61f4ab171..f3b22a70f 100644 --- a/lib/Util/Cache.js +++ b/lib/Util/Cache.js @@ -27,8 +27,8 @@ var Cache = (function (_Array) { return found; }; - Cache.prototype.has = function has(key, value) { - return !!this.get(key, value); + Cache.prototype.has = function has(object) { + return !!this.get(this.discrim, object[this.discrim]); }; Cache.prototype.getAll = function getAll(key, value) { @@ -106,4 +106,4 @@ var Cache = (function (_Array) { })(Array); exports["default"] = Cache; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Util/Equality.js b/lib/Util/Equality.js index aab165350..2963f1d51 100644 --- a/lib/Util/Equality.js +++ b/lib/Util/Equality.js @@ -45,4 +45,4 @@ var Equality = (function () { })(); exports["default"] = Equality; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Voice/AudioEncoder.js b/lib/Voice/AudioEncoder.js index 4b8489b9b..6aecca898 100644 --- a/lib/Voice/AudioEncoder.js +++ b/lib/Voice/AudioEncoder.js @@ -132,4 +132,4 @@ var AudioEncoder = (function () { })(); exports["default"] = AudioEncoder; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Voice/StreamIntent.js b/lib/Voice/StreamIntent.js index 37b981a14..e3fda0e83 100644 --- a/lib/Voice/StreamIntent.js +++ b/lib/Voice/StreamIntent.js @@ -25,4 +25,4 @@ var StreamIntent = (function (_EventEmitter) { })(_events2["default"]); exports["default"] = StreamIntent; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Voice/VoiceConnection.js b/lib/Voice/VoiceConnection.js index ac43034ea..886f2c1d4 100644 --- a/lib/Voice/VoiceConnection.js +++ b/lib/Voice/VoiceConnection.js @@ -363,4 +363,4 @@ var VoiceConnection = (function (_EventEmitter) { })(_events2["default"]); exports["default"] = VoiceConnection; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/Voice/VoicePacket.js b/lib/Voice/VoicePacket.js index 445f9d6c4..71b9fbaaf 100644 --- a/lib/Voice/VoicePacket.js +++ b/lib/Voice/VoicePacket.js @@ -26,4 +26,4 @@ var VoicePacket = function VoicePacket(data, sequence, time, ssrc) { }; exports["default"] = VoicePacket; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index f5ef732bc..3029911ad 100644 --- a/lib/index.js +++ b/lib/index.js @@ -76,4 +76,4 @@ exports["default"] = { VoiceChannel: _StructuresVoiceChannel2["default"], Constants: _Constants2["default"] }; -module.exports = exports["default"]; +module.exports = exports["default"]; \ No newline at end of file diff --git a/package.json b/package.json index 5e0f353f8..2d751327f 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "discord.js", - "version": "5.0.1", + "version": "5.0.2", "description": "A way to interface with the Discord API", "main": "./entrypoint.js", "scripts": { - "test": "node ./test/bot.js" + "test": "eslint *.js lib test && node test/lib-test.js" }, "repository": { "type": "git", @@ -31,11 +31,13 @@ "devDependencies": { "babel-preset-es2015": "^6.1.18", "babel-preset-es2015-loose": "^6.1.2", + "colors": "^1.1.2", "grunt": "~0.4.5", "grunt-babel": "<6.0.0", "grunt-browserify": "^4.0.0", "grunt-contrib-uglify": "^0.9.2", - "load-grunt-tasks": "^3.2.0" + "load-grunt-tasks": "^3.2.0", + "mocha": "^2.3.4" }, "optionalDependencies": { "node-opus": "^0.1.11" diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index 75a358359..9656febb1 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -174,7 +174,7 @@ export default class InternalClient { .end() .then(res => { // valid server, wait until it is cached - return waitFor(() => this.servers.get("id", res.body.guild.id)); + return waitFor(() => this.servers.get("id", res.body.id)); }); } @@ -447,8 +447,9 @@ export default class InternalClient { var channel; if (res.body.type === "text") { channel = new TextChannel(res.body, this.client, server); + }else{ + channel = new VoiceChannel(res.body, this.client, server); } - channel = new VoiceChannel(res.body, this.client, server); return server.channels.add(this.channels.add(channel)); }); } @@ -941,7 +942,6 @@ export default class InternalClient { }; this.websocket.onmessage = e => { - if (e.type === "Binary") { if (!zlib) zlib = require("zlib"); e.data = zlib.inflateSync(e.data).toString(); @@ -957,7 +957,6 @@ export default class InternalClient { } client.emit("raw", packet); - switch (packet.t) { case PacketType.READY: diff --git a/src/Util/Cache.js b/src/Util/Cache.js index 236613a13..fedbc3990 100644 --- a/src/Util/Cache.js +++ b/src/Util/Cache.js @@ -17,8 +17,8 @@ export default class Cache extends Array { return found; } - has(key, value) { - return !!this.get(key, value); + has(object) { + return !!this.get(this.discrim, object[this.discrim]); } getAll(key, value) { diff --git a/test/lib-test.js b/test/lib-test.js new file mode 100644 index 000000000..10fb80ff8 --- /dev/null +++ b/test/lib-test.js @@ -0,0 +1,163 @@ +/* global describe */ +/* global process */ + +var Discord = require("../"); +var client = new Discord.Client(); +var colors = require("colors"); + +var passes = 0, fails = 0; + +function section(s) { + console.log("\n " + s.yellow); +} + +function pass(msg) { + console.log(" ✓ ".green + msg); +} + +function err(msg) { + console.log(" ✗ ".red + msg); +} + +console.log("Beginning Lib Test".yellow); + +section("Logging In"); + +client.on("ready", () => { + pass("received ready"); + + makeServer(); +}) + +client.login(process.env["ds_email"], process.env["ds_password"]).then(token => { + + if (!token || token.length < 1) { + err("bad token"); + return; + } + + pass("valid login token"); + +}).catch(e => err("error logging in: " + e)); + +var server, channel, message; + +function makeServer() { + + section("Server Creation"); + client.createServer("test", "london").then(srv => { + + if (!srv) { + err("server not passed"); + return; + } + + if (srv.name !== "test" || srv.region !== "london") { + err("invalid server name/region"); + return; + } + + if (!srv.members.has(client.user)) { + console.log(srv.members); + err("client not included in members list"); + return; + } + + pass("created server"); + pass("valid server name & region"); + pass("client included in server members"); + + server = srv; + + makeChannel(); + + }).catch(e => { + err("couldn't create server: " + e); + }); +} + +function makeChannel() { + section("Channel Creation"); + + client.createChannel(server, "testchannel", "text").then(chann => { + + if (!chann) { + err("channel not passed"); + return; + } + + if (chann.name !== "testchannel" || chann.type !== "text") { + err("invalid channel name/type"); + return; + } + + pass("channel created"); + pass("valid channel name & type"); + + channel = chann; + + editChannel(); + + }); +} + +function editChannel() { + section("Channel Editting"); + + client.setChannelNameAndTopic(channel, "testing", "a testing channel - temporary").then(() => { + + if (channel.name !== "testing" || channel.topic !== "a testing channel - temporary") { + err("channel not updated"); + return; + } + + pass("channel name and topic updated"); + + sendMsg(); + + }).catch(e => { + err("error editting channel: " + e); + }); +} + +function sendMsg() { + section("Message Creation"); + + client.sendMessage(channel, "test message!", { tts: true }).then(msg => { + + if (!msg) { + err("message not passed"); + return; + } + + if (msg.content !== "test message!" || !msg.sender.equals(client.user) || !msg.channel.equals(channel)) { + err("invalid content, sender or channel assigned to message"); + return; + } + + if (!msg.tts) { + err("message wasn't TTS on response, even though on request it was"); + return; + } + + pass("message successfully created"); + pass("correct parameters in message"); + + message = msg; + + editMsg(); + + }).catch(e => { + err("error sending message: " + e) + }); +} + +function editMsg() { + + section("Message Manipulation"); + + client.updateMessage(message, client.user + channel + server).then(msg => { + + }); + +} \ No newline at end of file