From 5b51392724f7b41d2276824672bc7365e952fb83 Mon Sep 17 00:00:00 2001 From: Jan <66554238+Vaporox@users.noreply.github.com> Date: Sat, 12 Dec 2020 22:38:57 +0100 Subject: [PATCH] refactor(Client): improve generateInvite() (#5065) * cleanup(Client): improve generateInvite() * fix: you actually do need to fetch the application --- src/client/Client.js | 42 ++++++++++++++++++++++-------------------- typings/index.d.ts | 2 +- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/client/Client.js b/src/client/Client.js index 1d88cfac1..cefd4ebb6 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -372,9 +372,17 @@ class Client extends BaseClient { .then(data => new GuildPreview(this, data)); } + /** + * Options for {@link Client#generateInvite}. + * @typedef {Object} InviteGenerationOptions + * @property {PermissionResolvable} [permissions] Permissions to request + * @property {GuildResolvable} [guild] Guild to preselect + * @property {boolean} [disableGuildSelect] Whether to disable the guild selection + */ + /** * Generates a link that can be used to invite the bot to a guild. - * @param {InviteGenerationOptions|PermissionResolvable} [options] Permissions to request + * @param {InviteGenerationOptions} [options={}] Options for the invite * @returns {Promise} * @example * client.generateInvite({ @@ -384,27 +392,29 @@ class Client extends BaseClient { * .catch(console.error); */ async generateInvite(options = {}) { - if (Array.isArray(options) || ['string', 'number'].includes(typeof options) || options instanceof Permissions) { - process.emitWarning( - 'Client#generateInvite: Generate invite with an options object instead of a PermissionResolvable', - 'DeprecationWarning', - ); - options = { permissions: options }; - } + if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true); + const application = await this.fetchApplication(); const query = new URLSearchParams({ client_id: application.id, - permissions: Permissions.resolve(options.permissions), scope: 'bot', }); - if (typeof options.disableGuildSelect === 'boolean') { - query.set('disable_guild_select', options.disableGuildSelect.toString()); + + if (options.permissions) { + const permissions = Permissions.resolve(options.permissions); + if (permissions) query.set('permissions', permissions); } - if (typeof options.guild !== 'undefined') { + + if (options.disableGuildSelect) { + query.set('disable_guild_select', true); + } + + if (options.guild) { const guildID = this.guilds.resolveID(options.guild); if (!guildID) throw new TypeError('INVALID_TYPE', 'options.guild', 'GuildResolvable'); query.set('guild_id', guildID); } + return `${this.options.http.api}${this.api.oauth2.authorize}?${query}`; } @@ -483,14 +493,6 @@ class Client extends BaseClient { module.exports = Client; -/** - * Options for {@link Client#generateInvite}. - * @typedef {Object} InviteGenerationOptions - * @property {PermissionResolvable} [permissions] Permissions to request - * @property {GuildResolvable} [guild] Guild to preselect - * @property {boolean} [disableGuildSelect] Whether to disable the guild selection - */ - /** * Emitted for general warnings. * @event Client#warn diff --git a/typings/index.d.ts b/typings/index.d.ts index 7e406f407..11c0190b2 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -217,7 +217,7 @@ declare module 'discord.js' { public fetchGuildTemplate(template: GuildTemplateResolvable): Promise; public fetchVoiceRegions(): Promise>; public fetchWebhook(id: Snowflake, token?: string): Promise; - public generateInvite(options?: InviteGenerationOptions | PermissionResolvable): Promise; + public generateInvite(options?: InviteGenerationOptions): Promise; public login(token?: string): Promise; public sweepMessages(lifetime?: number): number; public toJSON(): object;