Files
dc-recorder/src/index.ts

91 lines
2.3 KiB
TypeScript
Raw Normal View History

import "./mock-crc";
import "libsodium-wrappers";
import "@snazzah/davey";
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";
import { discordPlayer } from "./player";
2026-05-13 15:28:25 +07:00
import { startRecording } from "./recorder";
import { startWebserver } from "./webserver";
import { createChildLogger } from "./logger";
import { retryWithBackoff } from "./retry";
const logger = createChildLogger("bot");
2026-05-12 19:38:23 +07:00
// Validasi environment variables
const token = process.env.DISCORD_TOKEN;
const voiceChannelId = process.env.VOICE_CHANNEL_ID;
const guildId = process.env.GUILD_ID;
if (!token) throw new Error("Missing DISCORD_TOKEN in .env");
if (!voiceChannelId) throw new Error("Missing VOICE_CHANNEL_ID in .env");
if (!guildId) throw new Error("Missing GUILD_ID in .env");
// Inisialisasi selfbot client
2026-05-12 19:38:23 +07:00
const client = new Client();
client.on("ready", async () => {
2026-05-13 15:28:25 +07:00
if (config.verbose) {
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) {
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") {
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 15:28:25 +07:00
if (config.verbose) {
logger.info(
{ channelName: channel.name, channelId: channel.id },
"Joining voice channel",
2026-05-13 15:28:25 +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);
logger.info("Player connected to voice channel");
2026-05-13 15:28:25 +07:00
}
2026-05-13 15:28:25 +07:00
// Start Webserver
startWebserver(config.webserverPort);
2026-05-12 19:38:23 +07:00
});
client.on("error", (err) => {
logger.error({ error: err }, "Client error");
2026-05-12 19:38:23 +07:00
});
// Graceful shutdown
process.on("SIGINT", () => {
2026-05-13 15:28:25 +07:00
if (config.verbose) {
logger.info("Shutting down gracefully...");
2026-05-13 15:28:25 +07:00
}
client.destroy();
process.exit(0);
2026-05-12 19:38:23 +07:00
});
process.on("SIGTERM", () => {
2026-05-13 15:28:25 +07:00
if (config.verbose) {
logger.info("Terminating...");
2026-05-13 15:28:25 +07:00
}
client.destroy();
process.exit(0);
2026-05-12 19:38:23 +07:00
});
client.login(token);