From c33767095ceb0045f147005fb3ed6d7661e80646 Mon Sep 17 00:00:00 2001 From: Elysia <71698422+aiko-chan-ai@users.noreply.github.com> Date: Sat, 3 Jun 2023 13:34:56 +0700 Subject: [PATCH] fix: captcha data --- package.json | 1 + src/rest/APIRequest.js | 33 ++++++++++++---------------- src/rest/RequestHandler.js | 1 + src/structures/ApplicationCommand.js | 6 +++-- src/util/Options.js | 2 +- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index aca9e8b..19c23db 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "safe-base64": "^2.0.1-0", "string_decoder": "^1.3.0", "string-similarity": "^4.0.4", + "tough-cookie": "^4.1.2", "ws": "^8.13.0" }, "engines": { diff --git a/src/rest/APIRequest.js b/src/rest/APIRequest.js index d012233..9b57505 100644 --- a/src/rest/APIRequest.js +++ b/src/rest/APIRequest.js @@ -6,8 +6,10 @@ const { setTimeout } = require('node:timers'); const makeFetchCookie = require('fetch-cookie'); const FormData = require('form-data'); const fetchOriginal = require('node-fetch'); +const { CookieJar } = require('tough-cookie'); -const fetch = makeFetchCookie(fetchOriginal); +const cookieJar = new CookieJar(); +const fetch = makeFetchCookie(fetchOriginal, cookieJar); let agent = null; @@ -85,6 +87,10 @@ class APIRequest { 'User-Agent': this.client.options.http.headers['User-Agent'], }; } + if (captchaKey && typeof captchaKey == 'string') { + headers['x-captcha-key'] = captchaKey; + if (captchaRqtoken) headers['x-captcha-rqtoken'] = captchaRqtoken; + } let body; if (this.options.files?.length) { @@ -98,29 +104,18 @@ class APIRequest { } else { body.append('payload_json', JSON.stringify(this.options.data)); } - } else if (typeof this.options.body !== 'undefined') { - if (this.options.dontUsePayloadJSON) { - for (const [key, value] of Object.entries(this.options.body)) body.append(key, value); - } else { - body.append('payload_json', JSON.stringify(this.options.body)); - } } headers = Object.assign(headers, body.getHeaders()); // eslint-disable-next-line eqeqeq } else if (this.options.data != null) { - headers['Content-Type'] = 'application/json'; - if (captchaKey && typeof captchaKey == 'string') { - if (!this.options.data) this.options.data = {}; - // Delete cookie (https://t.me/DMDGOBugsAndFeatures/626) Wtf Unknown Message Error ??? - headers.Cookie = undefined; - this.options.data.captcha_key = captchaKey; - if (captchaRqtoken) this.options.data.captcha_rqtoken = captchaRqtoken; + if (this.options.useFormDataPayloadJSON) { + body = new FormData(); + body.append('payload_json', JSON.stringify(this.options.data)); + headers = Object.assign(headers, body.getHeaders()); + } else { + body = JSON.stringify(this.options.data); + headers['Content-Type'] = 'application/json'; } - body = this.options.data ? JSON.stringify(this.options.data) : undefined; - } else if (this.options.body != null) { - body = new FormData(); - body.append('payload_json', JSON.stringify(this.options.body)); - headers = Object.assign(headers, body.getHeaders()); } const controller = new AbortController(); diff --git a/src/rest/RequestHandler.js b/src/rest/RequestHandler.js index 6e911f4..bc6d526 100644 --- a/src/rest/RequestHandler.js +++ b/src/rest/RequestHandler.js @@ -18,6 +18,7 @@ const captchaMessage = [ 'invalid-input-response', 'invalid-response', 'You need to update your app', + 'response-already-used-error', ]; function parseResponse(res) { diff --git a/src/structures/ApplicationCommand.js b/src/structures/ApplicationCommand.js index 3928744..15a6515 100644 --- a/src/structures/ApplicationCommand.js +++ b/src/structures/ApplicationCommand.js @@ -879,7 +879,8 @@ class ApplicationCommand extends Base { let nonce = SnowflakeUtil.generate(); const data = getDataPost(optionsData, nonce); await this.client.api.interactions.post({ - body: data, + data, + useFormDataPayloadJSON: true, files: attachmentsBuffer, }); this.client._interactionCache.set(nonce, { @@ -975,7 +976,8 @@ class ApplicationCommand extends Base { data.data.guild_id = message.guildId; } await this.client.api.interactions.post({ - body: data, + data, + useFormDataPayloadJSON: true, }); this.client._interactionCache.set(nonce, { channelId: message.channelId, diff --git a/src/util/Options.js b/src/util/Options.js index 6c27b22..f8dc807 100644 --- a/src/util/Options.js +++ b/src/util/Options.js @@ -205,7 +205,7 @@ class Options extends null { system_locale: 'en-US', browser_user_agent: defaultUA, browser_version: '22.3.2', - client_build_number: 199933, + client_build_number: 201332, native_build_number: 32266, client_event_source: null, design_id: 0,