2026-05-12 19:38:23 +07:00
|
|
|
import fs from "fs";
|
|
|
|
|
import path from "path";
|
|
|
|
|
import { pipeline } from "stream/promises";
|
|
|
|
|
import {
|
|
|
|
|
EndBehaviorType,
|
|
|
|
|
joinVoiceChannel,
|
|
|
|
|
VoiceConnectionStatus,
|
|
|
|
|
entersState,
|
|
|
|
|
getVoiceConnection,
|
|
|
|
|
} from "@discordjs/voice";
|
|
|
|
|
import type { VoiceChannel, Client } from "discord.js-selfbot-v13";
|
|
|
|
|
import prism from "prism-media";
|
|
|
|
|
|
|
|
|
|
import { PacketFilter } from "./packetFilter";
|
|
|
|
|
import { config } from "./config";
|
|
|
|
|
const recordingsDir = process.env.RECORDINGS_DIR ?? "./recordings";
|
|
|
|
|
|
|
|
|
|
// Pastikan folder recordings ada
|
|
|
|
|
if (!fs.existsSync(recordingsDir)) {
|
|
|
|
|
fs.mkdirSync(recordingsDir, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Join ke voice channel dan mulai merekam semua user yang bicara.
|
|
|
|
|
*/
|
|
|
|
|
export async function startRecording(client: Client, channel: VoiceChannel): Promise<void> {
|
|
|
|
|
const connection = joinVoiceChannel({
|
|
|
|
|
channelId: channel.id,
|
|
|
|
|
guildId: channel.guild.id,
|
|
|
|
|
adapterCreator: channel.guild.voiceAdapterCreator as any,
|
|
|
|
|
selfDeaf: false,
|
2026-05-13 01:03:46 +07:00
|
|
|
selfMute: false,
|
2026-05-12 19:38:23 +07:00
|
|
|
debug: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (config.verbose) {
|
|
|
|
|
console.log(`[recorder] Joining voice channel: #${channel.name}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connection.on('debug', msg => {
|
|
|
|
|
if (config.verbose) {
|
|
|
|
|
console.log(`[voice-debug] ${msg}`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
connection.on('error', err => {
|
|
|
|
|
console.error(`[voice-error]`, err);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Tunggu sampai benar-benar terhubung
|
|
|
|
|
try {
|
|
|
|
|
await entersState(connection, VoiceConnectionStatus.Ready, 15_000);
|
|
|
|
|
if (config.verbose) {
|
|
|
|
|
console.log("[recorder] Connected to voice channel. Recording started.");
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("[recorder] Failed to connect:", err);
|
|
|
|
|
connection.destroy();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const receiver = connection.receiver;
|
|
|
|
|
|
|
|
|
|
// Dengarkan siapapun yang mulai bicara
|
|
|
|
|
receiver.speaking.on("start", async (userId) => {
|
|
|
|
|
// Coba ambil data user dari cache atau fetch dari API
|
|
|
|
|
const user = client.users.cache.get(userId) || await client.users.fetch(userId).catch(() => null);
|
|
|
|
|
const username = user?.username ?? "Unknown User";
|
2026-05-13 02:30:09 +07:00
|
|
|
const avatar = user?.displayAvatarURL({ format: 'png', size: 64 }) ?? "https://cdn.discordapp.com/embed/avatars/0.png";
|
2026-05-12 19:38:23 +07:00
|
|
|
|
|
|
|
|
// Tampilkan format "nama user [voice activity]"
|
|
|
|
|
console.log(`${username} [voice activity]`);
|
2026-05-13 02:30:09 +07:00
|
|
|
|
|
|
|
|
// Notify webserver
|
|
|
|
|
if ((global as any).updateActiveUser) {
|
|
|
|
|
(global as any).updateActiveUser(userId, { username, avatar, speaking: true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Jangan record kalau sudah ada stream aktif untuk user ini
|
|
|
|
|
if (receiver.subscriptions.has(userId)) return;
|
2026-05-12 19:38:23 +07:00
|
|
|
|
|
|
|
|
const timestamp = Date.now();
|
|
|
|
|
const userDir = path.join(recordingsDir, userId);
|
|
|
|
|
if (!fs.existsSync(userDir)) {
|
|
|
|
|
fs.mkdirSync(userDir, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
const filename = path.join(userDir, `${timestamp}.ogg`);
|
|
|
|
|
const jsonFilename = path.join(userDir, `${timestamp}.json`);
|
|
|
|
|
|
|
|
|
|
const audioStream = receiver.subscribe(userId, {
|
|
|
|
|
end: {
|
|
|
|
|
behavior: EndBehaviorType.AfterSilence,
|
2026-05-13 02:58:11 +07:00
|
|
|
duration: 3000,
|
2026-05-12 19:38:23 +07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try {
|
2026-05-13 02:30:09 +07:00
|
|
|
// --- OGG file recording (unchanged) ---
|
|
|
|
|
const packetFilterForOgg = new PacketFilter(8);
|
2026-05-12 19:38:23 +07:00
|
|
|
const oggStream = new prism.opus.OggLogicalBitstream({
|
2026-05-13 02:30:09 +07:00
|
|
|
opusHead: new prism.opus.OpusHead({ channelCount: 2, sampleRate: 48000 }),
|
|
|
|
|
pageSizeControl: { maxPackets: 10 },
|
|
|
|
|
crc: true,
|
2026-05-12 19:38:23 +07:00
|
|
|
});
|
|
|
|
|
const out = fs.createWriteStream(filename);
|
2026-05-13 02:30:09 +07:00
|
|
|
audioStream.pipe(packetFilterForOgg).pipe(oggStream).pipe(out);
|
|
|
|
|
|
2026-05-13 02:58:11 +07:00
|
|
|
// --- Web broadcast: prism decoder with auto-recreate on error ---
|
|
|
|
|
// Prism's Transform stream enters a dead error state after first bad packet.
|
|
|
|
|
// We recreate the decoder instance when this happens, so subsequent packets
|
|
|
|
|
// are decoded normally. Each packet failure is fully isolated.
|
|
|
|
|
function makePcmListener(onPcm: (pcm: Buffer) => void) {
|
|
|
|
|
const d = new prism.opus.Decoder({ frameSize: 960, channels: 2, rate: 48000 });
|
|
|
|
|
d.on('data', onPcm);
|
|
|
|
|
d.on('error', () => {
|
|
|
|
|
// Decoder is dead — swap to a fresh one
|
|
|
|
|
currentDecoder = makePcmListener(onPcm);
|
|
|
|
|
});
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handlePcm = (pcm: Buffer) => {
|
2026-05-13 02:30:09 +07:00
|
|
|
if (!(global as any).broadcastPcmToWeb) return;
|
2026-05-13 02:58:11 +07:00
|
|
|
// Downsample 48kHz stereo → 24kHz mono (left channel, every 2nd sample)
|
2026-05-13 02:30:09 +07:00
|
|
|
const outBuf = Buffer.alloc(pcm.length / 4);
|
|
|
|
|
for (let i = 0; i < outBuf.length / 2; i++) {
|
|
|
|
|
outBuf.writeInt16LE(pcm.readInt16LE(i * 8), i * 2);
|
|
|
|
|
}
|
|
|
|
|
(global as any).broadcastPcmToWeb(outBuf, userId);
|
2026-05-13 02:58:11 +07:00
|
|
|
};
|
2026-05-12 19:38:23 +07:00
|
|
|
|
2026-05-13 02:58:11 +07:00
|
|
|
let currentDecoder = makePcmListener(handlePcm);
|
|
|
|
|
|
|
|
|
|
// Feed Opus packets one-by-one
|
2026-05-13 02:30:09 +07:00
|
|
|
let packetCount = 0;
|
|
|
|
|
audioStream.on('data', (chunk: Buffer) => {
|
|
|
|
|
packetCount++;
|
|
|
|
|
if (packetCount <= 5) {
|
|
|
|
|
console.log(`[recorder] Pkt #${packetCount} from ${userId}: ${chunk.length}b | 0x${chunk.slice(0,4).toString('hex')}`);
|
|
|
|
|
}
|
2026-05-13 02:58:11 +07:00
|
|
|
if (chunk.length < 8) return; // skip tiny control/DTX packets
|
2026-05-13 02:30:09 +07:00
|
|
|
try {
|
2026-05-13 02:58:11 +07:00
|
|
|
currentDecoder.write(chunk);
|
|
|
|
|
} catch (_) {
|
|
|
|
|
currentDecoder = makePcmListener(handlePcm);
|
|
|
|
|
}
|
2026-05-13 02:30:09 +07:00
|
|
|
});
|
2026-05-12 19:38:23 +07:00
|
|
|
|
2026-05-13 02:30:09 +07:00
|
|
|
audioStream.on('end', () => {
|
|
|
|
|
if ((global as any).updateActiveUser) {
|
|
|
|
|
(global as any).updateActiveUser(userId, { username, avatar, speaking: false });
|
2026-05-13 00:32:27 +07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-12 19:38:23 +07:00
|
|
|
|
|
|
|
|
out.on('finish', async () => {
|
|
|
|
|
if (config.verbose) {
|
|
|
|
|
console.log(`[recorder] Saved: ${filename}`);
|
|
|
|
|
}
|
|
|
|
|
const endTime = Date.now();
|
|
|
|
|
|
|
|
|
|
const eventMetadata = {
|
|
|
|
|
userId,
|
|
|
|
|
username: user?.username ?? "Unknown User",
|
|
|
|
|
tag: user?.tag ?? "Unknown#0000",
|
|
|
|
|
startTime: timestamp,
|
|
|
|
|
endTime,
|
|
|
|
|
durationMs: endTime - timestamp,
|
|
|
|
|
filename: path.basename(filename)
|
|
|
|
|
};
|
|
|
|
|
fs.writeFileSync(jsonFilename, JSON.stringify(eventMetadata, null, 2));
|
|
|
|
|
if (config.verbose) {
|
|
|
|
|
console.log(`[recorder] Saved metadata: ${jsonFilename}`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
audioStream.on('error', (err) => {
|
|
|
|
|
console.error(`[recorder] Audio Stream error ${userId}:`, err.message);
|
|
|
|
|
});
|
2026-05-13 02:30:09 +07:00
|
|
|
packetFilterForOgg.on('error', (err) => {
|
|
|
|
|
console.error(`[recorder] PacketFilter(ogg) error ${userId}:`, err.message);
|
2026-05-12 19:38:23 +07:00
|
|
|
});
|
|
|
|
|
out.on('error', (err) => {
|
|
|
|
|
console.error(`[recorder] File write error ${userId}:`, err.message);
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(`[recorder] Failed to create stream for ${userId}:`, e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Handle disconnect yang tidak disengaja
|
|
|
|
|
connection.on(VoiceConnectionStatus.Disconnected, async () => {
|
|
|
|
|
if (config.verbose) {
|
|
|
|
|
console.warn("[recorder] Disconnected from voice channel. Reconnecting...");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
await Promise.race([
|
|
|
|
|
entersState(connection, VoiceConnectionStatus.Signalling, 5_000),
|
|
|
|
|
entersState(connection, VoiceConnectionStatus.Connecting, 5_000),
|
|
|
|
|
]);
|
|
|
|
|
// Berhasil reconnect
|
|
|
|
|
} catch {
|
|
|
|
|
console.error("[recorder] Could not reconnect. Destroying connection.");
|
|
|
|
|
connection.destroy();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
connection.on(VoiceConnectionStatus.Destroyed, () => {
|
|
|
|
|
if (config.verbose) {
|
|
|
|
|
console.log("[recorder] Voice connection destroyed.");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Hentikan recording dan disconnect dari voice channel.
|
|
|
|
|
*/
|
|
|
|
|
export function stopRecording(guildId: string): void {
|
|
|
|
|
const connection = getVoiceConnection(guildId);
|
|
|
|
|
if (connection) {
|
|
|
|
|
connection.destroy();
|
|
|
|
|
if (config.verbose) {
|
|
|
|
|
console.log("[recorder] Recording stopped and disconnected.");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
console.warn("[recorder] No active connection to stop.");
|
|
|
|
|
}
|
|
|
|
|
}
|