// class.js
const WebSocket = require("ws");
let wsCon = [];
function initializeWebSocket(token, options = {}) {
return new Promise((resolve, reject) => {
const gatewayUrl = "wss://gateway.discord.gg";
const intents = options.intents || 3276799;
let reconnectUrl = gatewayUrl;
let ws,
seq = -1,
sessionId = "";
const payload = {
op: 2,
d: {
token,
intents,
properties: {
$os: options.os || "windows",
$browser: options.browser || "Discord",
$device: options.device || "Desktop",
},
},
};
const activities = [
{
application_id: "356876176465199104",
name: "Grand Theft Auto V",
type: options.self_stream ? 1 : 0,
},
{
application_id: "401518684763586560",
name: "League of Legends",
type: options.self_stream ? 1 : 0,
details: "Sihirdar Vadisi (Normal)",
},
{
application_id: "382624125287399424",
name: "FiveM",
type: options.self_stream ? 1 : 0,
details: "In the menus",
},
{
application_id: "1121186262116737025",
name: "NovaRP",
type: options.self_stream ? 1 : 0,
state: "novarp.com.tr",
},
{
application_id: "378293683256164352",
name: "Rage Multiplayer",
type: options.self_stream ? 1 : 0,
details: "Jack_Evans",
},
{
application_id: "1221845803237244968",
name: "Racon Roleplay",
type: options.self_stream ? 1 : 0,
details: "Oyuncular: 185/512",
},
{
application_id: "535371564850479134",
name: "Sea of Thieves",
type: options.self_stream ? 1 : 0,
},
{
application_id: "363445589247131668",
name: "Roblox",
type: options.self_stream ? 1 : 0,
},
{
application_id: "700136079562375258",
name: "VALORANT",
type: options.self_stream ? 1 : 0,
},
];
const connectWebSocket = () => {
if (ws && ws.readyState === WebSocket.OPEN) return;
ws = new WebSocket(`${reconnectUrl}/?v=10&encoding=json`);
wsCon.push(ws);
ws.on("open", () => {
if (sessionId) {
ws.send(
JSON.stringify({ op: 6, d: { token, session_id: sessionId, seq } })
);
} else {
ws.send(JSON.stringify(payload));
}
});
ws.on("message", (data) => {
const { t, s, op, d } = JSON.parse(data);
if (s) seq = s;
if (op === 10) {
if (ws.readyState === WebSocket.OPEN) {
setInterval(
() => ws.send(JSON.stringify({ op: 1, d: seq })),
d.heartbeat_interval
);
}
}
switch (t) {
case "READY":
console.log(`[ws]: @${d.user.username} connected`);
reconnectUrl = d.resume_gateway_url;
sessionId = d.session_id;
if (options.channel_id) {
ws.send(
JSON.stringify({
op: 4,
d: {
guild_id: options.guild_id,
channel_id: options.channel_id,
self_mute: options.self_mute || true,
self_deaf: options.self_deaf || true,
self_video: options.self_video || false,
self_stream: options.self_stream || false,
},
})
);
}
resolve();
break;
case "RESUMED":
console.log(`[ws]: ${token} resumed!`);
break;
}
});
ws.on("close", () => {
console.log(`[ws]: @${token} closed! reconnecting...`);
setTimeout(connectWebSocket, 2500); bağlanma
});
ws.on("error", (err) => {
console.error(`[ws] ${token} err:`, err.stack);
});
};
connectWebSocket();
process.on("SIGINT", () => {
console.log(`[ws]: ${token} disconnecting...`);
wsCon.forEach((ws) => {
if (ws.readyState === WebSocket.OPEN) {
ws.close(1000, "terminated");
}
});
process.exit(0);
});
});
}
module.exports = initializeWebSocket;