Skip to content

Commit

Permalink
change getting the build number to a static function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevYellowy committed Apr 5, 2024
1 parent 7e24c6a commit 16d53fe
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/main.json

Large diffs are not rendered by default.

23 changes: 14 additions & 9 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,22 @@ class Client extends BaseClient {
return this.readyAt ? Date.now() - this.readyAt : null;
}

/** @private */
async updateClientBuildNumber() {
/**
* @param {"www" | "canary" | "ptb"} type The type of discord build number to get ( www = normal discord )
* @returns {Promise<number | null>}
*/
static async getClientBuildNumber(type = 'www') {
const BUILD_NUMBER_STRING = 'build_number:"';
const doc = await fetch('https://discord.com/app').then(r => r.text());
const doc = await fetch(`https://${type}.discord.com/app`).then(r => r.text());
const scripts = doc.match(/\/assets\/[0-9]{1,5}.*?.js/gim);
let number = null;

for (const script of scripts.reverse()) {
try {
const js = await fetch(`https://discord.com${script}`, {
const js = await fetch(`https://${type}.discord.com${script}`, {
headers: {
Origin: 'https://discord.com',
Referer: 'https://discord.com/app',
Origin: `https://${type}.discord.com`,
Referer: `https://${type}.discord.com/app`,
},
}).then(r => r.text());

Expand All @@ -254,14 +258,15 @@ class Client extends BaseClient {
if (end == -1) end = 10;

const build = js.slice(idx + BUILD_NUMBER_STRING.length, idx + BUILD_NUMBER_STRING.length + end);
const number = Number(build);
number = Number(build);

this.options.ws.properties.client_build_number = number;
break;
} catch (e) {
console.error(e);
break;
}
}

return number;
}

/**
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
private _eval(script: string): unknown;
private _validateOptions(options: ClientOptions): void;
private updateClientBuildNumber(): Promise<void>;
public static getClientBuildNumber(type: 'www' | 'ptb' | 'canary'): Promise<number | null>;
public channels: ChannelManager;
public readonly emojis: BaseGuildEmojiManager;
public guilds: GuildManager;
Expand Down

0 comments on commit 16d53fe

Please sign in to comment.