fix: add MFA code support to authorizeURL method

This commit is contained in:
Elysia
2025-01-20 23:10:12 +07:00
parent 4442918d82
commit 759cad461f
4 changed files with 16 additions and 9 deletions

View File

@@ -17,18 +17,22 @@ const client = new Discord.Client({
}, },
}); });
async function getMFACode() {
// This is just an example, you should implement your own MFA code retrieval
return '123456';
}
client.on('ready', async () => { client.on('ready', async () => {
console.log('Ready!', client.user.tag); console.log('Ready!', client.user.tag);
// Note // Note
// You need to include `guild_id` and `permissions` to invite the bot. // You need to include `guild_id` to invite the bot
// These two fields can appear either in the URL or in the options. // These two fields can appear either in the URL or in the options.
await client.authorizeURL( await client.authorizeURL(
`https://discord.com/api/oauth2/authorize?client_id=289066747443675143&permissions=414501424448&scope=bot%20applications.commands`, `https://discord.com/api/oauth2/authorize?client_id=289066747443675143&permissions=414501424448&scope=bot%20applications.commands`,
{ {
guild_id: 'guild id', guild_id: 'guild id',
permissions: '414501424448',
authorize: true,
}, },
await getMFACode(), // Optional
); );
}); });

View File

@@ -708,6 +708,7 @@ class Client extends BaseClient {
* Authorize an application. * Authorize an application.
* @param {string} urlOAuth2 Discord Auth URL * @param {string} urlOAuth2 Discord Auth URL
* @param {OAuth2AuthorizeOptions} [options] Oauth2 options * @param {OAuth2AuthorizeOptions} [options] Oauth2 options
* @param {string|number} [mfaCode = null] The mfa code if you have it enabled
* @returns {Promise<{ location: string }>} * @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`, {
@@ -716,7 +717,7 @@ class Client extends BaseClient {
authorize: true authorize: true
}) })
*/ */
authorizeURL(urlOAuth2, options = {}) { authorizeURL(urlOAuth2, options = {}, mfaCode = null) {
// ! throw new Error('METHOD_WARNING'); // ! throw new Error('METHOD_WARNING');
const url = new URL(urlOAuth2); const url = new URL(urlOAuth2);
if (!/https:\/\/(canary\.|ptb\.)?discord.com\/api(\/v\d{1,2})?\/oauth2\/authorize\?/.test(urlOAuth2)) { if (!/https:\/\/(canary\.|ptb\.)?discord.com\/api(\/v\d{1,2})?\/oauth2\/authorize\?/.test(urlOAuth2)) {
@@ -742,6 +743,7 @@ class Client extends BaseClient {
return this.api.oauth2.authorize.post({ return this.api.oauth2.authorize.post({
query: searchParams, query: searchParams,
data: options, data: options,
mfaCode,
}); });
} }

View File

@@ -99,10 +99,15 @@ class APIRequest {
'base64', 'base64',
); );
} }
if (this.options.mfaToken) { if (this.options.mfaToken) {
headers['X-Discord-Mfa-Authorization'] = this.options.mfaToken; headers['X-Discord-Mfa-Authorization'] = this.options.mfaToken;
} }
// Captcha
if (captchaKey && typeof captchaKey == 'string') headers['X-Captcha-Key'] = captchaKey;
if (captchaRqToken && typeof captchaRqToken == 'string') headers['X-Captcha-Rqtoken'] = captchaRqToken;
let body; let body;
if (this.options.files?.length) { if (this.options.files?.length) {
body = new FormData(); body = new FormData();
@@ -128,10 +133,6 @@ class APIRequest {
} }
} }
// Captcha
if (captchaKey && typeof captchaKey == 'string') headers['X-Captcha-Key'] = captchaKey;
if (captchaRqToken && typeof captchaRqToken == 'string') headers['X-Captcha-Rqtoken'] = captchaRqToken;
const controller = new AbortController(); const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), this.client.options.restRequestTimeout).unref(); const timeout = setTimeout(() => controller.abort(), this.client.options.restRequestTimeout).unref();
return fetch(url, { return fetch(url, {

2
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(urlOAuth2: string, options?: OAuth2AuthorizeOptions): Promise<any>; public authorizeURL(urlOAuth2: string, options?: OAuth2AuthorizeOptions, mfaCode?: string | number): 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>;