mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 00:53:31 +01:00
feat(Client): enforce passing scopes to generateInvite (#6024)
Co-authored-by: Antonio Román <kyradiscord@gmail.com> Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
This commit is contained in:
@@ -375,10 +375,10 @@ class Client extends BaseClient {
|
||||
/**
|
||||
* Options for {@link Client#generateInvite}.
|
||||
* @typedef {Object} InviteGenerationOptions
|
||||
* @property {InviteScope[]} scopes Scopes that should be requested
|
||||
* @property {PermissionResolvable} [permissions] Permissions to request
|
||||
* @property {GuildResolvable} [guild] Guild to preselect
|
||||
* @property {boolean} [disableGuildSelect] Whether to disable the guild selection
|
||||
* @property {InviteScope[]} [additionalScopes] Whether any additional scopes should be requested
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -387,11 +387,17 @@ class Client extends BaseClient {
|
||||
* @returns {string}
|
||||
* @example
|
||||
* const link = client.generateInvite({
|
||||
* scopes: ['applications.commands'],
|
||||
* });
|
||||
* console.log(`Generated application invite link: ${link}`);
|
||||
* @example
|
||||
* const link = client.generateInvite({
|
||||
* permissions: [
|
||||
* Permissions.FLAGS.SEND_MESSAGES,
|
||||
* Permissions.FLAGS.MANAGE_GUILD,
|
||||
* Permissions.FLAGS.MENTION_EVERYONE,
|
||||
* ],
|
||||
* scopes: ['bot'],
|
||||
* });
|
||||
* console.log(`Generated bot invite link: ${link}`);
|
||||
*/
|
||||
@@ -401,9 +407,24 @@ class Client extends BaseClient {
|
||||
|
||||
const query = new URLSearchParams({
|
||||
client_id: this.application.id,
|
||||
scope: 'bot',
|
||||
});
|
||||
|
||||
const { scopes } = options;
|
||||
if (typeof scopes === 'undefined') {
|
||||
throw new TypeError('INVITE_MISSING_SCOPES');
|
||||
}
|
||||
if (!Array.isArray(scopes)) {
|
||||
throw new TypeError('INVALID_TYPE', 'scopes', 'Array of Invite Scopes', true);
|
||||
}
|
||||
if (!scopes.some(scope => ['bot', 'applications.commands'].includes(scope))) {
|
||||
throw new TypeError('INVITE_MISSING_SCOPES');
|
||||
}
|
||||
const invalidScope = scopes.find(scope => !InviteScopes.includes(scope));
|
||||
if (invalidScope) {
|
||||
throw new TypeError('INVALID_ELEMENT', 'Array', 'scopes', invalidScope);
|
||||
}
|
||||
query.set('scope', scopes.join(' '));
|
||||
|
||||
if (options.permissions) {
|
||||
const permissions = Permissions.resolve(options.permissions);
|
||||
if (permissions) query.set('permissions', permissions);
|
||||
@@ -419,18 +440,6 @@ class Client extends BaseClient {
|
||||
query.set('guild_id', guildId);
|
||||
}
|
||||
|
||||
if (options.additionalScopes) {
|
||||
const scopes = options.additionalScopes;
|
||||
if (!Array.isArray(scopes)) {
|
||||
throw new TypeError('INVALID_TYPE', 'additionalScopes', 'Array of Invite Scopes', true);
|
||||
}
|
||||
const invalidScope = scopes.find(scope => !InviteScopes.includes(scope));
|
||||
if (invalidScope) {
|
||||
throw new TypeError('INVALID_ELEMENT', 'Array', 'additionalScopes', invalidScope);
|
||||
}
|
||||
query.set('scope', ['bot', ...scopes].join(' '));
|
||||
}
|
||||
|
||||
return `${this.options.http.api}${this.api.oauth2.authorize}?${query}`;
|
||||
}
|
||||
|
||||
|
||||
@@ -132,6 +132,8 @@ const Messages = {
|
||||
INTERACTION_EPHEMERAL_REPLIED: 'Ephemeral responses cannot be fetched or deleted.',
|
||||
INTERACTION_FETCH_EPHEMERAL: 'Ephemeral responses cannot be fetched.',
|
||||
|
||||
INVITE_MISSING_SCOPES: 'At least one valid scope must be provided for the invite',
|
||||
|
||||
NOT_IMPLEMENTED: (what, name) => `Method ${what} not implemented on ${name}.`,
|
||||
};
|
||||
|
||||
|
||||
@@ -328,6 +328,7 @@ exports.InviteScopes = [
|
||||
'applications.commands',
|
||||
'applications.entitlements',
|
||||
'applications.store.update',
|
||||
'bot',
|
||||
'connections',
|
||||
'email',
|
||||
'identity',
|
||||
|
||||
2
typings/index.d.ts
vendored
2
typings/index.d.ts
vendored
@@ -3696,7 +3696,7 @@ declare module 'discord.js' {
|
||||
permissions?: PermissionResolvable;
|
||||
guild?: GuildResolvable;
|
||||
disableGuildSelect?: boolean;
|
||||
additionalScopes?: InviteScope[];
|
||||
scopes: InviteScope[];
|
||||
}
|
||||
|
||||
interface CreateInviteOptions {
|
||||
|
||||
Reference in New Issue
Block a user