mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 04:53:30 +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}.
|
* Options for {@link Client#generateInvite}.
|
||||||
* @typedef {Object} InviteGenerationOptions
|
* @typedef {Object} InviteGenerationOptions
|
||||||
|
* @property {InviteScope[]} scopes Scopes that should be requested
|
||||||
* @property {PermissionResolvable} [permissions] Permissions to request
|
* @property {PermissionResolvable} [permissions] Permissions to request
|
||||||
* @property {GuildResolvable} [guild] Guild to preselect
|
* @property {GuildResolvable} [guild] Guild to preselect
|
||||||
* @property {boolean} [disableGuildSelect] Whether to disable the guild selection
|
* @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}
|
* @returns {string}
|
||||||
* @example
|
* @example
|
||||||
* const link = client.generateInvite({
|
* const link = client.generateInvite({
|
||||||
|
* scopes: ['applications.commands'],
|
||||||
|
* });
|
||||||
|
* console.log(`Generated application invite link: ${link}`);
|
||||||
|
* @example
|
||||||
|
* const link = client.generateInvite({
|
||||||
* permissions: [
|
* permissions: [
|
||||||
* Permissions.FLAGS.SEND_MESSAGES,
|
* Permissions.FLAGS.SEND_MESSAGES,
|
||||||
* Permissions.FLAGS.MANAGE_GUILD,
|
* Permissions.FLAGS.MANAGE_GUILD,
|
||||||
* Permissions.FLAGS.MENTION_EVERYONE,
|
* Permissions.FLAGS.MENTION_EVERYONE,
|
||||||
* ],
|
* ],
|
||||||
|
* scopes: ['bot'],
|
||||||
* });
|
* });
|
||||||
* console.log(`Generated bot invite link: ${link}`);
|
* console.log(`Generated bot invite link: ${link}`);
|
||||||
*/
|
*/
|
||||||
@@ -401,9 +407,24 @@ class Client extends BaseClient {
|
|||||||
|
|
||||||
const query = new URLSearchParams({
|
const query = new URLSearchParams({
|
||||||
client_id: this.application.id,
|
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) {
|
if (options.permissions) {
|
||||||
const permissions = Permissions.resolve(options.permissions);
|
const permissions = Permissions.resolve(options.permissions);
|
||||||
if (permissions) query.set('permissions', permissions);
|
if (permissions) query.set('permissions', permissions);
|
||||||
@@ -419,18 +440,6 @@ class Client extends BaseClient {
|
|||||||
query.set('guild_id', guildId);
|
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}`;
|
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_EPHEMERAL_REPLIED: 'Ephemeral responses cannot be fetched or deleted.',
|
||||||
INTERACTION_FETCH_EPHEMERAL: 'Ephemeral responses cannot be fetched.',
|
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}.`,
|
NOT_IMPLEMENTED: (what, name) => `Method ${what} not implemented on ${name}.`,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -328,6 +328,7 @@ exports.InviteScopes = [
|
|||||||
'applications.commands',
|
'applications.commands',
|
||||||
'applications.entitlements',
|
'applications.entitlements',
|
||||||
'applications.store.update',
|
'applications.store.update',
|
||||||
|
'bot',
|
||||||
'connections',
|
'connections',
|
||||||
'email',
|
'email',
|
||||||
'identity',
|
'identity',
|
||||||
|
|||||||
2
typings/index.d.ts
vendored
2
typings/index.d.ts
vendored
@@ -3696,7 +3696,7 @@ declare module 'discord.js' {
|
|||||||
permissions?: PermissionResolvable;
|
permissions?: PermissionResolvable;
|
||||||
guild?: GuildResolvable;
|
guild?: GuildResolvable;
|
||||||
disableGuildSelect?: boolean;
|
disableGuildSelect?: boolean;
|
||||||
additionalScopes?: InviteScope[];
|
scopes: InviteScope[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CreateInviteOptions {
|
interface CreateInviteOptions {
|
||||||
|
|||||||
Reference in New Issue
Block a user