fix: update Client#authorizeURL method

This commit is contained in:
Elysia
2025-01-20 22:59:10 +07:00
parent 48facbefea
commit 4442918d82
3 changed files with 25 additions and 29 deletions

View File

@@ -33,7 +33,6 @@ const Widget = require('../structures/Widget');
const { Events, Status } = require('../util/Constants'); const { Events, Status } = require('../util/Constants');
const DataResolver = require('../util/DataResolver'); const DataResolver = require('../util/DataResolver');
const Intents = require('../util/Intents'); const Intents = require('../util/Intents');
const Permissions = require('../util/Permissions');
const DiscordAuthWebsocket = require('../util/RemoteAuth'); const DiscordAuthWebsocket = require('../util/RemoteAuth');
const Sweepers = require('../util/Sweepers'); const Sweepers = require('../util/Sweepers');
@@ -699,7 +698,7 @@ class Client extends BaseClient {
/** /**
* @typedef {Object} OAuth2AuthorizeOptions * @typedef {Object} OAuth2AuthorizeOptions
* @property {string} [guild_id] Guild ID * @property {string} [guild_id] Guild ID
* @property {PermissionResolvable} [permissions] Permissions * @property {string} [permissions] Permissions
* @property {boolean} [authorize] Whether to authorize or not * @property {boolean} [authorize] Whether to authorize or not
* @property {string} [code] 2FA Code * @property {string} [code] 2FA Code
* @property {string} [webhook_channel_id] Webhook Channel ID * @property {string} [webhook_channel_id] Webhook Channel ID
@@ -707,9 +706,9 @@ class Client extends BaseClient {
/** /**
* Authorize an application. * Authorize an application.
* @param {string} url Discord Auth URL * @param {string} urlOAuth2 Discord Auth URL
* @param {OAuth2AuthorizeOptions} options Oauth2 options * @param {OAuth2AuthorizeOptions} [options] Oauth2 options
* @returns {Promise<any>} * @returns {Promise<{ location: string }>}
* @example * @example
* client.authorizeURL(`https://discord.com/api/oauth2/authorize?client_id=botID&permissions=8&scope=applications.commands%20bot`, { * client.authorizeURL(`https://discord.com/api/oauth2/authorize?client_id=botID&permissions=8&scope=applications.commands%20bot`, {
guild_id: "guildID", guild_id: "guildID",
@@ -717,31 +716,29 @@ class Client extends BaseClient {
authorize: true authorize: true
}) })
*/ */
authorizeURL(url, options = { authorize: true }) { authorizeURL(urlOAuth2, options = {}) {
// ! throw new Error('METHOD_WARNING'); // ! throw new Error('METHOD_WARNING');
const pathnameAPI = /\/api\/(v\d{1,2}\/)?oauth2\/authorize/; const url = new URL(urlOAuth2);
const pathnameURL = /\/oauth2\/authorize/; if (!/https:\/\/(canary\.|ptb\.)?discord.com\/api(\/v\d{1,2})?\/oauth2\/authorize\?/.test(urlOAuth2)) {
const url_ = new URL(url); throw new Error('INVALID_URL', urlOAuth2);
if (
!['discord.com', 'canary.discord.com', 'ptb.discord.com'].includes(url_.hostname) ||
(!pathnameAPI.test(url_.pathname) && !pathnameURL.test(url_.pathname))
) {
throw new Error('INVALID_URL', url);
} }
const searchParams = Object.fromEntries(url_.searchParams); const searchParams = Object.fromEntries(url.searchParams);
options.permissions ??= `${Permissions.resolve(searchParams.permissions || 0)}`; // Assign options
options.integration_type ??= searchParams.integration_type || 0; options = {
options.location_context = { authorize: true,
permissions: '0',
integration_type: 0,
location_context: {
guild_id: '10000', guild_id: '10000',
channel_id: '10000', channel_id: '10000',
channel_type: 10000, channel_type: 10000,
},
...searchParams,
...options,
}; };
options.guild_id ??= searchParams.guild_id;
options.authorize ??= true;
delete searchParams.permissions; delete searchParams.permissions;
delete searchParams.integration_type; delete searchParams.integration_type;
delete searchParams.guild_id; delete searchParams.guild_id;
if (!options.permissions || !options.guild_id) throw new Error('INVALID_OAUTH_OPTIONS');
return this.api.oauth2.authorize.post({ return this.api.oauth2.authorize.post({
query: searchParams, query: searchParams,
data: options, data: options,

View File

@@ -209,7 +209,6 @@ const Messages = {
STREAM_CANNOT_JOIN: 'Cannot join a stream to itself', STREAM_CANNOT_JOIN: 'Cannot join a stream to itself',
VOICE_USER_NOT_STREAMING: 'User is not streaming', VOICE_USER_NOT_STREAMING: 'User is not streaming',
POLL_ALREADY_EXPIRED: 'This poll has already expired.', POLL_ALREADY_EXPIRED: 'This poll has already expired.',
INVALID_OAUTH_OPTIONS: 'Invalid options for authenticating with OAuth2.',
METHOD_WARNING: METHOD_WARNING:
'This method is flagged as it may lead to a temporary or permanent account ban. Do not use until further notice.', 'This method is flagged as it may lead to a temporary or permanent account ban. Do not use until further notice.',
}; };

8
typings/index.d.ts vendored
View File

@@ -794,7 +794,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
options?: AcceptInviteOptions, options?: AcceptInviteOptions,
): Promise<Guild | DMChannel | GroupDMChannel>; ): Promise<Guild | DMChannel | GroupDMChannel>;
public redeemNitro(nitro: string, channel?: TextChannelResolvable, paymentSourceId?: Snowflake): Promise<any>; public redeemNitro(nitro: string, channel?: TextChannelResolvable, paymentSourceId?: Snowflake): Promise<any>;
public authorizeURL(url: string, options?: OAuth2AuthorizeOptions): Promise<any>; public authorizeURL(urlOAuth2: string, options?: OAuth2AuthorizeOptions): Promise<any>;
public installUserApps(applicationId: Snowflake): Promise<void>; public installUserApps(applicationId: Snowflake): Promise<void>;
public deauthorize(applicationId: Snowflake): Promise<void>; public deauthorize(applicationId: Snowflake): Promise<void>;
@@ -828,13 +828,13 @@ export interface AcceptInviteOptions {
bypassVerify: boolean; bypassVerify: boolean;
} }
export interface OAuth2AuthorizeOptions { export type OAuth2AuthorizeOptions = {
guild_id?: Snowflake; guild_id?: Snowflake;
permissions?: PermissionResolvable; permissions?: string;
authorize?: boolean; authorize?: boolean;
code?: string; code?: string;
webhook_channel_id?: Snowflake; webhook_channel_id?: Snowflake;
} } & Record<string, unknown>;
export class ClientPresence extends Presence { export class ClientPresence extends Presence {
private constructor(client: Client, data: RawPresenceData); private constructor(client: Client, data: RawPresenceData);