feat: integrate otplib for TOTP authentication and remove deprecated TOTP utility

This commit is contained in:
Elysia
2025-07-10 20:11:13 +07:00
parent d774c97d94
commit a82ba7aad1
7 changed files with 51 additions and 216 deletions

View File

@@ -2,15 +2,10 @@
const Buffer = require('node:buffer').Buffer;
const { setTimeout } = require('node:timers');
const makeFetchCookie = require('fetch-cookie');
const { CookieJar } = require('tough-cookie');
const { fetch: fetchOriginal, FormData, buildConnector, Client, ProxyAgent } = require('undici');
const { FormData, buildConnector, Client, ProxyAgent } = require('undici');
const { ciphers } = require('../util/Constants');
const Util = require('../util/Util');
const cookieJar = new CookieJar();
const fetch = makeFetchCookie.default(fetchOriginal, cookieJar);
let agent = null;
class APIRequest {
@@ -142,15 +137,17 @@ class APIRequest {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), this.client.options.restRequestTimeout).unref();
return fetch(url, {
method: this.method.toUpperCase(), // Undici doesn't normalize "patch" into "PATCH" (which surprisingly follows the spec).
headers,
body,
signal: controller.signal,
redirect: 'follow',
dispatcher: agent,
credentials: 'include',
}).finally(() => clearTimeout(timeout));
return this.rest
.fetch(url, {
method: this.method.toUpperCase(), // Undici doesn't normalize "patch" into "PATCH" (which surprisingly follows the spec).
headers,
body,
signal: controller.signal,
redirect: 'follow',
dispatcher: agent,
credentials: 'include',
})
.finally(() => clearTimeout(timeout));
}
}

View File

@@ -2,6 +2,9 @@
const { setInterval } = require('node:timers');
const { Collection } = require('@discordjs/collection');
const makeFetchCookie = require('fetch-cookie');
const { CookieJar } = require('tough-cookie');
const { fetch: fetchOriginal } = require('undici');
const APIRequest = require('./APIRequest');
const routeBuilder = require('./APIRouter');
const RequestHandler = require('./RequestHandler');
@@ -17,6 +20,8 @@ class RESTManager {
this.globalRemaining = this.globalLimit;
this.globalReset = null;
this.globalDelay = null;
this.cookieJar = new CookieJar();
this.fetch = makeFetchCookie.default(fetchOriginal, this.cookieJar);
if (client.options.restSweepInterval > 0) {
this.sweepInterval = setInterval(() => {
this.handlers.sweep(handler => handler._inactive);

View File

@@ -9,7 +9,6 @@ const RateLimitError = require('./RateLimitError');
const {
Events: { DEBUG, RATE_LIMIT, INVALID_REQUEST_WARNING, API_RESPONSE, API_REQUEST },
} = require('../util/Constants');
const TOTP = require('../util/Totp');
const captchaMessage = [
'incorrect-captcha',
@@ -396,7 +395,7 @@ class RequestHandler {
request.retries < 1
) {
// Get mfa code
const { otp } = await TOTP.generate(this.manager.client.options.TOTPKey);
const otp = this.manager.client.authenticator.generate(this.manager.client.options.TOTPKey);
this.manager.client.emit(
DEBUG,
`${data.message}