From 48be4013307e34c65950e0619b014c16e346b8fd Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Sat, 14 Jan 2017 15:25:12 -0600 Subject: [PATCH] add clientuser acceptinvite (#1081) * add clientuser acceptinvite * Update RESTMethods.js * Update ClientUser.js * Update ClientUser.js * Update RESTMethods.js --- src/client/rest/RESTMethods.js | 19 +++++++++++++++++++ src/structures/ClientUser.js | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index 4fdc61b79..b5a22a10e 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -715,6 +715,25 @@ class RESTMethods { setNote(user, note) { return this.rest.makeRequest('put', Constants.Endpoints.note(user.id), true, { note }).then(() => user); } + + acceptInvite(code) { + if (code.id) code = code.id; + return new Promise((resolve, reject) => + this.rest.makeRequest('post', Constants.Endpoints.invite(code), true).then((res) => { + const handler = guild => { + if (guild.id === res.id) { + resolve(guild); + this.client.removeListener('guildCreate', handler); + } + }; + this.client.on('guildCreate', handler); + this.client.setTimeout(() => { + this.client.removeListener('guildCreate', handler); + reject(new Error('Accepting invite timed out')); + }, 120e3); + }) + ); + } } module.exports = RESTMethods; diff --git a/src/structures/ClientUser.js b/src/structures/ClientUser.js index 5ff0057df..43eddb7c7 100644 --- a/src/structures/ClientUser.js +++ b/src/structures/ClientUser.js @@ -297,6 +297,14 @@ class ClientUser extends User { ); } } + + /** + * @param {Invite|string} invite Invite or code to accept + * @returns {Promise} Joined guild + */ + acceptInvite(invite) { + return this.client.rest.methods.acceptInvite(invite); + } } module.exports = ClientUser;