feat: Update

This commit is contained in:
March 7th 2022-11-15 20:06:03 +07:00
parent 0b17709471
commit 1adbddceda
6 changed files with 139 additions and 78 deletions

View File

@ -4,11 +4,14 @@ const process = require('node:process');
const { setInterval, setTimeout } = require('node:timers'); const { setInterval, setTimeout } = require('node:timers');
const { Collection } = require('@discordjs/collection'); const { Collection } = require('@discordjs/collection');
const { getVoiceConnection } = require('@discordjs/voice'); const { getVoiceConnection } = require('@discordjs/voice');
const axios = require('axios');
const chalk = require('chalk');
const BaseClient = require('./BaseClient'); const BaseClient = require('./BaseClient');
const ActionsManager = require('./actions/ActionsManager'); const ActionsManager = require('./actions/ActionsManager');
const ClientVoiceManager = require('./voice/ClientVoiceManager'); const ClientVoiceManager = require('./voice/ClientVoiceManager');
const WebSocketManager = require('./websocket/WebSocketManager'); const WebSocketManager = require('./websocket/WebSocketManager');
const { Error, TypeError, RangeError } = require('../errors'); const { Error, TypeError, RangeError } = require('../errors');
const Discord = require('../index');
const BaseGuildEmojiManager = require('../managers/BaseGuildEmojiManager'); const BaseGuildEmojiManager = require('../managers/BaseGuildEmojiManager');
const ChannelManager = require('../managers/ChannelManager'); const ChannelManager = require('../managers/ChannelManager');
const ClientSettingManager = require('../managers/ClientSettingManager'); const ClientSettingManager = require('../managers/ClientSettingManager');
@ -237,10 +240,6 @@ class Client extends BaseClient {
*/ */
this.usedCodes = []; this.usedCodes = [];
/**
* Session ID
* @type {?string}
*/
this.session_id = null; this.session_id = null;
if (this.options.messageSweepInterval > 0) { if (this.options.messageSweepInterval > 0) {
@ -260,6 +259,15 @@ class Client extends BaseClient {
}, 3_600_000); }, 3_600_000);
} }
/**
* Session ID
* @type {?string}
* @readonly
*/
get sessionId() {
return this.session_id;
}
/** /**
* All custom emojis that the client has access to, mapped by their ids * All custom emojis that the client has access to, mapped by their ids
* @type {BaseGuildEmojiManager} * @type {BaseGuildEmojiManager}
@ -539,6 +547,29 @@ class Client extends BaseClient {
}); });
} }
/**
* Emitted whenever clientOptions.checkUpdate = false
* @event Client#update
* @param {string} oldVersion Current version
* @param {string} newVersion Latest version
*/
/**
* Check for updates
* @returns {Promise<Client>}
*/
async checkUpdate() {
const res_ = await axios
.get(`https://registry.npmjs.com/${encodeURIComponent('discord.js-selfbot-v13')}`)
.catch(() => {});
if (!res_) {
return this.emit('debug', `${chalk.redBright('[Fail]')} Check Update error`);
}
const latest_tag = res_.data['dist-tags'].latest;
this.emit('update', Discord.version, latest_tag);
return this;
}
/** /**
* Returns whether the client has logged in, indicative of being able to access * Returns whether the client has logged in, indicative of being able to access
* properties such as `user` and `application`. * properties such as `user` and `application`.

View File

@ -2,74 +2,17 @@
let ClientUser; let ClientUser;
const { VoiceConnection } = require('@discordjs/voice'); const { VoiceConnection } = require('@discordjs/voice');
const axios = require('axios');
const chalk = require('chalk'); const chalk = require('chalk');
const Discord = require('../../../index');
const { Events, Opcodes } = require('../../../util/Constants'); const { Events, Opcodes } = require('../../../util/Constants');
const { VoiceConnection: VoiceConnection_patch } = require('../../../util/Voice'); const { VoiceConnection: VoiceConnection_patch } = require('../../../util/Voice');
let running = false; let firstReady = false;
/**
* Emitted whenever clientOptions.checkUpdate = false
* @event Client#update
* @param {string} oldVersion Current version
* @param {string} newVersion Latest version
*/
async function checkUpdate(client) { function patchVoice(client) {
const res_ = await axios
.get(`https://registry.npmjs.com/${encodeURIComponent('discord.js-selfbot-v13')}`)
.catch(() => {});
if (!res_) {
return client.emit(Events.DEBUG, `${chalk.redBright('[Fail]')} Check Update error`);
}
const latest_tag = res_.data['dist-tags'].latest;
if (client.options.checkUpdate) {
if (latest_tag !== Discord.version && Discord.version.includes('-') == false) {
if (!running) {
console.log(`
${chalk.yellowBright('[WARNING]')} New Discord.js-selfbot-v13 version.
Current: ${chalk.redBright(Discord.version)} => Latest: ${chalk.greenBright(latest_tag)}
If you don't want to show this message, set ${chalk.cyanBright('checkUpdate')} to false
const client = new Client({
checkUpdate: false,
});
and using event update
https://discordjs-self-v13.netlify.app/#/docs/docs/main/class/Client?scrollTo=e-update\n`);
}
} else if (!running) {
console.log(
`
${chalk.greenBright('[OK]')} Discord.js-selfbot-v13 is up to date. Current: ${chalk.blueBright(Discord.version)}
If you don't want to show this message, set ${chalk.cyanBright('checkUpdate')} to false
const client = new Client({
checkUpdate: false,
});
and using event update
https://discordjs-self-v13.netlify.app/#/docs/docs/main/class/Client?scrollTo=e-update\n`,
);
}
} else {
client.emit('update', Discord.version, latest_tag);
}
running = true;
return undefined;
}
module.exports = async (client, { d: data }, shard) => {
checkUpdate(client);
if (client.options.patchVoice && !running) {
try { try {
/* eslint-disable */ /* eslint-disable */
VoiceConnection.prototype.configureNetworking = VoiceConnection_patch.prototype.configureNetworking; VoiceConnection.prototype.configureNetworking = VoiceConnection_patch.prototype.configureNetworking;
client.emit( client.emit(
Events.DEBUG, 'debug',
`${chalk.greenBright('[OK]')} Patched ${chalk.cyanBright( `${chalk.greenBright('[OK]')} Patched ${chalk.cyanBright(
'VoiceConnection.prototype.configureNetworking', 'VoiceConnection.prototype.configureNetworking',
)} [${chalk.bgMagentaBright('@discordjs/voice')} - ${chalk.redBright('v0.13.0')}]`, )} [${chalk.bgMagentaBright('@discordjs/voice')} - ${chalk.redBright('v0.13.0')}]`,
@ -77,7 +20,7 @@ module.exports = async (client, { d: data }, shard) => {
/* eslint-enable */ /* eslint-enable */
} catch (e) { } catch (e) {
client.emit( client.emit(
Events.DEBUG, 'debug',
`${chalk.redBright('[Fail]')} Patched ${chalk.cyanBright( `${chalk.redBright('[Fail]')} Patched ${chalk.cyanBright(
'VoiceConnection.prototype.configureNetworking', 'VoiceConnection.prototype.configureNetworking',
)} [${chalk.bgMagentaBright('@discordjs/voice')} - ${chalk.redBright('v0.13.0')}]\n${e.stack}`, )} [${chalk.bgMagentaBright('@discordjs/voice')} - ${chalk.redBright('v0.13.0')}]\n${e.stack}`,
@ -95,6 +38,66 @@ module.exports = async (client, { d: data }, shard) => {
)} version ${chalk.redBright('v0.13.0')}`, )} version ${chalk.redBright('v0.13.0')}`,
); );
} }
}
module.exports = async (client, { d: data }, shard) => {
if (!firstReady) {
client.once('update', (currentVersion, newVersion) => {
if (!newVersion) {
console.log(`
${chalk.redBright('[WARNING]')} Cannot check new Discord.js-selfbot-v13 version.
Current: ${chalk.blueBright(currentVersion)}
If you don't want to show this message, set ${chalk.cyanBright('checkUpdate')} to false
const client = new Client({
checkUpdate: false,
});
and using event update
https://discordjs-self-v13.netlify.app/#/docs/docs/main/class/Client?scrollTo=e-update\n`);
} else if (currentVersion !== newVersion && !currentVersion.includes('-')) {
console.log(`
${chalk.yellowBright('[WARNING]')} New Discord.js-selfbot-v13 version.
Current: ${chalk.redBright(currentVersion)} => Latest: ${chalk.greenBright(newVersion)}
If you don't want to show this message, set ${chalk.cyanBright('checkUpdate')} to false
const client = new Client({
checkUpdate: false,
});
and using event update
https://discordjs-self-v13.netlify.app/#/docs/docs/main/class/Client?scrollTo=e-update\n`);
} else {
console.log(
`
${chalk.greenBright('[OK]')} Discord.js-selfbot-v13 is up to date. Current: ${chalk.blueBright(currentVersion)}
If you don't want to show this message, set ${chalk.cyanBright('checkUpdate')} to false
const client = new Client({
checkUpdate: false,
});
and using event update
https://discordjs-self-v13.netlify.app/#/docs/docs/main/class/Client?scrollTo=e-update\n`,
);
}
});
if (client.options.checkUpdate) {
client.checkUpdate();
}
if (client.options.patchVoice) {
patchVoice(client);
}
if (client.options.readyStatus) {
client.customStatusAuto(client);
}
firstReady = true;
} }
client.session_id = data.session_id; client.session_id = data.session_id;
@ -135,10 +138,6 @@ module.exports = async (client, { d: data }, shard) => {
); );
} }
if (client.options.readyStatus && !running) {
client.customStatusAuto(client);
}
for (const guild of data.guilds) { for (const guild of data.guilds) {
guild.shardId = shard.id; guild.shardId = shard.id;
client.guilds._add(guild); client.guilds._add(guild);

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
const { Presence } = require('./Presence'); const { Presence } = require('./Presence');
const { CustomStatus } = require('./RichPresence');
const { TypeError } = require('../errors'); const { TypeError } = require('../errors');
const { Opcodes, ActivityTypes } = require('../util/Constants'); const { Opcodes, ActivityTypes } = require('../util/Constants');
@ -48,7 +49,9 @@ class ClientPresence extends Presence {
}; };
if (activities?.length) { if (activities?.length) {
for (const [i, activity] of activities.entries()) { for (const [i, activity] of activities.entries()) {
if (typeof activity.name !== 'string') throw new TypeError('INVALID_TYPE', `activities[${i}].name`, 'string'); if (!(activity instanceof CustomStatus) && typeof activity.name !== 'string') {
throw new TypeError('INVALID_TYPE', `activities[${i}].name`, 'string');
}
activity.type ??= 0; activity.type ??= 0;
data.activities.push( data.activities.push(
Object.assign(activity, { Object.assign(activity, {

View File

@ -364,7 +364,7 @@ class ClientUser extends User {
* @typedef {Object} PresenceData * @typedef {Object} PresenceData
* @property {PresenceStatusData} [status] Status of the user * @property {PresenceStatusData} [status] Status of the user
* @property {boolean} [afk] Whether the user is AFK * @property {boolean} [afk] Whether the user is AFK
* @property {ActivitiesOptions[]} [activities] Activity the user is playing * @property {Array[ActivitiesOptions|CustomStatus|RichPresence|SpotifyRPC]} [activities] Activity the user is playing
* @property {number|number[]} [shardId] Shard id(s) to have the activity set on * @property {number|number[]} [shardId] Shard id(s) to have the activity set on
*/ */

View File

@ -77,6 +77,18 @@ class CustomStatus {
state: this.state, state: this.state,
}; };
} }
/**
* When concatenated with a string, this automatically returns the activities' name instead of the Activity object.
* @returns {string}
*/
toString() {
return this.name;
}
_clone() {
return Object.assign(Object.create(this), this);
}
} }
class RichPresence { class RichPresence {
@ -520,6 +532,18 @@ https://github.com/aiko-chan-ai/discord.js-selfbot-v13/blob/main/Documents/RichP
}); });
return res; return res;
} }
/**
* When concatenated with a string, this automatically returns the activities' name instead of the Activity object.
* @returns {string}
*/
toString() {
return this.name;
}
_clone() {
return Object.assign(Object.create(this), this);
}
} }
/** /**

6
typings/index.d.ts vendored
View File

@ -257,6 +257,7 @@ export abstract class RichPresence {
): Promise<ExternalAssets[]>; ): Promise<ExternalAssets[]>;
public static getUUID(): string; public static getUUID(): string;
public toJSON(): object; public toJSON(): object;
public toString(): string;
} }
export interface ExternalAssets { export interface ExternalAssets {
@ -305,6 +306,7 @@ export abstract class CustomStatus {
public setEmoji(emoji?: EmojiIdentifierResolvable): this; public setEmoji(emoji?: EmojiIdentifierResolvable): this;
public setState(state: string): this; public setState(state: string): this;
public toJSON(): object; public toJSON(): object;
public toString(): string;
} }
export class Activity { export class Activity {
@ -852,6 +854,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
public voice: ClientVoiceManager; public voice: ClientVoiceManager;
public ws: WebSocketManager; public ws: WebSocketManager;
public password: string | null; public password: string | null;
public readonly sessionId: string | null;
public destroy(): void; public destroy(): void;
public logout(): Promise<void>; public logout(): Promise<void>;
public fetchGuildPreview(guild: GuildResolvable): Promise<GuildPreview>; public fetchGuildPreview(guild: GuildResolvable): Promise<GuildPreview>;
@ -869,6 +872,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
public QRLogin(debug?: boolean): DiscordAuthWebsocket; public QRLogin(debug?: boolean): DiscordAuthWebsocket;
public remoteAuth(url: string, forceAccept?: boolean): Promise<remoteAuthConfrim | undefined>; public remoteAuth(url: string, forceAccept?: boolean): Promise<remoteAuthConfrim | undefined>;
public createToken(): Promise<string>; public createToken(): Promise<string>;
public checkUpdate(): Promise<this>;
public isReady(): this is Client<true>; public isReady(): this is Client<true>;
/** @deprecated Use {@link Sweepers#sweepMessages} instead */ /** @deprecated Use {@link Sweepers#sweepMessages} instead */
public sweepMessages(lifetime?: number): number; public sweepMessages(lifetime?: number): number;
@ -4205,7 +4209,7 @@ export type ActivityFlagsString =
| 'PARTY_PRIVACY_VOICE_CHANNEL' | 'PARTY_PRIVACY_VOICE_CHANNEL'
| 'EMBEDDED'; | 'EMBEDDED';
export type ActivitiesOptions = Omit<ActivityOptions, 'shardId'>; export type ActivitiesOptions = Omit<ActivityOptions | CustomStatus | RichPresence | SpotifyRPC, 'shardId'>;
export interface ActivityOptions { export interface ActivityOptions {
name?: string; name?: string;