2026-05-13 01:12:11 +07:00
|
|
|
import "./mock-crc";
|
2026-05-13 02:30:09 +07:00
|
|
|
import "libsodium-wrappers";
|
|
|
|
|
import "@snazzah/davey";
|
2026-05-13 16:57:07 +07:00
|
|
|
import "dotenv/config";
|
2026-05-13 15:28:25 +07:00
|
|
|
import { getVoiceConnection } from "@discordjs/voice";
|
2026-05-12 19:38:23 +07:00
|
|
|
import { Client } from "discord.js-selfbot-v13";
|
|
|
|
|
import { config } from "./config";
|
2026-05-13 16:57:07 +07:00
|
|
|
import { createChildLogger } from "./logger";
|
2026-05-13 00:32:27 +07:00
|
|
|
import { discordPlayer } from "./player";
|
2026-05-13 16:32:14 +07:00
|
|
|
import { startRecording, stopRecording } from "./recorder";
|
2026-05-13 15:28:25 +07:00
|
|
|
import { startWebserver } from "./webserver";
|
2026-05-13 16:25:01 +07:00
|
|
|
|
|
|
|
|
const logger = createChildLogger("bot");
|
2026-05-12 19:38:23 +07:00
|
|
|
|
2026-05-13 16:57:07 +07:00
|
|
|
const token = config.DISCORD_TOKEN;
|
|
|
|
|
const voiceChannelId = config.VOICE_CHANNEL_ID;
|
|
|
|
|
const guildId = config.GUILD_ID;
|
2026-05-12 19:38:23 +07:00
|
|
|
|
2026-05-13 16:25:01 +07:00
|
|
|
// Inisialisasi selfbot client
|
2026-05-12 19:38:23 +07:00
|
|
|
const client = new Client();
|
|
|
|
|
|
2026-05-13 16:32:14 +07:00
|
|
|
// Track shutdown state
|
|
|
|
|
let isShuttingDown = false;
|
|
|
|
|
|
|
|
|
|
async function gracefulShutdown(signal: string) {
|
|
|
|
|
if (isShuttingDown) {
|
|
|
|
|
logger.warn(`Already shutting down, ignoring ${signal}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isShuttingDown = true;
|
|
|
|
|
logger.info({ signal }, "Graceful shutdown initiated");
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Step 1: Stop recording
|
|
|
|
|
if (guildId) {
|
|
|
|
|
logger.info("Stopping recording...");
|
|
|
|
|
stopRecording(guildId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 2: Pause player
|
|
|
|
|
logger.info("Pausing player...");
|
|
|
|
|
discordPlayer.pause();
|
|
|
|
|
|
|
|
|
|
// Step 3: Destroy voice connection
|
|
|
|
|
if (guildId) {
|
|
|
|
|
const connection = getVoiceConnection(guildId);
|
|
|
|
|
if (connection) {
|
|
|
|
|
logger.info("Destroying voice connection...");
|
|
|
|
|
try {
|
|
|
|
|
connection.destroy();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.warn({ error: err }, "Error destroying voice connection");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 4: Destroy client
|
|
|
|
|
logger.info("Destroying Discord client...");
|
|
|
|
|
try {
|
|
|
|
|
client.destroy();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.warn({ error: err }, "Error destroying client");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("Graceful shutdown completed");
|
|
|
|
|
process.exit(0);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error({ error: err }, "Error during graceful shutdown");
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-12 19:38:23 +07:00
|
|
|
client.on("ready", async () => {
|
2026-05-13 16:57:07 +07:00
|
|
|
if (config.VERBOSE) {
|
2026-05-13 16:25:01 +07:00
|
|
|
logger.info({ user: client.user?.tag }, "Bot logged in");
|
2026-05-13 15:28:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ambil guild
|
|
|
|
|
const guild = client.guilds.cache.get(guildId!);
|
|
|
|
|
if (!guild) {
|
2026-05-13 16:25:01 +07:00
|
|
|
logger.error({ guildId }, "Guild not found");
|
2026-05-13 15:28:25 +07:00
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2026-05-12 19:38:23 +07:00
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
// Fetch channels jika belum ada di cache
|
|
|
|
|
const channel =
|
|
|
|
|
guild.channels.cache.get(voiceChannelId!) ??
|
|
|
|
|
(await guild.channels.fetch(voiceChannelId!).catch(() => null));
|
2026-05-12 19:38:23 +07:00
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
if (!channel || channel.type !== "GUILD_VOICE") {
|
2026-05-13 16:25:01 +07:00
|
|
|
logger.error({ voiceChannelId }, "Voice channel not found or wrong type");
|
2026-05-13 15:28:25 +07:00
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2026-05-12 19:38:23 +07:00
|
|
|
|
2026-05-13 16:57:07 +07:00
|
|
|
if (config.VERBOSE) {
|
2026-05-13 16:25:01 +07:00
|
|
|
logger.info(
|
|
|
|
|
{ channelName: channel.name, channelId: channel.id },
|
|
|
|
|
"Joining voice channel",
|
2026-05-13 15:28:25 +07:00
|
|
|
);
|
|
|
|
|
}
|
2026-05-13 16:25:01 +07:00
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
await startRecording(client, channel as any);
|
2026-05-12 19:38:23 +07:00
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
// Set up player connection
|
|
|
|
|
const connection = getVoiceConnection(guildId!);
|
|
|
|
|
if (connection) {
|
|
|
|
|
discordPlayer.setConnection(connection);
|
2026-05-13 16:25:01 +07:00
|
|
|
logger.info("Player connected to voice channel");
|
2026-05-13 15:28:25 +07:00
|
|
|
}
|
2026-05-13 00:32:27 +07:00
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
// Start Webserver
|
2026-05-13 16:57:07 +07:00
|
|
|
startWebserver(config.WEBSERVER_PORT);
|
2026-05-12 19:38:23 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
client.on("error", (err) => {
|
2026-05-13 16:25:01 +07:00
|
|
|
logger.error({ error: err }, "Client error");
|
2026-05-12 19:38:23 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-13 16:32:14 +07:00
|
|
|
// Graceful shutdown handlers
|
2026-05-12 19:38:23 +07:00
|
|
|
process.on("SIGINT", () => {
|
2026-05-13 16:32:14 +07:00
|
|
|
gracefulShutdown("SIGINT");
|
2026-05-12 19:38:23 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
process.on("SIGTERM", () => {
|
2026-05-13 16:32:14 +07:00
|
|
|
gracefulShutdown("SIGTERM");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Handle uncaught exceptions
|
|
|
|
|
process.on("uncaughtException", (err) => {
|
|
|
|
|
logger.error({ error: err }, "Uncaught exception");
|
|
|
|
|
gracefulShutdown("uncaughtException");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Handle unhandled promise rejections
|
|
|
|
|
process.on("unhandledRejection", (reason, promise) => {
|
|
|
|
|
logger.error({ reason, promise }, "Unhandled rejection");
|
|
|
|
|
gracefulShutdown("unhandledRejection");
|
2026-05-12 19:38:23 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
client.login(token);
|