Files
discord.js-selfbot/src/client/websocket/handlers/READY.js

169 lines
4.9 KiB
JavaScript
Raw Normal View History

2022-03-19 17:37:45 +07:00
'use strict';
let ClientUser;
2022-05-21 20:58:33 +07:00
const { VoiceConnection, VoiceConnectionStatus } = require('@discordjs/voice');
const axios = require('axios');
2022-04-16 19:01:05 +07:00
const chalk = require('chalk');
const Discord = require('../../../index');
2022-05-11 20:19:04 +07:00
const { Events, Opcodes } = require('../../../util/Constants');
2022-05-21 20:58:33 +07:00
const { Networking } = require('../../../util/Voice');
2022-07-16 15:31:44 +07:00
let running = false;
2022-06-13 23:53:43 +07:00
/**
* Emitted whenever clientOptions.checkUpdate = true
* @event Client#update
2022-06-25 10:28:31 +07:00
* @param {string} oldVersion Current version
* @param {string} newVersion Latest version
2022-06-13 23:53:43 +07:00
*/
async function checkUpdate(client) {
const res_ = await axios
.get(`https://registry.npmjs.com/${encodeURIComponent('discord.js-selfbot-v13')}`)
.catch(() => {});
if (!res_) {
2022-06-13 23:53:43 +07:00
return client.emit('update', `${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) {
2022-07-16 15:31:44 +07:00
if (!running) {
console.log(`
2022-06-25 10:28:31 +07:00
${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 \`checkUpdate\` to false
new Client({
checkUpdate: false,
});
2022-06-25 10:28:31 +07:00
and using event update
https://discordjs-self-v13.netlify.app/#/docs/docs/main/class/Client?scrollTo=e-update`);
2022-07-16 15:31:44 +07:00
}
} else if (!running) {
console.log(
2022-06-25 10:28:31 +07:00
`
${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 \`checkUpdate\` to false
new Client({
checkUpdate: false,
});
2022-06-25 10:28:31 +07:00
and using event update
https://discordjs-self-v13.netlify.app/#/docs/docs/main/class/Client?scrollTo=e-update`,
);
}
}
2022-07-16 15:50:48 +07:00
running = true;
return client.emit('update', Discord.version, latest_tag);
}
2022-03-19 17:37:45 +07:00
module.exports = async (client, { d: data }, shard) => {
checkUpdate(client);
2022-05-21 20:58:33 +07:00
2022-07-16 15:31:44 +07:00
if (client.options.patchVoice && !running) {
2022-05-21 20:58:33 +07:00
/* eslint-disable */
VoiceConnection.prototype.configureNetworking = function () {
const { server, state } = this.packets;
if (
!server ||
!state ||
this.state.status === VoiceConnectionStatus.Destroyed /* Destroyed */ ||
!server.endpoint
)
2022-06-26 09:40:04 +07:00
return;
const networking = new Networking(
{
endpoint: server.endpoint,
serverId: server.guild_id ?? server.channel_id,
token: server.token,
sessionId: state.session_id,
userId: state.user_id,
},
Boolean(this.debug),
);
networking.once('close', this.onNetworkingClose);
networking.on('stateChange', this.onNetworkingStateChange);
networking.on('error', this.onNetworkingError);
networking.on('debug', this.onNetworkingDebug);
2022-05-21 20:58:33 +07:00
this.state = {
...this.state,
2022-06-26 09:40:04 +07:00
status: VoiceConnectionStatus.Connecting /* Connecting */,
networking,
2022-05-21 20:58:33 +07:00
};
};
client.emit(
Events.DEBUG,
2022-08-03 19:47:37 +07:00
`${chalk.greenBright('[OK]')} Patched VoiceConnection.prototype.configureNetworking [@discordjs/voice - v0.11.0]`,
2022-05-21 20:58:33 +07:00
);
/* eslint-enable */
}
client.session_id = data.session_id;
if (client.user) {
client.user._patch(data.user);
} else {
ClientUser ??= require('../../../structures/ClientUser');
client.user = new ClientUser(client, data.user);
client.users.cache.set(client.user.id, client.user);
}
2022-03-19 17:37:45 +07:00
client.user.setAFK(false);
2022-03-19 17:37:45 +07:00
client.settings._patch(data.user_settings);
client.user.connectedAccounts = data.connected_accounts ?? [];
client.user._patchNote(data.notes);
2022-07-16 16:37:41 +07:00
const syncTime = Date.now();
for (const private_channel of data.private_channels) {
2022-07-16 16:37:41 +07:00
const channel = client.channels._add(private_channel);
// Rate limit warning
if (client.options.DMSync) {
client.ws.broadcast({
op: Opcodes.DM_UPDATE,
d: {
channel_id: channel.id,
},
});
}
}
if (client.options.DMSync) {
console.warn(
`Gateway Rate Limit Warning: Sending ${data.private_channels.length} Requests / ${Date.now() - syncTime || 1} ms`,
);
}
2022-07-16 15:31:44 +07:00
if (client.options.readyStatus && !running) {
client.customStatusAuto(client);
}
for (const guild of data.guilds) {
guild.shardId = shard.id;
client.guilds._add(guild);
}
2022-03-19 17:37:45 +07:00
// Receive messages in large guilds
for (const guild of data.guilds) {
await client.sleep(client.options.messageCreateEventGuildTimeout);
2022-05-11 20:19:04 +07:00
client.ws.broadcast({
op: Opcodes.LAZY_REQUEST,
d: {
guild_id: guild.id,
typing: true,
2022-08-09 10:43:16 +07:00
threads: true,
2022-05-11 20:19:04 +07:00
activities: true,
thread_member_lists: [],
members: [],
channels: {},
2022-05-11 20:19:04 +07:00
},
});
}
2022-05-11 20:19:04 +07:00
client.relationships._setup(data.relationships);
2022-03-19 17:37:45 +07:00
shard.checkReady();
2022-03-19 17:37:45 +07:00
};