From 2a000646468978a5c10be081ecfa7654f7a0534c Mon Sep 17 00:00:00 2001 From: abalabahaha Date: Sun, 3 Jan 2016 11:14:17 -0800 Subject: [PATCH] Validate invite if possible --- lib/Client/InternalClient.js | 7 ++++++- lib/Structures/Invite.js | 2 +- src/Client/InternalClient.js | 12 +++++++++--- src/Structures/Invite.js | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index 4327a6942..e0216ef79 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -929,7 +929,12 @@ var InternalClient = (function () { } return this.apiRequest("get", _Constants.Endpoints.INVITE(invite), true).then(function (res) { - return new _StructuresInvite2["default"](res, _this21.channels.get("id", res.channel.id), _this21.client); + if (!_this21.channels.has("id", res.channel.id)) { + return new _StructuresInvite2["default"](res, null, _this21.client); + } + return _this21.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(res.channel.id), true, { validate: invite }).then(function (res2) { + return new _StructuresInvite2["default"](res2, _this21.channels.get("id", res.channel.id), _this21.client); + }); }); }; diff --git a/lib/Structures/Invite.js b/lib/Structures/Invite.js index 8c4940777..77b4be4db 100644 --- a/lib/Structures/Invite.js +++ b/lib/Structures/Invite.js @@ -10,7 +10,7 @@ var Invite = (function () { this.maxAge = data.max_age; this.code = data.code; - if (data.chan) { + if (chan) { this.channel = chan; this.server = chan.server; } else { diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index 8fd8faa9f..c95d4bd63 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -763,7 +763,7 @@ export default class InternalClient { //def deleteInvite deleteInvite(invite) { invite = this.resolver.resolveInviteID(invite); - if(!invite) { + if (!invite) { throw new Error("Not a valid invite"); } return this.apiRequest("del", Endpoints.INVITE(invite), true); @@ -772,12 +772,18 @@ export default class InternalClient { //def getInvite getInvite(invite) { invite = this.resolver.resolveInviteID(invite); - if(!invite) { + if (!invite) { return Promise.reject(new Error("Not a valid invite")); } return this.apiRequest("get", Endpoints.INVITE(invite), true) - .then(res => new Invite(res, this.channels.get("id", res.channel.id), this.client)); + .then(res => { + if (!this.channels.has("id", res.channel.id)) { + return new Invite(res, null, this.client); + } + return this.apiRequest("post", Endpoints.CHANNEL_INVITES(res.channel.id), true, {validate: invite}) + .then(res2 => new Invite(res2, this.channels.get("id", res.channel.id), this.client)); + }); } //def overwritePermissions diff --git a/src/Structures/Invite.js b/src/Structures/Invite.js index 61268ac3c..df21711df 100644 --- a/src/Structures/Invite.js +++ b/src/Structures/Invite.js @@ -4,7 +4,7 @@ export default class Invite { constructor(data, chan, client){ this.maxAge = data.max_age; this.code = data.code; - if (data.chan) { + if (chan) { this.channel = chan; this.server = chan.server; } else {