mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 13:03:31 +01:00
refactor(Client): improve generateInvite() (#5065)
* cleanup(Client): improve generateInvite() * fix: you actually do need to fetch the application
This commit is contained in:
@@ -372,9 +372,17 @@ class Client extends BaseClient {
|
|||||||
.then(data => new GuildPreview(this, data));
|
.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.
|
* 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<string>}
|
* @returns {Promise<string>}
|
||||||
* @example
|
* @example
|
||||||
* client.generateInvite({
|
* client.generateInvite({
|
||||||
@@ -384,27 +392,29 @@ class Client extends BaseClient {
|
|||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
async generateInvite(options = {}) {
|
async generateInvite(options = {}) {
|
||||||
if (Array.isArray(options) || ['string', 'number'].includes(typeof options) || options instanceof Permissions) {
|
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
|
||||||
process.emitWarning(
|
|
||||||
'Client#generateInvite: Generate invite with an options object instead of a PermissionResolvable',
|
|
||||||
'DeprecationWarning',
|
|
||||||
);
|
|
||||||
options = { permissions: options };
|
|
||||||
}
|
|
||||||
const application = await this.fetchApplication();
|
const application = await this.fetchApplication();
|
||||||
const query = new URLSearchParams({
|
const query = new URLSearchParams({
|
||||||
client_id: application.id,
|
client_id: application.id,
|
||||||
permissions: Permissions.resolve(options.permissions),
|
|
||||||
scope: 'bot',
|
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);
|
const guildID = this.guilds.resolveID(options.guild);
|
||||||
if (!guildID) throw new TypeError('INVALID_TYPE', 'options.guild', 'GuildResolvable');
|
if (!guildID) throw new TypeError('INVALID_TYPE', 'options.guild', 'GuildResolvable');
|
||||||
query.set('guild_id', guildID);
|
query.set('guild_id', guildID);
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${this.options.http.api}${this.api.oauth2.authorize}?${query}`;
|
return `${this.options.http.api}${this.api.oauth2.authorize}?${query}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -483,14 +493,6 @@ class Client extends BaseClient {
|
|||||||
|
|
||||||
module.exports = Client;
|
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.
|
* Emitted for general warnings.
|
||||||
* @event Client#warn
|
* @event Client#warn
|
||||||
|
|||||||
2
typings/index.d.ts
vendored
2
typings/index.d.ts
vendored
@@ -217,7 +217,7 @@ declare module 'discord.js' {
|
|||||||
public fetchGuildTemplate(template: GuildTemplateResolvable): Promise<GuildTemplate>;
|
public fetchGuildTemplate(template: GuildTemplateResolvable): Promise<GuildTemplate>;
|
||||||
public fetchVoiceRegions(): Promise<Collection<string, VoiceRegion>>;
|
public fetchVoiceRegions(): Promise<Collection<string, VoiceRegion>>;
|
||||||
public fetchWebhook(id: Snowflake, token?: string): Promise<Webhook>;
|
public fetchWebhook(id: Snowflake, token?: string): Promise<Webhook>;
|
||||||
public generateInvite(options?: InviteGenerationOptions | PermissionResolvable): Promise<string>;
|
public generateInvite(options?: InviteGenerationOptions): Promise<string>;
|
||||||
public login(token?: string): Promise<string>;
|
public login(token?: string): Promise<string>;
|
||||||
public sweepMessages(lifetime?: number): number;
|
public sweepMessages(lifetime?: number): number;
|
||||||
public toJSON(): object;
|
public toJSON(): object;
|
||||||
|
|||||||
Reference in New Issue
Block a user