From dd6dd6fb59d747be9edb7651bc55ff9181be2ef5 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 8 Feb 2017 22:04:39 +0100 Subject: [PATCH] Added support for adding users to guild 2 (#1179) * Added support for adding users to guild added RESTMethods#AddGuildMemberOptions and Guild#addMember with typedef AddGuildMemberOptions to be able to add user to guild as a member through `PUT/guilds/{guild.id}/members/{user.id}` https://discordapp.com/developers/docs/resources/guild#add-guild-member * fixing lint errors * Changes based on discussion * Changes based on discussion 2 * Changes based on discussion 3 Yay! More changes. --- src/client/rest/RESTMethods.js | 16 ++++++++++++++++ src/structures/Guild.js | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index ce257b210..32eb154e3 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -377,6 +377,22 @@ class RESTMethods { return this.rest.makeRequest('get', Constants.Endpoints.channelMessage(channel.id, messageID), true); } + putGuildMember(guild, user, options) { + if (options.roles) { + var roles = options.roles; + if (roles instanceof Collection || (roles instanceof Array && roles[0] instanceof Role)) { + options.roles = roles.map(role => role.id); + } + } + if (options.accessToken) { + options.access_token = options.accessToken; + } else { + return Promise.reject(new Error('OAuth2 access token was not specified.')); + } + return this.rest.makeRequest('put', Constants.Endpoints.guildMember(guild.id, user.id), true, options) + .then(data => this.client.actions.GuildMemberGet.handle(guild, data).member); + } + getGuildMember(guild, user, cache) { return this.rest.makeRequest('get', Constants.Endpoints.guildMember(guild.id, user.id), true).then(data => { if (cache) { diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 131e09ff4..02e605d93 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -331,6 +331,28 @@ class Guild { return this.client.rest.methods.fetchVoiceRegions(this.id); } + /** + * The data for a role + * @typedef {Object} AddGuildMemberOptions + * @property {string} accessToken An oauth2 access token granted with the guilds.join to the bot's application + * for the user you want to add to the guild + * @property {string} [nick] Value to set users nickname to + * @property {Collection|Role[]|string[]} [roles] The roles or role IDs to add + * @property {boolean} [mute] If the user is muted + * @property {boolean} [deaf] If the user is deafened + */ + + /** + * Add a user to this guild using OAuth2 + * @param {UserResolvable|string} user The user or ID of the user to add to guild + * @param {AddGuildMemberOptions} options Options object containing the access_token + * @returns {Promise} + */ + addMember(user, options) { + if (this.members.has(user.id)) return Promise.resolve(this.members.get(user.id)); + return this.client.rest.methods.putGuildMember(this, user, options); + } + /** * Fetch a single guild member from a user. * @param {UserResolvable} user The user to fetch the member for