mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat(Client): allow options for generateInvite (#4741)
This commit is contained in:
@@ -356,21 +356,37 @@ class Client extends BaseClient {
|
||||
|
||||
/**
|
||||
* Generates a link that can be used to invite the bot to a guild.
|
||||
* @param {PermissionResolvable} [permissions] Permissions to request
|
||||
* @param {InviteGenerationOptions|PermissionResolvable} [options] Permissions to request
|
||||
* @returns {Promise<string>}
|
||||
* @example
|
||||
* client.generateInvite(['SEND_MESSAGES', 'MANAGE_GUILD', 'MENTION_EVERYONE'])
|
||||
* client.generateInvite({
|
||||
* permissions: ['SEND_MESSAGES', 'MANAGE_GUILD', 'MENTION_EVERYONE'],
|
||||
* })
|
||||
* .then(link => console.log(`Generated bot invite link: ${link}`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
async generateInvite(permissions) {
|
||||
permissions = Permissions.resolve(permissions);
|
||||
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 };
|
||||
}
|
||||
const application = await this.fetchApplication();
|
||||
const query = new URLSearchParams({
|
||||
client_id: application.id,
|
||||
permissions: permissions,
|
||||
permissions: Permissions.resolve(options.permissions),
|
||||
scope: 'bot',
|
||||
});
|
||||
if (typeof options.disableGuildSelect === 'boolean') {
|
||||
query.set('disable_guild_select', options.disableGuildSelect.toString());
|
||||
}
|
||||
if (typeof options.guild !== 'undefined') {
|
||||
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}`;
|
||||
}
|
||||
|
||||
@@ -443,6 +459,14 @@ 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
|
||||
|
||||
8
typings/index.d.ts
vendored
8
typings/index.d.ts
vendored
@@ -187,7 +187,7 @@ declare module 'discord.js' {
|
||||
public fetchInvite(invite: InviteResolvable): Promise<Invite>;
|
||||
public fetchVoiceRegions(): Promise<Collection<string, VoiceRegion>>;
|
||||
public fetchWebhook(id: Snowflake, token?: string): Promise<Webhook>;
|
||||
public generateInvite(permissions?: PermissionResolvable): Promise<string>;
|
||||
public generateInvite(options?: InviteGenerationOptions | PermissionResolvable): Promise<string>;
|
||||
public login(token?: string): Promise<string>;
|
||||
public sweepMessages(lifetime?: number): number;
|
||||
public toJSON(): object;
|
||||
@@ -2664,6 +2664,12 @@ declare module 'discord.js' {
|
||||
| 'DIRECT_MESSAGE_REACTIONS'
|
||||
| 'DIRECT_MESSAGE_TYPING';
|
||||
|
||||
interface InviteGenerationOptions {
|
||||
permissions?: PermissionResolvable;
|
||||
guild?: GuildResolvable;
|
||||
disableGuildSelect?: boolean;
|
||||
}
|
||||
|
||||
interface InviteOptions {
|
||||
temporary?: boolean;
|
||||
maxAge?: number;
|
||||
|
||||
Reference in New Issue
Block a user