// class.js
const WebSocket = require('ws');
const Spotify = require('./spotify.json');
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 getSpotifyTrack = () => {
const track = Spotify[Math.floor(Math.random() * Spotify.length)];
const artist = track.track.album.artists.map(a => a.name).join(", ");
const album = track.track.album.name;
const image = track.track.album.images[0].url.replace("https://i.scdn.co/image/", "");
return {
name: track.track.name,
artist,
album,
image,
duration: track.track.duration_ms,
};
};
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.showActivity) {
const activityType = ["game", "spotify"][Math.floor(Math.random() * 2)];
if (activityType === 'game') {
const activity = activities[Math.floor(Math.random() * activities.length)];
ws.send(JSON.stringify({ op: 3, d: { since: Date.now(), activities: [activity], status: 'dnd', afk: false } }));
} else {
const Track = getSpotifyTrack();
ws.send(JSON.stringify({
op: 3, d: {
since: Date.now(), activities: [{
name: 'Spotify',
type: 2,
party: { id: `spotify:${d.user.id}` },
id: 'spotify:1',
flags: 48,
assets: { large_image: `spotify:${Track.image}`, large_text: Track.album },
state: Track.artist,
details: Track.name,
timestamps: { start: Date.now(), end: Date.now() + Track.duration }
}],
status: 'dnd', afk: false
}
}));
}
}
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);
});
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;