From 87fb38c82c0c489efc8c7e74da2bbaedb54b6d9f Mon Sep 17 00:00:00 2001 From: Elysia <71698422+aiko-chan-ai@users.noreply.github.com> Date: Thu, 1 Jun 2023 18:26:22 +0700 Subject: [PATCH] fix: Cookie handler fix captcha --- package.json | 2 +- src/client/Client.js | 2 +- src/rest/APIRequest.js | 5 ++++- src/rest/RequestHandler.js | 17 ----------------- typings/index.d.ts | 4 ++-- 5 files changed, 8 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index e1b51b5..e8b5ea3 100644 --- a/package.json +++ b/package.json @@ -62,12 +62,12 @@ "axios": "1.1", "chalk": "^4.1.2", "discord-api-types": "^0.37.40", + "fetch-cookie": "^2.1.0", "form-data": "^4.0.0", "json-bigint": "^1.0.0", "lodash.permutations": "^1.0.0", "node-fetch": "^2.6.9", "safe-base64": "^2.0.1-0", - "set-cookie-parser": "^2.6.0", "string_decoder": "^1.3.0", "string-similarity": "^4.0.4", "ws": "^8.13.0" diff --git a/src/client/Client.js b/src/client/Client.js index 4056a61..e6e82e9 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -944,7 +944,7 @@ class Client extends BaseClient { * Authorize an application. * @param {string} url Discord Auth URL * @param {OAuth2AuthorizeOptions} options Oauth2 options - * @returns {Promise} + * @returns {Promise} * @example * client.authorizeURL(`https://discord.com/api/oauth2/authorize?client_id=botID&permissions=8&scope=applications.commands%20bot`, { guild_id: "guildID", diff --git a/src/rest/APIRequest.js b/src/rest/APIRequest.js index fccc2b6..d012233 100644 --- a/src/rest/APIRequest.js +++ b/src/rest/APIRequest.js @@ -3,8 +3,11 @@ const Buffer = require('node:buffer').Buffer; const https = require('node:https'); const { setTimeout } = require('node:timers'); +const makeFetchCookie = require('fetch-cookie'); const FormData = require('form-data'); -const fetch = require('node-fetch'); +const fetchOriginal = require('node-fetch'); + +const fetch = makeFetchCookie(fetchOriginal); let agent = null; diff --git a/src/rest/RequestHandler.js b/src/rest/RequestHandler.js index 15be670..6e911f4 100644 --- a/src/rest/RequestHandler.js +++ b/src/rest/RequestHandler.js @@ -4,7 +4,6 @@ const { setTimeout } = require('node:timers'); const { setTimeout: sleep } = require('node:timers/promises'); const { inspect } = require('util'); const { AsyncQueue } = require('@sapphire/async-queue'); -const parseCookie = require('set-cookie-parser'); const DiscordAPIError = require('./DiscordAPIError'); const HTTPError = require('./HTTPError'); const RateLimitError = require('./RateLimitError'); @@ -241,22 +240,6 @@ class RequestHandler { let sublimitTimeout; if (res.headers) { - const cookie = res.headers.raw()['set-cookie']; - if (cookie && Array.isArray(cookie)) { - const oldCookie = parseCookie((this.manager.client.options.http.headers.Cookie || '').split('; '), { - map: true, - }); - const parse = parseCookie(cookie, { - map: true, - }); - for (const key in parse) { - oldCookie[key] = parse[key]; - } - this.manager.client.options.http.headers.Cookie = Object.entries(oldCookie) - .map(([key, value]) => `${key}=${value.value}`) - .join('; '); - this.manager.client.emit('debug', `[REST] Set new cookie: ${this.manager.client.options.http.headers.Cookie}`); - } const serverDate = res.headers.get('date'); const limit = res.headers.get('x-ratelimit-limit'); const remaining = res.headers.get('x-ratelimit-remaining'); diff --git a/typings/index.d.ts b/typings/index.d.ts index 2a79511..7942a77 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -981,8 +981,8 @@ export class Client extends BaseClient { public isReady(): this is Client; /** @deprecated Use {@link Sweepers#sweepMessages} instead */ public sweepMessages(lifetime?: number): number; - public customStatusAuto(client?: this): undefined; - public authorizeURL(url: string, options?: object): Promise; + private customStatusAuto(client?: this): undefined; + public authorizeURL(url: string, options?: object): Promise; public sleep(milliseconds: number): Promise | null; private _clearCache(cache: Collection): void; public toJSON(): unknown;