From d900f7321c3a6b07f3b439325478d402d2a765ff Mon Sep 17 00:00:00 2001 From: March 7th <71698422+aiko-chan-ai@users.noreply.github.com> Date: Sun, 10 Apr 2022 19:03:40 +0700 Subject: [PATCH] Update 1. Fix Interaction with ephemeral message 2. client.updateCookie() 3. clientUser.findFriend('test', 1234) 4. WebEmbed not working, sad 5. Update Document later ... --- src/client/Client.js | 81 +++++++++++++----------- src/managers/ClientUserSettingManager.js | 9 +-- src/structures/ClientUser.js | 26 ++++++-- src/structures/MessageButton.js | 1 + src/structures/MessageSelectMenu.js | 35 +++++----- src/structures/WebEmbed.js | 7 +- typings/index.d.ts | 4 +- 7 files changed, 96 insertions(+), 67 deletions(-) diff --git a/src/client/Client.js b/src/client/Client.js index d34a3ee..8115c43 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -242,6 +242,49 @@ class Client extends BaseClient { return this.readyAt ? Date.now() - this.readyAt : null; } + /** + * Update Cloudflare Cookie and Discord Fingerprint + */ + async updateCookie() { + /* Auto find fingerprint and add Cookie */ + let cookie = ''; + await require('axios')({ + method: 'get', + url: 'https://discord.com/api/v9/experiments', + headers: this.options.http.headers, + }) + .then((res) => { + if (!'set-cookie' in res.headers) return; + res.headers['set-cookie'].map((line) => { + line.split('; ').map((arr) => { + if ( + arr.startsWith('Expires') || + arr.startsWith('Path') || + arr.startsWith('Domain') || + arr.startsWith('HttpOnly') || + arr.startsWith('Secure') || + arr.startsWith('Max-Age') || + arr.startsWith('SameSite') + ) { + return; + } else { + cookie += `${arr}; `; + } + }); + }); + this.options.http.headers['Cookie'] = `${cookie}locale=en`; + this.options.http.headers['x-fingerprint'] = res.data.fingerprint; + this.emit(Events.DEBUG, `Added Cookie: ${cookie}`); + this.emit(Events.DEBUG, `Added Fingerprint: ${res.data.fingerprint}`); + }) + .catch((err) => { + this.emit( + Events.DEBUG, + `Finding Cookie and Fingerprint failed: ${err.message}`, + ); + }); + } + /** * Logs the client in, establishing a WebSocket connection to Discord. * @param {string} [token=this.token] Token of the account to log in with @@ -261,43 +304,7 @@ class Client extends BaseClient { ); if (this.options.autoCookie) { - /* Auto find fingerprint and add Cookie */ - let cookie = ''; - await require('axios')({ - method: 'get', - url: 'https://discord.com/api/v9/experiments', - headers: this.options.http.headers, - }) - .then((res) => { - if (!'set-cookie' in res.headers) return; - res.headers['set-cookie'].map((line) => { - line.split('; ').map((arr) => { - if ( - arr.startsWith('Expires') || - arr.startsWith('Path') || - arr.startsWith('Domain') || - arr.startsWith('HttpOnly') || - arr.startsWith('Secure') || - arr.startsWith('Max-Age') || - arr.startsWith('SameSite') - ) { - return; - } else { - cookie += `${arr}; `; - } - }); - }); - this.options.http.headers['Cookie'] = `${cookie}locale=en`; - this.options.http.headers['x-fingerprint'] = res.data.fingerprint; - this.emit(Events.DEBUG, `Added Cookie: ${cookie}`); - this.emit(Events.DEBUG, `Added Fingerprint: ${res.data.fingerprint}`); - }) - .catch((err) => { - this.emit( - Events.DEBUG, - `Finding Cookie and Fingerprint failed: ${err.message}`, - ); - }); + await this.updateCookie(); } if (this.options.presence) { diff --git a/src/managers/ClientUserSettingManager.js b/src/managers/ClientUserSettingManager.js index 08dc09f..ebd3df8 100644 --- a/src/managers/ClientUserSettingManager.js +++ b/src/managers/ClientUserSettingManager.js @@ -1,17 +1,14 @@ 'use strict'; - -const CachedManager = require('./CachedManager'); const { default: Collection } = require('@discordjs/collection'); const { Error, TypeError } = require('../errors/DJSError'); const { remove } = require('lodash'); const { localeObject, DMScanLevel, stickerAnimationMode } = require('../util/Constants') /** - * Manages API methods for users and stores their cache. - * @extends {CachedManager} + * Manages API methods for users. */ -class ClientUserSettingManager extends CachedManager { +class ClientUserSettingManager { constructor(client, iterable) { - super(client); + this.client = client; // Raw data this.rawSetting = {}; // Language diff --git a/src/structures/ClientUser.js b/src/structures/ClientUser.js index 6cc15ef..67dab7d 100644 --- a/src/structures/ClientUser.js +++ b/src/structures/ClientUser.js @@ -156,9 +156,10 @@ class ClientUser extends User { async setHypeSquad(type) { const id = typeof type === 'string' ? HypeSquadOptions[type] : type; if (!id && id !== 0) throw new Error('Invalid HypeSquad type.'); - if (id !== 0) return await this.client.api.hypesquad.online.post({ - data: { house_id: id }, - }); + if (id !== 0) + return await this.client.api.hypesquad.online.post({ + data: { house_id: id }, + }); else return await this.client.api.hypesquad.online.delete(); } @@ -178,7 +179,8 @@ class ClientUser extends User { * @returns {Promise} */ setDiscriminator(discriminator, password) { - if (!this.nitro) throw new Error('You must be a Nitro User to change your discriminator.'); + if (!this.nitro) + throw new Error('You must be a Nitro User to change your discriminator.'); if (!password && !this.client.password) throw new Error('A password is required to change a discriminator.'); return this.edit({ @@ -352,6 +354,22 @@ class ClientUser extends User { setAFK(afk = true, shardId) { return this.setPresence({ afk, shardId }); } + + /** + * Send Friend Request to the user + * @returns {Promise} the user object + */ + async findFriend(username, discriminator) { + return await this.client.api + .users('@me') + .relationships.post({ + data: { + username: username, + discriminator: parseInt(discriminator), + }, + }) + .then((_) => _); + } } module.exports = ClientUser; diff --git a/src/structures/MessageButton.js b/src/structures/MessageButton.js index 828aa3d..9adb88a 100644 --- a/src/structures/MessageButton.js +++ b/src/structures/MessageButton.js @@ -183,6 +183,7 @@ class MessageButton extends BaseMessageComponent { component_type: 2, // Button custom_id: this.customId }, + message_flags: message.flags.bitfield, } } ) diff --git a/src/structures/MessageSelectMenu.js b/src/structures/MessageSelectMenu.js index da48910..ac75d58 100644 --- a/src/structures/MessageSelectMenu.js +++ b/src/structures/MessageSelectMenu.js @@ -231,24 +231,23 @@ class MessageSelectMenu extends BaseMessageComponent { return false; }) if (check_) throw new RangeError("[SELECT_MENU_INVALID_VALUE] The value " + check_ + " is invalid. Please use a valid value " + validValue.join(', ')); - await message.client.api.interactions.post( - { - data: { - type: 3, // ? - guild_id: message.guild?.id ?? null, // In DMs - channel_id: message.channel.id, - message_id: message.id, - application_id: message.author.id, - session_id: message.client.session_id, - data: { - component_type: 3, // Select Menu - custom_id: this.customId, - type: 3, // Select Menu - values, - }, - } - } - ) + await message.client.api.interactions.post({ + data: { + type: 3, // ? + guild_id: message.guild?.id ?? null, // In DMs + channel_id: message.channel.id, + message_id: message.id, + application_id: message.author.id, + session_id: message.client.session_id, + data: { + component_type: 3, // Select Menu + custom_id: this.customId, + type: 3, // Select Menu + values, + }, + message_flags: message.flags.bitfield, + }, + }); return true; } } diff --git a/src/structures/WebEmbed.js b/src/structures/WebEmbed.js index ad8b4f9..07d0627 100644 --- a/src/structures/WebEmbed.js +++ b/src/structures/WebEmbed.js @@ -1,13 +1,18 @@ 'use strict'; const axios = require('axios'); -const baseURL = 'https://embed.benny.fun/?'; +const baseURL = 'https://embed.benny.fun/?'; // error, not working .-. sad ... const hiddenCharter = '||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||'; const { RangeError } = require('../errors'); const Util = require('../util/Util'); +const process = require('node:process'); +const warn = true; class WebEmbed { constructor(data = {}) { + if (warn) process.emitWarning( + 'The WebEmbed constructor encountered a problem with the URL.', + ); this._setup(data); /** * Shorten the link diff --git a/typings/index.d.ts b/typings/index.d.ts index 3e0d974..b313fa8 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -550,6 +550,7 @@ export class Client extends BaseClient { public setting: ClientUserSettingManager; public friends: FriendsManager; public blocked: BlockedManager; + public updateCookie(): Promise; // End public channels: ChannelManager; public readonly emojis: BaseGuildEmojiManager; @@ -650,6 +651,7 @@ export class ClientUser extends User { public setPassword(oldPassword: string, newPassword: string): Promise; public disableAccount(password: string): Promise; public deleteAccount(password: string): Promise; + public findFriend(username: string, discriminator: string | number): Promise; // Selfbot public readonly nitro: boolean; /** @@ -3064,7 +3066,7 @@ export class ChannelManager extends CachedManager): Promise; } -export class ClientUserSettingManager extends CachedManager { +export class ClientUserSettingManager { private constructor(client: Client, iterable?: Iterable); public fetch(): Promise; public setDisplayCompactMode(value?: boolean): Promise;