From 35781789419cfd87f48a2f0273bfa3eb3a607bc4 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Sat, 10 Oct 2015 11:43:31 +0100 Subject: [PATCH] Fixed PM Channel bug, 3.8.2 Clients no longer crashed when receiving PM channel messages. --- lib/PMChannel.js | 1 + lib/channel.js | 40 ++++++++++++++++++++-------------------- lib/message.js | 8 +++++++- package.json | 2 +- src/PMChannel.js | 1 + src/channel.js | 9 ++++----- src/message.js | 8 +++++++- test/bot.1.js | 8 ++------ 8 files changed, 43 insertions(+), 34 deletions(-) diff --git a/lib/PMChannel.js b/lib/PMChannel.js index 0dbc0405e..f777d342c 100644 --- a/lib/PMChannel.js +++ b/lib/PMChannel.js @@ -11,6 +11,7 @@ var PMChannel = (function () { this.user = client.getUser("id", data.recipient.id); this.id = data.id; this.messages = []; + this.client = client; } _createClass(PMChannel, [{ diff --git a/lib/channel.js b/lib/channel.js index fbaa8ce4c..4ff1e673c 100644 --- a/lib/channel.js +++ b/lib/channel.js @@ -18,32 +18,32 @@ var Channel = (function () { this.messages = []; this.roles = []; - var _iteratorNormalCompletion = true; - var _didIteratorError = false; - var _iteratorError = undefined; + if (data.permission_overwrites) { + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; - try { - for (var _iterator = data.permission_overwrites[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { - var role = _step.value; - - this.roles.push(new ChannelPermissions(role, this)); - } - - //this.isPrivate = isPrivate; //not sure about the implementation of this... - } catch (err) { - _didIteratorError = true; - _iteratorError = err; - } finally { try { - if (!_iteratorNormalCompletion && _iterator["return"]) { - _iterator["return"](); + for (var _iterator = data.permission_overwrites[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var role = _step.value; + + this.roles.push(new ChannelPermissions(role, this)); } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; } finally { - if (_didIteratorError) { - throw _iteratorError; + try { + if (!_iteratorNormalCompletion && _iterator["return"]) { + _iterator["return"](); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } } } - } + } //this.isPrivate = isPrivate; //not sure about the implementation of this... } _createClass(Channel, [{ diff --git a/lib/message.js b/lib/message.js index 2e2ddd69d..a156acc81 100644 --- a/lib/message.js +++ b/lib/message.js @@ -20,7 +20,13 @@ var Message = (function () { this.editedTimestamp = data.edited_timestamp; this.content = data.content.trim(); this.channel = channel; - this.author = this.channel.server.getMember("id", author.id); + + if (this.isPrivate) { + this.author = this.channel.client.getUser("id", author.id); + } else { + this.author = this.channel.server.getMember("id", author.id) || this.channel.client.getUser("id", author.id); + } + this.attachments = data.attachments; } diff --git a/package.json b/package.json index b7cd03391..16c7552a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discord.js", - "version": "3.8.1", + "version": "3.8.2", "description": "A way to interface with the Discord API", "main": "./lib/index.js", "scripts": { diff --git a/src/PMChannel.js b/src/PMChannel.js index d7985f9c4..d2814afe5 100644 --- a/src/PMChannel.js +++ b/src/PMChannel.js @@ -3,6 +3,7 @@ class PMChannel { this.user = client.getUser("id", data.recipient.id); this.id = data.id; this.messages = []; + this.client = client; } addMessage(data){ diff --git a/src/channel.js b/src/channel.js index 6e3b2ef99..e5bef86e2 100644 --- a/src/channel.js +++ b/src/channel.js @@ -11,11 +11,10 @@ class Channel { this.messages = []; this.roles = []; - for (var role of data.permission_overwrites) { - - this.roles.push( new ChannelPermissions(role, this) ); - - } + if(data.permission_overwrites) + for (var role of data.permission_overwrites) { + this.roles.push( new ChannelPermissions(role, this) ); + } //this.isPrivate = isPrivate; //not sure about the implementation of this... } diff --git a/src/message.js b/src/message.js index 1cbc8be7e..991ae7677 100644 --- a/src/message.js +++ b/src/message.js @@ -12,7 +12,13 @@ class Message{ this.editedTimestamp = data.edited_timestamp; this.content = data.content.trim(); this.channel = channel; - this.author = this.channel.server.getMember("id", author.id); + + if(this.isPrivate){ + this.author = this.channel.client.getUser("id", author.id); + }else{ + this.author = this.channel.server.getMember("id", author.id) || this.channel.client.getUser("id", author.id); + } + this.attachments = data.attachments; } diff --git a/test/bot.1.js b/test/bot.1.js index 55c57651a..97ca23558 100644 --- a/test/bot.1.js +++ b/test/bot.1.js @@ -22,13 +22,9 @@ mybot.on("message", function (message) { user = message.sender; } - console.log("the ID is ", user.id); + console.log( mybot.getUser("username", "meew0") ); - for(key in message.channel.permissionsOf(user)){ - console.log(key); - } - - mybot.reply(message, user + "'s evaluated permissions in this channel are " + message.channel.permissionsOf(user).sendTTSMessages); + mybot.reply(message, JSON.stringify(message.mentions, null, 4)); });