diff --git a/lib/Client.js b/lib/Client.js index 80b9a925f..71124b2ce 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -148,7 +148,7 @@ var Client = (function () { self.state = 2; //set state to logged in (not yet ready) self.token = res.body.token; //set our token - getGateway().then(function (url) { + self.getGateway().then(function (url) { self.createws(url); callback(null, self.token); resolve(self.token); @@ -337,7 +337,7 @@ var Client = (function () { } var user = destination.sender; - self.sendMessage(destination, message, callback, user + ", ").then(response)["catch"](reject); + self.sendMessage(destination, message, tts, callback, user + ", ").then(response)["catch"](reject); }); } }, { @@ -1600,6 +1600,20 @@ var Client = (function () { })(); } } + }, { + key: "getGateway", + value: function getGateway() { + var self = this; + return new Promise(function (resolve, reject) { + request.get(Endpoints.API + "/gateway").set("authorization", self.token).end(function (err, res) { + if (err) { + reject(err); + } else { + resolve(res.body.url); + } + }); + }); + } }, { key: "uptime", get: function get() { @@ -1668,19 +1682,4 @@ var Client = (function () { return Client; })(); -function getGateway() { - - var self = this; - - return new Promise(function (resolve, reject) { - request.get(Endpoints.API + "/gateway").end(function (err, res) { - if (err) { - reject(err); - } else { - resolve(res.body.url); - } - }); - }); -} - module.exports = Client; \ No newline at end of file diff --git a/lib/channel.js b/lib/channel.js index dfb4f72d8..79bc71b55 100644 --- a/lib/channel.js +++ b/lib/channel.js @@ -64,7 +64,7 @@ var Channel = (function () { }, { key: "toString", value: function toString() { - return "#" + this.name; + return "<#" + this.id + ">"; } }, { key: "client", diff --git a/lib/server.js b/lib/server.js index 5bf3beca1..a5c90170d 100644 --- a/lib/server.js +++ b/lib/server.js @@ -165,6 +165,11 @@ var Server = (function () { get: function get() { return this.client.getUser("id", this.ownerID); } + }, { + key: "users", + get: function get() { + return this.members; + } }]); return Server; diff --git a/package.json b/package.json index 4af3dbde8..55f98889f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discord.js", - "version": "3.3.2", + "version": "3.3.3", "description": "A way to interface with the Discord API", "main": "./lib/index.js", "scripts": { diff --git a/src/Client.js b/src/Client.js index 6421121e6..89486be44 100644 --- a/src/Client.js +++ b/src/Client.js @@ -153,7 +153,7 @@ class Client { if (err) { self.state = 4; //set state to disconnected self.trigger("disconnected"); - if(self.websocket){ + if (self.websocket) { self.websocket.close(); } callback(err); @@ -162,7 +162,7 @@ class Client { self.state = 2; //set state to logged in (not yet ready) self.token = res.body.token; //set our token - getGateway().then(function (url) { + self.getGateway().then(function (url) { self.createws(url); callback(null, self.token); resolve(self.token); @@ -367,14 +367,14 @@ class Client { return new Promise(function (response, reject) { - if(typeof tts === "function"){ + if (typeof tts === "function") { // tts is a function, which means the developer wants this to be the callback callback = tts; tts = false; } var user = destination.sender; - self.sendMessage(destination, message, callback, user + ", ").then(response).catch(reject); + self.sendMessage(destination, message, tts, callback, user + ", ").then(response).catch(reject); }); @@ -383,7 +383,7 @@ class Client { deleteMessage(message, timeout, callback = function (err, msg) { }) { var self = this; - + var prom = new Promise(function (resolve, reject) { if (timeout) { setTimeout(remove, timeout) @@ -392,7 +392,7 @@ class Client { } function remove() { - if(self.options.queue){ + if (self.options.queue) { if (!self.queue[message.channel.id]) { self.queue[message.channel.id] = []; } @@ -402,26 +402,26 @@ class Client { then: good, error: bad }); - + self.checkQueue(message.channel.id); - }else{ + } else { self._deleteMessage(message).then(good).catch(bad); } } - - function good(){ + + function good() { prom.success = true; callback(null); resolve(); } - - function bad(err){ + + function bad(err) { prom.error = err; callback(err); reject(err); } }); - + return prom; } @@ -433,7 +433,7 @@ class Client { content = (content instanceof Array ? content.join("\n") : content); - if(self.options.queue){ + if (self.options.queue) { if (!self.queue[message.channel.id]) { self.queue[message.channel.id] = []; } @@ -444,26 +444,26 @@ class Client { then: good, error: bad }); - + self.checkQueue(message.channel.id); - }else{ + } else { self._updateMessage(message, content).then(good).catch(bad); } - - function good(msg){ - prom.message = msg; - callback(null, msg); - resolve(msg); - } - - function bad(error){ - prom.error = error; - callback(error); - reject(error); - } + + function good(msg) { + prom.message = msg; + callback(null, msg); + resolve(msg); + } + + function bad(error) { + prom.error = error; + callback(error); + reject(error); + } }); - + return prom; } @@ -611,7 +611,7 @@ class Client { self.resolveDestination(destination).then(send).catch(bad); function send(destination) { - if(self.options.queue){ + if (self.options.queue) { //queue send file too if (!self.queue[destination]) { self.queue[destination] = []; @@ -619,14 +619,14 @@ class Client { self.queue[destination].push({ action: "sendFile", - attachment : fstream, - attachmentName : fileName, + attachment: fstream, + attachmentName: fileName, then: good, error: bad }); self.checkQueue(destination); - }else{ + } else { //not queue self._sendFile(destination, fstream, fileName).then(good).catch(bad); } @@ -645,7 +645,7 @@ class Client { } }); - + return prom; } @@ -655,13 +655,13 @@ class Client { var self = this; var prom = new Promise(function (resolve, reject) { - - if(typeof tts === "function"){ + + if (typeof tts === "function") { // tts is a function, which means the developer wants this to be the callback callback = tts; tts = false; } - + message = premessage + resolveMessage(message); var mentions = resolveMentions(); self.resolveDestination(destination).then(send).catch(error); @@ -682,7 +682,7 @@ class Client { action: "sendMessage", content: message, mentions: mentions, - tts : !!tts, //incase it's not a boolean + tts: !!tts, //incase it's not a boolean then: mgood, error: mbad }); @@ -723,7 +723,7 @@ class Client { } }); - + return prom; } @@ -1179,7 +1179,7 @@ class Client { .send({ content: content, mentions: mentions, - tts : tts + tts: tts }) .end(function (err, res) { @@ -1207,39 +1207,39 @@ class Client { }); } - - _sendFile(destination, attachment, attachmentName = "DEFAULT BECAUSE YOU DIDN'T SPECIFY WHY.png"){ - + + _sendFile(destination, attachment, attachmentName = "DEFAULT BECAUSE YOU DIDN'T SPECIFY WHY.png") { + var self = this; - - return new Promise(function(resolve, reject){ - request - .post(`${Endpoints.CHANNELS}/${destination}/messages`) - .set("authorization", self.token) - .attach("file", attachment, attachmentName) - .end(function (err, res) { - if (err) { - reject(err); - } else { - - var chann = self.getChannel("id", destination); - if (chann) { - var msg = chann.addMessage(new Message(res.body, chann, [], self.user)); - resolve(msg); - } + return new Promise(function (resolve, reject) { + request + .post(`${Endpoints.CHANNELS}/${destination}/messages`) + .set("authorization", self.token) + .attach("file", attachment, attachmentName) + .end(function (err, res) { + if (err) { + reject(err); + } else { + var chann = self.getChannel("id", destination); + if (chann) { + var msg = chann.addMessage(new Message(res.body, chann, [], self.user)); + resolve(msg); } - }); + + } + + }); }); - + } - - _updateMessage(message, content){ + + _updateMessage(message, content) { var self = this; - return new Promise(function(resolve, reject){ + return new Promise(function (resolve, reject) { request .patch(`${Endpoints.CHANNELS}/${message.channel.id}/messages/${message.id}`) .set("authorization", self.token) @@ -1258,28 +1258,28 @@ class Client { }); }); } - - _deleteMessage(message){ + + _deleteMessage(message) { var self = this; - return new Promise(function(resolve, reject){ - + return new Promise(function (resolve, reject) { + request - .del(`${Endpoints.CHANNELS}/${message.channel.id}/messages/${message.id}`) - .set("authorization", self.token) - .end(function (err, res) { - if (err) { - reject(err); - } else { - resolve(); - } - }); + .del(`${Endpoints.CHANNELS}/${message.channel.id}/messages/${message.id}`) + .set("authorization", self.token) + .end(function (err, res) { + if (err) { + reject(err); + } else { + resolve(); + } + }); }); } checkQueue(channelID) { var self = this; - + if (!this.checkingQueue[channelID]) { //if we aren't already checking this queue. this.checkingQueue[channelID] = true; @@ -1309,12 +1309,12 @@ class Client { case "sendFile": var fileToSend = queuedEvent; self._sendFile(channelID, fileToSend.attachment, fileToSend.attachmentName) - .then(function (msg){ + .then(function (msg) { fileToSend.then(msg); self.queue[channelID].shift(); doNext(); }) - .catch(function(err){ + .catch(function (err) { fileToSend.error(err); self.queue[channelID].shift(); doNext(); @@ -1323,30 +1323,30 @@ class Client { case "updateMessage": var msgToUpd = queuedEvent; self._updateMessage(msgToUpd.message, msgToUpd.content) - .then(function(msg){ - msgToUpd.then(msg); - self.queue[channelID].shift(); - doNext(); - }) - .catch(function(err){ - msgToUpd.error(err); - self.queue[channelID].shift(); - doNext(); - }); + .then(function (msg) { + msgToUpd.then(msg); + self.queue[channelID].shift(); + doNext(); + }) + .catch(function (err) { + msgToUpd.error(err); + self.queue[channelID].shift(); + doNext(); + }); break; case "deleteMessage": var msgToDel = queuedEvent; self._deleteMessage(msgToDel.message) - .then(function(msg){ - msgToDel.then(msg); - self.queue[channelID].shift(); - doNext(); - }) - .catch(function(err){ - msgToDel.error(err); - self.queue[channelID].shift(); - doNext(); - }); + .then(function (msg) { + msgToDel.then(msg); + self.queue[channelID].shift(); + doNext(); + }) + .catch(function (err) { + msgToDel.error(err); + self.queue[channelID].shift(); + doNext(); + }); break; default: done(); @@ -1360,24 +1360,22 @@ class Client { } } } -} - -function getGateway() { - - var self = this; - - return new Promise(function (resolve, reject) { - request - .get(`${Endpoints.API}/gateway`) - .end(function (err, res) { - if (err) { - reject(err); - } else { - resolve(res.body.url); - } - }); - }); + getGateway() { + var self = this; + return new Promise(function (resolve, reject) { + request + .get(`${Endpoints.API}/gateway`) + .set("authorization", self.token) + .end(function (err, res) { + if (err) { + reject(err); + } else { + resolve(res.body.url); + } + }); + }); + } } module.exports = Client; \ No newline at end of file diff --git a/src/channel.js b/src/channel.js index 0dc0b957a..d813e9b74 100644 --- a/src/channel.js +++ b/src/channel.js @@ -34,7 +34,7 @@ class Channel { } toString(){ - return "#" + this.name; + return "<#" + this.id + ">"; } get isPrivate(){ diff --git a/src/server.js b/src/server.js index 248a04683..c0f24af27 100644 --- a/src/server.js +++ b/src/server.js @@ -50,6 +50,10 @@ class Server { return this.client.getUser("id", this.ownerID); } + get users() { + return this.members; + } + // get/set getChannel(key, value) { for (var channel of this.channels) { diff --git a/test/bot.1.js b/test/bot.1.js index f99314baa..38f25d390 100644 --- a/test/bot.1.js +++ b/test/bot.1.js @@ -17,7 +17,7 @@ mybot.on("message", function (message) { } // we can go ahead :) - mybot.sendMessage(message.author, message.sender.username); + mybot.reply(message, message.channel); }); mybot.on("ready", function () {