chore: add code quality tooling

This commit is contained in:
MythEclipse
2026-05-13 15:28:25 +07:00
parent 2676998411
commit 138aa397e2
15 changed files with 2023 additions and 891 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
// Configuration for the bot
export const config = {
verbose: process.argv.includes('-v') || process.argv.includes('--verbose'),
verbose: process.argv.includes("-v") || process.argv.includes("--verbose"),
};
+50 -48
View File
@@ -1,12 +1,12 @@
import "./mock-crc";
import "libsodium-wrappers";
import "@snazzah/davey";
import { Client } from "discord.js-selfbot-v13";
import { startRecording } from "./recorder";
import { config } from "./config";
import { startWebserver } from "./webserver";
import { discordPlayer } from "./player";
import { getVoiceConnection } from "@discordjs/voice";
import { Client } from "discord.js-selfbot-v13";
import { config } from "./config";
import { discordPlayer } from "./player";
import { startRecording } from "./recorder";
import { startWebserver } from "./webserver";
// Validasi environment variables
const token = process.env.DISCORD_TOKEN;
@@ -21,64 +21,66 @@ if (!guildId) throw new Error("Missing GUILD_ID in .env");
const client = new Client();
client.on("ready", async () => {
if (config.verbose) {
console.log(`[bot] Logged in as ${client.user!.tag}`);
}
if (config.verbose) {
console.log(`[bot] Logged in as ${client.user!.tag}`);
}
// Ambil guild
const guild = client.guilds.cache.get(guildId!);
if (!guild) {
console.error(`[bot] Guild not found: ${guildId}`);
process.exit(1);
}
// Ambil guild
const guild = client.guilds.cache.get(guildId!);
if (!guild) {
console.error(`[bot] Guild not found: ${guildId}`);
process.exit(1);
}
// Fetch channels jika belum ada di cache
const channel =
guild.channels.cache.get(voiceChannelId!) ??
(await guild.channels.fetch(voiceChannelId!).catch(() => null));
// Fetch channels jika belum ada di cache
const channel =
guild.channels.cache.get(voiceChannelId!) ??
(await guild.channels.fetch(voiceChannelId!).catch(() => null));
if (!channel || channel.type !== "GUILD_VOICE") {
console.error(
`[bot] Voice channel not found or wrong type: ${voiceChannelId}`
);
process.exit(1);
}
if (!channel || channel.type !== "GUILD_VOICE") {
console.error(
`[bot] Voice channel not found or wrong type: ${voiceChannelId}`,
);
process.exit(1);
}
if (config.verbose) {
console.log(`[bot] Joining voice channel: #${channel.name} (${channel.id})`);
}
await startRecording(client, channel as any);
// Set up player connection
const connection = getVoiceConnection(guildId!);
if (connection) {
discordPlayer.setConnection(connection);
console.log("[bot] Player connected to voice channel");
}
if (config.verbose) {
console.log(
`[bot] Joining voice channel: #${channel.name} (${channel.id})`,
);
}
await startRecording(client, channel as any);
// Start Webserver
startWebserver(3000);
// Set up player connection
const connection = getVoiceConnection(guildId!);
if (connection) {
discordPlayer.setConnection(connection);
console.log("[bot] Player connected to voice channel");
}
// Start Webserver
startWebserver(3000);
});
client.on("error", (err) => {
console.error("[bot] Client error:", err);
console.error("[bot] Client error:", err);
});
// Graceful shutdown
process.on("SIGINT", () => {
if (config.verbose) {
console.log("\n[bot] Shutting down...");
}
client.destroy();
process.exit(0);
if (config.verbose) {
console.log("\n[bot] Shutting down...");
}
client.destroy();
process.exit(0);
});
process.on("SIGTERM", () => {
if (config.verbose) {
console.log("[bot] Terminating...");
}
client.destroy();
process.exit(0);
if (config.verbose) {
console.log("[bot] Terminating...");
}
client.destroy();
process.exit(0);
});
client.login(token);
+32 -21
View File
@@ -1,31 +1,42 @@
// Mock node-crc to provide pure JS implementation and bypass native build issues
const CRC_TABLE = new Uint32Array(256);
for (let i = 0; i < 256; i++) {
let r = i << 24;
for (let j = 0; j < 8; j++) {
r = (r & 0x80000000) !== 0 ? ((r << 1) ^ 0x04c11db7) : (r << 1);
}
CRC_TABLE[i] = (r >>> 0);
let r = i << 24;
for (let j = 0; j < 8; j++) {
r = (r & 0x80000000) !== 0 ? (r << 1) ^ 0x04c11db7 : r << 1;
}
CRC_TABLE[i] = r >>> 0;
}
const Module = require('module');
const Module = require("module");
const originalRequire = Module.prototype.require;
Module.prototype.require = function (id: string) {
if (id === 'node-crc') {
return {
crc: function (width: number, reflectIn: boolean, poly: number, init: number, refOut: boolean, xorOut: number, unk1: number, unk2: number, buffer: Buffer) {
let crc = 0;
for (let i = 0; i < buffer.length; i++) {
crc = ((crc << 8) >>> 0) ^ CRC_TABLE[((crc >>> 24) ^ buffer[i]) & 0xff];
crc >>>= 0;
}
const result = Buffer.alloc(4);
result.writeUInt32BE(crc, 0);
return result;
}
};
}
return originalRequire.apply(this, arguments);
if (id === "node-crc") {
return {
crc: function (
width: number,
reflectIn: boolean,
poly: number,
init: number,
refOut: boolean,
xorOut: number,
unk1: number,
unk2: number,
buffer: Buffer,
) {
let crc = 0;
for (let i = 0; i < buffer.length; i++) {
crc =
((crc << 8) >>> 0) ^ CRC_TABLE[((crc >>> 24) ^ buffer[i]) & 0xff];
crc >>>= 0;
}
const result = Buffer.alloc(4);
result.writeUInt32BE(crc, 0);
return result;
},
};
}
return originalRequire.apply(this, arguments);
};
console.log("[mock] node-crc has been mocked globally.");
+209 -174
View File
@@ -1,178 +1,212 @@
import ffmpeg from "fluent-ffmpeg";
import fs from "fs";
import path from "path";
import ffmpeg from "fluent-ffmpeg";
const recordingsDir = process.env.RECORDINGS_DIR ?? "./recordings";
interface EventMetadata {
userId: string;
username: string;
tag: string;
displayName?: string;
avatarUrl?: string;
bot?: boolean;
roles?: Array<{ id: string; name: string; position: number }>;
highestRole?: { id: string; name: string; position: number } | null;
joinedTimestamp?: number | null;
sessionId?: string;
sessionStartTime?: number;
segmentIndex?: number;
segmentMs?: number;
startTime: number;
endTime: number;
durationMs: number;
filename: string;
userId: string;
username: string;
tag: string;
displayName?: string;
avatarUrl?: string;
bot?: boolean;
roles?: Array<{ id: string; name: string; position: number }>;
highestRole?: { id: string; name: string; position: number } | null;
joinedTimestamp?: number | null;
sessionId?: string;
sessionStartTime?: number;
segmentIndex?: number;
segmentMs?: number;
startTime: number;
endTime: number;
durationMs: number;
filename: string;
}
interface ClipInfo {
oggPath: string;
jsonPath: string;
meta: EventMetadata;
oggPath: string;
jsonPath: string;
meta: EventMetadata;
}
async function startMuxingToAup3() {
console.log("[muxer-aup3] Scanning recordings directory...");
if (!fs.existsSync(recordingsDir)) {
console.error("[muxer-aup3] Recordings directory not found.");
return;
}
console.log("[muxer-aup3] Scanning recordings directory...");
if (!fs.existsSync(recordingsDir)) {
console.error("[muxer-aup3] Recordings directory not found.");
return;
}
const clips: ClipInfo[] = [];
const clips: ClipInfo[] = [];
// Scan user directories
const items = fs.readdirSync(recordingsDir);
console.log(`[muxer-aup3] Found ${items.length} directories to scan...`);
let processedDirs = 0;
for (const item of items) {
const itemPath = path.join(recordingsDir, item);
if (fs.statSync(itemPath).isDirectory()) {
const files = fs.readdirSync(itemPath);
for (const file of files) {
if (file.endsWith(".json")) {
const jsonPath = path.join(itemPath, file);
const oggPath = jsonPath.replace(/\.json$/, ".ogg");
if (fs.existsSync(oggPath)) {
try {
// Check if OGG file is valid (not empty and has reasonable size)
const oggStats = fs.statSync(oggPath);
if (oggStats.size === 0) {
console.warn(`[muxer-aup3] Skipping empty OGG file: ${oggPath}`);
continue;
}
// Skip files that are too small (less than 1KB likely corrupted)
if (oggStats.size < 1024) {
console.warn(`[muxer-aup3] Skipping too small OGG file (${oggStats.size} bytes): ${oggPath}`);
continue;
}
// Check if OGG file has valid header (starts with "OggS")
const oggBuffer = fs.readFileSync(oggPath);
const oggHeader = oggBuffer.slice(0, 4).toString();
if (oggHeader !== "OggS") {
console.warn(`[muxer-aup3] Skipping invalid OGG file (bad header): ${oggPath}`);
continue;
}
const meta: EventMetadata = JSON.parse(fs.readFileSync(jsonPath, "utf-8"));
clips.push({ oggPath, jsonPath, meta });
} catch (e) {
console.error(`[muxer-aup3] Failed to read/parse JSON: ${jsonPath}`, e);
}
}
}
// Scan user directories
const items = fs.readdirSync(recordingsDir);
console.log(`[muxer-aup3] Found ${items.length} directories to scan...`);
let processedDirs = 0;
for (const item of items) {
const itemPath = path.join(recordingsDir, item);
if (fs.statSync(itemPath).isDirectory()) {
const files = fs.readdirSync(itemPath);
for (const file of files) {
if (file.endsWith(".json")) {
const jsonPath = path.join(itemPath, file);
const oggPath = jsonPath.replace(/\.json$/, ".ogg");
if (fs.existsSync(oggPath)) {
try {
// Check if OGG file is valid (not empty and has reasonable size)
const oggStats = fs.statSync(oggPath);
if (oggStats.size === 0) {
console.warn(
`[muxer-aup3] Skipping empty OGG file: ${oggPath}`,
);
continue;
}
// Skip files that are too small (less than 1KB likely corrupted)
if (oggStats.size < 1024) {
console.warn(
`[muxer-aup3] Skipping too small OGG file (${oggStats.size} bytes): ${oggPath}`,
);
continue;
}
// Check if OGG file has valid header (starts with "OggS")
const oggBuffer = fs.readFileSync(oggPath);
const oggHeader = oggBuffer.slice(0, 4).toString();
if (oggHeader !== "OggS") {
console.warn(
`[muxer-aup3] Skipping invalid OGG file (bad header): ${oggPath}`,
);
continue;
}
const meta: EventMetadata = JSON.parse(
fs.readFileSync(jsonPath, "utf-8"),
);
clips.push({ oggPath, jsonPath, meta });
} catch (e) {
console.error(
`[muxer-aup3] Failed to read/parse JSON: ${jsonPath}`,
e,
);
}
processedDirs++;
const progress = ((processedDirs / items.length) * 100).toFixed(2);
console.log(`[muxer-aup3] Scanning progress: ${progress}% (${processedDirs}/${items.length} directories)`);
}
}
}
processedDirs++;
const progress = ((processedDirs / items.length) * 100).toFixed(2);
console.log(
`[muxer-aup3] Scanning progress: ${progress}% (${processedDirs}/${items.length} directories)`,
);
}
}
if (clips.length === 0) {
console.log("[muxer-aup3] No recording clips found to mux.");
return;
}
if (clips.length === 0) {
console.log("[muxer-aup3] No recording clips found to mux.");
return;
}
// Sort by startTime so chronologically they are in order
clips.sort((a, b) => a.meta.startTime - b.meta.startTime);
// Sort by startTime so chronologically they are in order
clips.sort((a, b) => a.meta.startTime - b.meta.startTime);
// Find the global start time
const globalStartTime = clips[0].meta.startTime;
console.log(`[muxer-aup3] Found ${clips.length} clips. Base timestamp: ${globalStartTime}`);
// Find the global start time
const globalStartTime = clips[0].meta.startTime;
console.log(
`[muxer-aup3] Found ${clips.length} clips. Base timestamp: ${globalStartTime}`,
);
const command = ffmpeg();
const filterParts: string[] = [];
const command = ffmpeg();
const filterParts: string[] = [];
console.log(`[muxer-aup3] Creating audio filters for ${clips.length} clips...`);
clips.forEach((clip, index) => {
command.input(clip.oggPath);
console.log(
`[muxer-aup3] Creating audio filters for ${clips.length} clips...`,
);
clips.forEach((clip, index) => {
command.input(clip.oggPath);
// Calculate delay relative to the global start time
const delayMs = clip.meta.startTime - globalStartTime;
// Calculate delay relative to the global start time
const delayMs = clip.meta.startTime - globalStartTime;
// FFmpeg filter structure: [0:a]adelay=1000|1000[a0]
// Setting adelay multiple times covers stereo channels.
// We ensure all multiple channels get delayed.
const inputSpecifier = `[${index}:a]`;
const outputSpecifier = `[pad${index}]`;
// FFmpeg filter structure: [0:a]adelay=1000|1000[a0]
// Setting adelay multiple times covers stereo channels.
// We ensure all multiple channels get delayed.
const inputSpecifier = `[${index}:a]`;
const outputSpecifier = `[pad${index}]`;
filterParts.push(`${inputSpecifier}adelay=${delayMs}|${delayMs}${outputSpecifier}`);
const progress = (((index + 1) / clips.length) * 100).toFixed(2);
console.log(`[muxer-aup3] Filter creation progress: ${progress}% (${index + 1}/${clips.length} clips)`);
filterParts.push(
`${inputSpecifier}adelay=${delayMs}|${delayMs}${outputSpecifier}`,
);
const progress = (((index + 1) / clips.length) * 100).toFixed(2);
console.log(
`[muxer-aup3] Filter creation progress: ${progress}% (${index + 1}/${clips.length} clips)`,
);
});
// Merge them using amix
const amixInputs = clips.map((_, i) => `[pad${i}]`).join("");
// We add the amix command. dropout_transition=0 avoids volume drop when streams end.
filterParts.push(
`${amixInputs}amix=inputs=${clips.length}:dropout_transition=0[out]`,
);
const timestamp = Date.now();
const wavFilename = path.join(recordingsDir, `muxed-${timestamp}.wav`);
const aup3Filename = path.join(recordingsDir, `muxed-${timestamp}.aup3`);
console.log(
`[muxer-aup3] Combining clips to WAV. This might take a while...`,
);
// Using fluent-ffmpeg's complexFilter
command
.complexFilter(filterParts, "out")
.audioCodec("pcm_s16le")
.audioFrequency(44100)
.audioChannels(2)
.save(wavFilename)
.on("progress", (progress) => {
if (progress.percent) {
console.log(
`[muxer-aup3] WAV Progress: ${progress.percent.toFixed(2)}%`,
);
}
})
.on("end", () => {
console.log(`[muxer-aup3] WAV file created: ${wavFilename}`);
console.log(`[muxer-aup3] Creating AUP3 project file...`);
createAup3Project(wavFilename, aup3Filename, clips, globalStartTime);
})
.on("error", (err) => {
console.error(`[muxer-aup3] FFmpeg Error:`, err);
});
// Merge them using amix
const amixInputs = clips.map((_, i) => `[pad${i}]`).join("");
// We add the amix command. dropout_transition=0 avoids volume drop when streams end.
filterParts.push(`${amixInputs}amix=inputs=${clips.length}:dropout_transition=0[out]`);
const timestamp = Date.now();
const wavFilename = path.join(recordingsDir, `muxed-${timestamp}.wav`);
const aup3Filename = path.join(recordingsDir, `muxed-${timestamp}.aup3`);
console.log(`[muxer-aup3] Combining clips to WAV. This might take a while...`);
// Using fluent-ffmpeg's complexFilter
command
.complexFilter(filterParts, "out")
.audioCodec("pcm_s16le")
.audioFrequency(44100)
.audioChannels(2)
.save(wavFilename)
.on("progress", (progress) => {
if (progress.percent) {
console.log(`[muxer-aup3] WAV Progress: ${progress.percent.toFixed(2)}%`);
}
})
.on("end", () => {
console.log(`[muxer-aup3] WAV file created: ${wavFilename}`);
console.log(`[muxer-aup3] Creating AUP3 project file...`);
createAup3Project(wavFilename, aup3Filename, clips, globalStartTime);
})
.on("error", (err) => {
console.error(`[muxer-aup3] FFmpeg Error:`, err);
});
}
function createAup3Project(wavFilename: string, aup3Filename: string, clips: ClipInfo[], globalStartTime: number) {
try {
console.log(`[muxer-aup3] AUP3 Progress: Reading WAV file...`);
// Read WAV file to get duration
const wavStats = fs.statSync(wavFilename);
const wavSize = wavStats.size;
// Calculate approximate duration (assuming 44.1kHz, 16-bit, stereo)
// Duration = (file_size - 44) / (44100 * 2 * 2) for WAV
const duration = (wavSize - 44) / (44100 * 4);
console.log(`[muxer-aup3] AUP3 Progress: Calculating duration... ${duration.toFixed(2)}s`);
console.log(`[muxer-aup3] AUP3 Progress: Creating XML structure...`);
// Create AUP3 project XML structure
const aup3Content = `<?xml version="1.0" encoding="UTF-8"?>
function createAup3Project(
wavFilename: string,
aup3Filename: string,
clips: ClipInfo[],
globalStartTime: number,
) {
try {
console.log(`[muxer-aup3] AUP3 Progress: Reading WAV file...`);
// Read WAV file to get duration
const wavStats = fs.statSync(wavFilename);
const wavSize = wavStats.size;
// Calculate approximate duration (assuming 44.1kHz, 16-bit, stereo)
// Duration = (file_size - 44) / (44100 * 2 * 2) for WAV
const duration = (wavSize - 44) / (44100 * 4);
console.log(
`[muxer-aup3] AUP3 Progress: Calculating duration... ${duration.toFixed(2)}s`,
);
console.log(`[muxer-aup3] AUP3 Progress: Creating XML structure...`);
// Create AUP3 project XML structure
const aup3Content = `<?xml version="1.0" encoding="UTF-8"?>
<audacityproject xmlns="http://audacity.sourceforge.net/xml/" projname="muxed" version="1.3.0" audacityversion="3.5.1">
<tags>
<tag name="GENRE" value=""/>
@@ -197,39 +231,40 @@ function createAup3Project(wavFilename: string, aup3Filename: string, clips: Cli
</timetrack>
</audacityproject>`;
console.log(`[muxer-aup3] AUP3 Progress: Writing AUP3 file...`);
// Write AUP3 file
fs.writeFileSync(aup3Filename, aup3Content, "utf-8");
console.log(`[muxer-aup3] AUP3 Progress: Creating clip info file...`);
// Create a simple info file with clip details
const infoFilename = aup3Filename.replace('.aup3', '-info.txt');
const infoContent = clips.map((clip, index) => {
const delayMs = clip.meta.startTime - globalStartTime;
return `Clip ${index + 1}:
console.log(`[muxer-aup3] AUP3 Progress: Writing AUP3 file...`);
// Write AUP3 file
fs.writeFileSync(aup3Filename, aup3Content, "utf-8");
console.log(`[muxer-aup3] AUP3 Progress: Creating clip info file...`);
// Create a simple info file with clip details
const infoFilename = aup3Filename.replace(".aup3", "-info.txt");
const infoContent = clips
.map((clip, index) => {
const delayMs = clip.meta.startTime - globalStartTime;
return `Clip ${index + 1}:
User: ${clip.meta.username} (${clip.meta.userId})
Tag: ${clip.meta.tag}
Start Time: ${new Date(clip.meta.startTime).toISOString()}
Delay: ${delayMs}ms
Duration: ${clip.meta.durationMs}ms
File: ${path.basename(clip.oggPath)}`;
}).join('\n\n');
fs.writeFileSync(infoFilename, infoContent, "utf-8");
console.log(`[muxer-aup3] AUP3 Progress: 100% - Complete!`);
console.log(`[muxer-aup3] Successfully created AUP3 project!`);
console.log(`[muxer-aup3] WAV file: ${wavFilename}`);
console.log(`[muxer-aup3] AUP3 file: ${aup3Filename}`);
console.log(`[muxer-aup3] Clip info saved to: ${infoFilename}`);
console.log(`[muxer-aup3] Total clips processed: ${clips.length}`);
console.log(`[muxer-aup3] Duration: ${duration.toFixed(2)} seconds`);
} catch (error) {
console.error(`[muxer-aup3] Error creating AUP3 project:`, error);
}
})
.join("\n\n");
fs.writeFileSync(infoFilename, infoContent, "utf-8");
console.log(`[muxer-aup3] AUP3 Progress: 100% - Complete!`);
console.log(`[muxer-aup3] Successfully created AUP3 project!`);
console.log(`[muxer-aup3] WAV file: ${wavFilename}`);
console.log(`[muxer-aup3] AUP3 file: ${aup3Filename}`);
console.log(`[muxer-aup3] Clip info saved to: ${infoFilename}`);
console.log(`[muxer-aup3] Total clips processed: ${clips.length}`);
console.log(`[muxer-aup3] Duration: ${duration.toFixed(2)} seconds`);
} catch (error) {
console.error(`[muxer-aup3] Error creating AUP3 project:`, error);
}
}
startMuxingToAup3();
+145 -124
View File
@@ -1,153 +1,174 @@
import ffmpeg from "fluent-ffmpeg";
import fs from "fs";
import path from "path";
import ffmpeg from "fluent-ffmpeg";
const recordingsDir = process.env.RECORDINGS_DIR ?? "./recordings";
interface EventMetadata {
userId: string;
username: string;
tag: string;
displayName?: string;
avatarUrl?: string;
bot?: boolean;
roles?: Array<{ id: string; name: string; position: number }>;
highestRole?: { id: string; name: string; position: number } | null;
joinedTimestamp?: number | null;
sessionId?: string;
sessionStartTime?: number;
segmentIndex?: number;
segmentMs?: number;
startTime: number;
endTime: number;
durationMs: number;
filename: string;
userId: string;
username: string;
tag: string;
displayName?: string;
avatarUrl?: string;
bot?: boolean;
roles?: Array<{ id: string; name: string; position: number }>;
highestRole?: { id: string; name: string; position: number } | null;
joinedTimestamp?: number | null;
sessionId?: string;
sessionStartTime?: number;
segmentIndex?: number;
segmentMs?: number;
startTime: number;
endTime: number;
durationMs: number;
filename: string;
}
interface ClipInfo {
oggPath: string;
jsonPath: string;
meta: EventMetadata;
oggPath: string;
jsonPath: string;
meta: EventMetadata;
}
async function startMuxing() {
console.log("[muxer] Scanning recordings directory...");
if (!fs.existsSync(recordingsDir)) {
console.error("[muxer] Recordings directory not found.");
return;
}
console.log("[muxer] Scanning recordings directory...");
if (!fs.existsSync(recordingsDir)) {
console.error("[muxer] Recordings directory not found.");
return;
}
const clips: ClipInfo[] = [];
const clips: ClipInfo[] = [];
// Scan user directories
const items = fs.readdirSync(recordingsDir);
console.log(`[muxer] Found ${items.length} directories to scan...`);
let processedDirs = 0;
for (const item of items) {
const itemPath = path.join(recordingsDir, item);
if (fs.statSync(itemPath).isDirectory()) {
const files = fs.readdirSync(itemPath);
for (const file of files) {
if (file.endsWith(".json")) {
const jsonPath = path.join(itemPath, file);
const oggPath = jsonPath.replace(/\.json$/, ".ogg");
if (fs.existsSync(oggPath)) {
try {
// Check if OGG file is valid (not empty and has reasonable size)
const oggStats = fs.statSync(oggPath);
if (oggStats.size === 0) {
console.warn(`[muxer] Skipping empty OGG file: ${oggPath}`);
continue;
}
// Skip files that are too small (less than 1KB likely corrupted)
if (oggStats.size < 1024) {
console.warn(`[muxer] Skipping too small OGG file (${oggStats.size} bytes): ${oggPath}`);
continue;
}
// Check if OGG file has valid header (starts with "OggS")
const oggBuffer = fs.readFileSync(oggPath);
const oggHeader = oggBuffer.slice(0, 4).toString();
if (oggHeader !== "OggS") {
console.warn(`[muxer] Skipping invalid OGG file (bad header): ${oggPath}`);
continue;
}
const meta: EventMetadata = JSON.parse(fs.readFileSync(jsonPath, "utf-8"));
clips.push({ oggPath, jsonPath, meta });
} catch (e) {
console.error(`[muxer] Failed to read/parse JSON: ${jsonPath}`, e);
}
}
}
// Scan user directories
const items = fs.readdirSync(recordingsDir);
console.log(`[muxer] Found ${items.length} directories to scan...`);
let processedDirs = 0;
for (const item of items) {
const itemPath = path.join(recordingsDir, item);
if (fs.statSync(itemPath).isDirectory()) {
const files = fs.readdirSync(itemPath);
for (const file of files) {
if (file.endsWith(".json")) {
const jsonPath = path.join(itemPath, file);
const oggPath = jsonPath.replace(/\.json$/, ".ogg");
if (fs.existsSync(oggPath)) {
try {
// Check if OGG file is valid (not empty and has reasonable size)
const oggStats = fs.statSync(oggPath);
if (oggStats.size === 0) {
console.warn(`[muxer] Skipping empty OGG file: ${oggPath}`);
continue;
}
// Skip files that are too small (less than 1KB likely corrupted)
if (oggStats.size < 1024) {
console.warn(
`[muxer] Skipping too small OGG file (${oggStats.size} bytes): ${oggPath}`,
);
continue;
}
// Check if OGG file has valid header (starts with "OggS")
const oggBuffer = fs.readFileSync(oggPath);
const oggHeader = oggBuffer.slice(0, 4).toString();
if (oggHeader !== "OggS") {
console.warn(
`[muxer] Skipping invalid OGG file (bad header): ${oggPath}`,
);
continue;
}
const meta: EventMetadata = JSON.parse(
fs.readFileSync(jsonPath, "utf-8"),
);
clips.push({ oggPath, jsonPath, meta });
} catch (e) {
console.error(
`[muxer] Failed to read/parse JSON: ${jsonPath}`,
e,
);
}
processedDirs++;
const progress = ((processedDirs / items.length) * 100).toFixed(2);
console.log(`[muxer] Scanning progress: ${progress}% (${processedDirs}/${items.length} directories)`);
}
}
}
processedDirs++;
const progress = ((processedDirs / items.length) * 100).toFixed(2);
console.log(
`[muxer] Scanning progress: ${progress}% (${processedDirs}/${items.length} directories)`,
);
}
}
if (clips.length === 0) {
console.log("[muxer] No recording clips found to mux.");
return;
}
if (clips.length === 0) {
console.log("[muxer] No recording clips found to mux.");
return;
}
// Sort by startTime so chronologically they are in order
clips.sort((a, b) => a.meta.startTime - b.meta.startTime);
// Sort by startTime so chronologically they are in order
clips.sort((a, b) => a.meta.startTime - b.meta.startTime);
// Find the global start time
const globalStartTime = clips[0].meta.startTime;
console.log(`[muxer] Found ${clips.length} clips. Base timestamp: ${globalStartTime}`);
// Find the global start time
const globalStartTime = clips[0].meta.startTime;
console.log(
`[muxer] Found ${clips.length} clips. Base timestamp: ${globalStartTime}`,
);
const command = ffmpeg();
const filterParts: string[] = [];
const command = ffmpeg();
const filterParts: string[] = [];
console.log(`[muxer] Creating audio filters for ${clips.length} clips...`);
clips.forEach((clip, index) => {
command.input(clip.oggPath);
console.log(`[muxer] Creating audio filters for ${clips.length} clips...`);
clips.forEach((clip, index) => {
command.input(clip.oggPath);
// Calculate delay relative to the global start time
const delayMs = clip.meta.startTime - globalStartTime;
// Calculate delay relative to the global start time
const delayMs = clip.meta.startTime - globalStartTime;
// FFmpeg filter structure: [0:a]adelay=1000|1000[a0]
// Setting adelay multiple times covers stereo channels.
// We ensure all multiple channels get delayed.
const inputSpecifier = `[${index}:a]`;
const outputSpecifier = `[pad${index}]`;
// FFmpeg filter structure: [0:a]adelay=1000|1000[a0]
// Setting adelay multiple times covers stereo channels.
// We ensure all multiple channels get delayed.
const inputSpecifier = `[${index}:a]`;
const outputSpecifier = `[pad${index}]`;
filterParts.push(`${inputSpecifier}adelay=${delayMs}|${delayMs}${outputSpecifier}`);
const progress = (((index + 1) / clips.length) * 100).toFixed(2);
console.log(`[muxer] Filter creation progress: ${progress}% (${index + 1}/${clips.length} clips)`);
filterParts.push(
`${inputSpecifier}adelay=${delayMs}|${delayMs}${outputSpecifier}`,
);
const progress = (((index + 1) / clips.length) * 100).toFixed(2);
console.log(
`[muxer] Filter creation progress: ${progress}% (${index + 1}/${clips.length} clips)`,
);
});
// Merge them using amix
const amixInputs = clips.map((_, i) => `[pad${i}]`).join("");
// We add the amix command. dropout_transition=0 avoids volume drop when streams end.
filterParts.push(
`${amixInputs}amix=inputs=${clips.length}:dropout_transition=0[out]`,
);
const outputFilename = path.join(recordingsDir, `muxed-${Date.now()}.mp3`);
console.log(`[muxer] Combining clips. This might take a while...`);
// Using fluent-ffmpeg's complexFilter
command
.complexFilter(filterParts, "out")
.audioCodec("libmp3lame")
.save(outputFilename)
.on("progress", (progress) => {
if (progress.percent) {
console.log(`[muxer] Progress: ${progress.percent.toFixed(2)}%`);
}
})
.on("end", () => {
console.log(
`[muxer] Successfully muxed! Output saved to: ${outputFilename}`,
);
})
.on("error", (err) => {
console.error(`[muxer] FFmpeg Error:`, err);
});
// Merge them using amix
const amixInputs = clips.map((_, i) => `[pad${i}]`).join("");
// We add the amix command. dropout_transition=0 avoids volume drop when streams end.
filterParts.push(`${amixInputs}amix=inputs=${clips.length}:dropout_transition=0[out]`);
const outputFilename = path.join(recordingsDir, `muxed-${Date.now()}.mp3`);
console.log(`[muxer] Combining clips. This might take a while...`);
// Using fluent-ffmpeg's complexFilter
command
.complexFilter(filterParts, "out")
.audioCodec("libmp3lame")
.save(outputFilename)
.on("progress", (progress) => {
if (progress.percent) {
console.log(`[muxer] Progress: ${progress.percent.toFixed(2)}%`);
}
})
.on("end", () => {
console.log(`[muxer] Successfully muxed! Output saved to: ${outputFilename}`);
})
.on("error", (err) => {
console.error(`[muxer] FFmpeg Error:`, err);
});
}
startMuxing();
+42 -43
View File
@@ -1,57 +1,56 @@
import {
createAudioPlayer,
createAudioResource,
AudioPlayerStatus,
VoiceConnection,
AudioPlayer,
StreamType
import {
AudioPlayer,
AudioPlayerStatus,
createAudioPlayer,
createAudioResource,
StreamType,
VoiceConnection,
} from "@discordjs/voice";
import prism from "prism-media";
import { Readable } from "stream";
import prism from "prism-media";
export class DiscordPlayer {
private player: AudioPlayer;
private connection: VoiceConnection | null = null;
private player: AudioPlayer;
private connection: VoiceConnection | null = null;
constructor() {
this.player = createAudioPlayer();
this.player.on(AudioPlayerStatus.Playing, () => {
console.log("[player] Audio player is now playing!");
});
constructor() {
this.player = createAudioPlayer();
this.player.on("error", error => {
console.error(`[player] Error: ${error.message}`);
});
}
this.player.on(AudioPlayerStatus.Playing, () => {
console.log("[player] Audio player is now playing!");
});
public setConnection(connection: VoiceConnection) {
this.connection = connection;
this.connection.subscribe(this.player);
}
this.player.on("error", (error) => {
console.error(`[player] Error: ${error.message}`);
});
}
public playStream(stream: Readable) {
console.log("[player] Starting new audio stream...");
const resource = createAudioResource(stream, {
inputType: StreamType.OggOpus,
});
this.player.play(resource);
}
public setConnection(connection: VoiceConnection) {
this.connection = connection;
this.connection.subscribe(this.player);
}
public pause() {
this.player.pause(true);
}
public playStream(stream: Readable) {
console.log("[player] Starting new audio stream...");
public unpause() {
this.player.unpause();
}
const resource = createAudioResource(stream, {
inputType: StreamType.OggOpus,
});
public stop() {
this.player.stop();
}
this.player.play(resource);
}
public pause() {
this.player.pause(true);
}
public unpause() {
this.player.unpause();
}
public stop() {
this.player.stop();
}
}
export const discordPlayer = new DiscordPlayer();
+350 -302
View File
@@ -1,344 +1,392 @@
import {
EndBehaviorType,
entersState,
getVoiceConnection,
joinVoiceChannel,
VoiceConnectionStatus,
} from "@discordjs/voice";
import type { Client, VoiceChannel } from "discord.js-selfbot-v13";
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 { pipeline } from "stream/promises";
import { config } from "./config";
import { PacketFilter } from "./packetFilter";
const recordingsDir = process.env.RECORDINGS_DIR ?? "./recordings";
// Pastikan folder recordings ada
if (!fs.existsSync(recordingsDir)) {
fs.mkdirSync(recordingsDir, { recursive: true });
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,
selfMute: false,
debug: true,
});
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,
selfMute: false,
debug: true,
});
if (config.verbose) {
console.log(`[recorder] Joining voice channel: #${channel.name}`);
}
connection.on("debug", (msg) => {
if (config.verbose) {
console.log(`[recorder] Joining voice channel: #${channel.name}`);
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 member =
channel.guild.members.cache.get(userId) ||
(await channel.guild.members.fetch(userId).catch(() => null));
const username = user?.username ?? "Unknown User";
const avatarUrl =
user?.displayAvatarURL({ format: "png", size: 64 }) ??
"https://cdn.discordapp.com/embed/avatars/0.png";
const displayName = member?.displayName ?? username;
const roles =
member?.roles.cache
.filter((role) => role.id !== channel.guild.id)
.sort((a, b) => b.position - a.position)
.map((role) => ({
id: role.id,
name: role.name,
position: role.position,
})) ?? [];
const highestRole = roles.length > 0 ? roles[0] : null;
const joinedTimestamp = member?.joinedTimestamp ?? null;
// Tampilkan format "nama user [voice activity]"
console.log(`${username} [voice activity]`);
// Notify webserver
if ((global as any).updateActiveUser) {
(global as any).updateActiveUser(userId, {
username,
avatar: avatarUrl,
speaking: true,
});
}
connection.on('debug', msg => {
if (config.verbose) {
console.log(`[voice-debug] ${msg}`);
}
// Jangan record kalau sudah ada stream aktif untuk user ini
if (receiver.subscriptions.has(userId)) return;
const timestamp = Date.now();
const sessionStartTime = timestamp;
const sessionId = `${userId}-${sessionStartTime}`;
const recordingSegmentMsRaw = Number(
process.env.RECORDING_SEGMENT_MS ?? 5_000,
);
const recordingSegmentMs =
Number.isFinite(recordingSegmentMsRaw) && recordingSegmentMsRaw > 0
? recordingSegmentMsRaw
: 0;
const userDir = path.join(recordingsDir, userId);
if (!fs.existsSync(userDir)) {
fs.mkdirSync(userDir, { recursive: true });
}
const audioStream = receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 3000,
},
});
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;
}
// --- OGG file recording with segment rotation ---
const packetFilterForOgg = new PacketFilter(8);
const oggPacketStream = audioStream.pipe(packetFilterForOgg);
let segmentIndex = 0;
let currentSegment: {
index: number;
startTime: number;
endTime: number | null;
filename: string;
jsonFilename: string;
oggStream: any;
out: fs.WriteStream;
} | null = null;
const receiver = connection.receiver;
const openSegment = () => {
const index = segmentIndex++;
const startTime = Date.now();
const segmentFilename = path.join(userDir, `${startTime}.ogg`);
const segmentJsonFilename = path.join(userDir, `${startTime}.json`);
const oggStream = new prism.opus.OggLogicalBitstream({
opusHead: new prism.opus.OpusHead({
channelCount: 2,
sampleRate: 48000,
}),
pageSizeControl: { maxPackets: 10 },
crc: true,
});
const out = fs.createWriteStream(segmentFilename);
oggPacketStream.pipe(oggStream).pipe(out);
// 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 member = channel.guild.members.cache.get(userId) || await channel.guild.members.fetch(userId).catch(() => null);
const username = user?.username ?? "Unknown User";
const avatarUrl = user?.displayAvatarURL({ format: "png", size: 64 }) ?? "https://cdn.discordapp.com/embed/avatars/0.png";
const displayName = member?.displayName ?? username;
const roles = member?.roles.cache
.filter((role) => role.id !== channel.guild.id)
.sort((a, b) => b.position - a.position)
.map((role) => ({ id: role.id, name: role.name, position: role.position })) ?? [];
const highestRole = roles.length > 0 ? roles[0] : null;
const joinedTimestamp = member?.joinedTimestamp ?? null;
const segment = {
index,
startTime,
endTime: null as number | null,
filename: segmentFilename,
jsonFilename: segmentJsonFilename,
oggStream,
out,
};
// Tampilkan format "nama user [voice activity]"
console.log(`${username} [voice activity]`);
// Notify webserver
if ((global as any).updateActiveUser) {
(global as any).updateActiveUser(userId, { username, avatar: avatarUrl, speaking: true });
}
out.on("finish", () => {
if (config.verbose) {
console.log(`[recorder] Saved: ${segment.filename}`);
}
const endTime = segment.endTime ?? Date.now();
// Jangan record kalau sudah ada stream aktif untuk user ini
if (receiver.subscriptions.has(userId)) return;
const timestamp = Date.now();
const sessionStartTime = timestamp;
const sessionId = `${userId}-${sessionStartTime}`;
const recordingSegmentMsRaw = Number(process.env.RECORDING_SEGMENT_MS ?? 5_000);
const recordingSegmentMs = Number.isFinite(recordingSegmentMsRaw) && recordingSegmentMsRaw > 0
? recordingSegmentMsRaw
: 0;
const userDir = path.join(recordingsDir, userId);
if (!fs.existsSync(userDir)) {
fs.mkdirSync(userDir, { recursive: true });
}
const audioStream = receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 3000,
},
const eventMetadata = {
userId,
username,
tag: user?.tag ?? "Unknown#0000",
displayName,
avatarUrl,
bot: user?.bot ?? false,
roles,
highestRole,
joinedTimestamp,
sessionId,
sessionStartTime,
segmentIndex: segment.index,
segmentMs: recordingSegmentMs,
startTime: segment.startTime,
endTime,
durationMs: endTime - segment.startTime,
filename: path.basename(segment.filename),
};
fs.writeFileSync(
segment.jsonFilename,
JSON.stringify(eventMetadata, null, 2),
);
if (config.verbose) {
console.log(`[recorder] Saved metadata: ${segment.jsonFilename}`);
}
});
out.on("error", (err) => {
console.error(`[recorder] File write error ${userId}:`, err.message);
});
return segment;
};
const closeSegment = () => {
if (!currentSegment) return;
currentSegment.endTime = Date.now();
oggPacketStream.unpipe(currentSegment.oggStream);
currentSegment.oggStream.end();
currentSegment = null;
};
const rotateSegmentIfNeeded = () => {
if (!currentSegment) return;
if (recordingSegmentMs <= 0) return;
if (Date.now() - currentSegment.startTime < recordingSegmentMs) return;
closeSegment();
currentSegment = openSegment();
};
currentSegment = openSegment();
// --- Web broadcast: prism decoder with safe restart and cooldown ---
// OpusScript can crash on long/invalid streams; avoid taking down the process.
const decoderConfig = {
frameSize: 960,
channels: 2 as const,
rate: 48000 as const,
};
const decoderCooldownMs = 30_000;
const decoderRotateMs = Number(process.env.DECODER_ROTATE_MS ?? 5_000);
let currentDecoder: prism.opus.Decoder | null = null;
let decoderDisabledUntil = 0;
let decoderCreatedAt = 0;
const handlePcm = (pcm: Buffer) => {
if (!(global as any).broadcastPcmToWeb) return;
// Downsample 48kHz stereo → 24kHz mono (left channel, every 2nd sample)
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);
};
const destroyDecoder = () => {
if (!currentDecoder) return;
currentDecoder.removeAllListeners();
currentDecoder.destroy();
currentDecoder = null;
decoderCreatedAt = 0;
};
const createDecoder = () => {
if (Date.now() < decoderDisabledUntil) return null;
try {
// --- OGG file recording with segment rotation ---
const packetFilterForOgg = new PacketFilter(8);
const oggPacketStream = audioStream.pipe(packetFilterForOgg);
let segmentIndex = 0;
let currentSegment: {
index: number;
startTime: number;
endTime: number | null;
filename: string;
jsonFilename: string;
oggStream: any;
out: fs.WriteStream;
} | null = null;
const openSegment = () => {
const index = segmentIndex++;
const startTime = Date.now();
const segmentFilename = path.join(userDir, `${startTime}.ogg`);
const segmentJsonFilename = path.join(userDir, `${startTime}.json`);
const oggStream = new prism.opus.OggLogicalBitstream({
opusHead: new prism.opus.OpusHead({ channelCount: 2, sampleRate: 48000 }),
pageSizeControl: { maxPackets: 10 },
crc: true,
});
const out = fs.createWriteStream(segmentFilename);
oggPacketStream.pipe(oggStream).pipe(out);
const segment = {
index,
startTime,
endTime: null as number | null,
filename: segmentFilename,
jsonFilename: segmentJsonFilename,
oggStream,
out,
};
out.on("finish", () => {
if (config.verbose) {
console.log(`[recorder] Saved: ${segment.filename}`);
}
const endTime = segment.endTime ?? Date.now();
const eventMetadata = {
userId,
username,
tag: user?.tag ?? "Unknown#0000",
displayName,
avatarUrl,
bot: user?.bot ?? false,
roles,
highestRole,
joinedTimestamp,
sessionId,
sessionStartTime,
segmentIndex: segment.index,
segmentMs: recordingSegmentMs,
startTime: segment.startTime,
endTime,
durationMs: endTime - segment.startTime,
filename: path.basename(segment.filename)
};
fs.writeFileSync(segment.jsonFilename, JSON.stringify(eventMetadata, null, 2));
if (config.verbose) {
console.log(`[recorder] Saved metadata: ${segment.jsonFilename}`);
}
});
out.on("error", (err) => {
console.error(`[recorder] File write error ${userId}:`, err.message);
});
return segment;
};
const closeSegment = () => {
if (!currentSegment) return;
currentSegment.endTime = Date.now();
oggPacketStream.unpipe(currentSegment.oggStream);
currentSegment.oggStream.end();
currentSegment = null;
};
const rotateSegmentIfNeeded = () => {
if (!currentSegment) return;
if (recordingSegmentMs <= 0) return;
if (Date.now() - currentSegment.startTime < recordingSegmentMs) return;
closeSegment();
currentSegment = openSegment();
};
currentSegment = openSegment();
// --- Web broadcast: prism decoder with safe restart and cooldown ---
// OpusScript can crash on long/invalid streams; avoid taking down the process.
const decoderConfig = { frameSize: 960, channels: 2, rate: 48000 };
const decoderCooldownMs = 30_000;
const decoderRotateMs = Number(process.env.DECODER_ROTATE_MS ?? 5_000);
let currentDecoder: prism.opus.Decoder | null = null;
let decoderDisabledUntil = 0;
let decoderCreatedAt = 0;
const handlePcm = (pcm: Buffer) => {
if (!(global as any).broadcastPcmToWeb) return;
// Downsample 48kHz stereo → 24kHz mono (left channel, every 2nd sample)
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);
};
const destroyDecoder = () => {
if (!currentDecoder) return;
currentDecoder.removeAllListeners();
currentDecoder.destroy();
currentDecoder = null;
decoderCreatedAt = 0;
};
const createDecoder = () => {
if (Date.now() < decoderDisabledUntil) return null;
try {
const d = new prism.opus.Decoder(decoderConfig);
d.on('data', handlePcm);
d.on('error', (err) => {
console.warn("[recorder] Opus decoder error, cooling down:", err);
decoderDisabledUntil = Date.now() + decoderCooldownMs;
destroyDecoder();
});
decoderCreatedAt = Date.now();
return d;
} catch (err) {
console.warn("[recorder] Opus decoder init failed, cooling down:", err);
decoderDisabledUntil = Date.now() + decoderCooldownMs;
return null;
}
};
const rotateDecoderIfNeeded = () => {
if (!currentDecoder || decoderRotateMs <= 0) return;
if (Date.now() - decoderCreatedAt < decoderRotateMs) return;
destroyDecoder();
currentDecoder = createDecoder();
};
const ensureDecoder = () => {
if (!currentDecoder) {
currentDecoder = createDecoder();
}
return currentDecoder;
};
// Feed Opus packets one-by-one
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')}`);
}
if (chunk.length < 8) return; // skip tiny control/DTX packets
rotateSegmentIfNeeded();
if (!(global as any).broadcastPcmToWeb) return;
rotateDecoderIfNeeded();
const decoder = ensureDecoder();
if (!decoder) return;
try {
decoder.write(chunk);
} catch (err) {
console.warn("[recorder] Opus decoder write failed, cooling down:", err);
decoderDisabledUntil = Date.now() + decoderCooldownMs;
destroyDecoder();
}
});
audioStream.on('end', () => {
closeSegment();
destroyDecoder();
if ((global as any).updateActiveUser) {
(global as any).updateActiveUser(userId, { username, avatar: avatarUrl, speaking: false });
}
});
audioStream.on('error', (err) => {
closeSegment();
destroyDecoder();
console.error(`[recorder] Audio Stream error ${userId}:`, err.message);
});
packetFilterForOgg.on('error', (err) => {
closeSegment();
console.error(`[recorder] PacketFilter(ogg) error ${userId}:`, err.message);
});
} catch (e) {
console.error(`[recorder] Failed to create stream for ${userId}:`, e);
const d = new prism.opus.Decoder(decoderConfig);
d.on("data", handlePcm);
d.on("error", (err) => {
console.warn("[recorder] Opus decoder error, cooling down:", err);
decoderDisabledUntil = Date.now() + decoderCooldownMs;
destroyDecoder();
});
decoderCreatedAt = Date.now();
return d;
} catch (err) {
console.warn(
"[recorder] Opus decoder init failed, cooling down:",
err,
);
decoderDisabledUntil = Date.now() + decoderCooldownMs;
return null;
}
});
};
// Handle disconnect yang tidak disengaja
connection.on(VoiceConnectionStatus.Disconnected, async () => {
if (config.verbose) {
console.warn("[recorder] Disconnected from voice channel. Reconnecting...");
const rotateDecoderIfNeeded = () => {
if (!currentDecoder || decoderRotateMs <= 0) return;
if (Date.now() - decoderCreatedAt < decoderRotateMs) return;
destroyDecoder();
currentDecoder = createDecoder();
};
const ensureDecoder = () => {
if (!currentDecoder) {
currentDecoder = createDecoder();
}
return currentDecoder;
};
// Feed Opus packets one-by-one
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")}`,
);
}
if (chunk.length < 8) return; // skip tiny control/DTX packets
rotateSegmentIfNeeded();
if (!(global as any).broadcastPcmToWeb) return;
rotateDecoderIfNeeded();
const decoder = ensureDecoder();
if (!decoder) return;
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();
decoder.write(chunk);
} catch (err) {
console.warn(
"[recorder] Opus decoder write failed, cooling down:",
err,
);
decoderDisabledUntil = Date.now() + decoderCooldownMs;
destroyDecoder();
}
});
});
connection.on(VoiceConnectionStatus.Destroyed, () => {
if (config.verbose) {
console.log("[recorder] Voice connection destroyed.");
audioStream.on("end", () => {
closeSegment();
destroyDecoder();
if ((global as any).updateActiveUser) {
(global as any).updateActiveUser(userId, {
username,
avatar: avatarUrl,
speaking: false,
});
}
});
});
audioStream.on("error", (err) => {
closeSegment();
destroyDecoder();
console.error(`[recorder] Audio Stream error ${userId}:`, err.message);
});
packetFilterForOgg.on("error", (err) => {
closeSegment();
console.error(
`[recorder] PacketFilter(ogg) 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.");
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.");
}
}
+182 -149
View File
@@ -1,182 +1,215 @@
import express from "express";
import http from "http";
import { WebSocketServer } from "ws";
import path from "path";
import prism from "prism-media";
import { WebSocketServer } from "ws";
import { discordPlayer } from "./player";
const activeUsers = new Map<string, { username: string, avatar: string, speaking: boolean }>();
const activeUsers = new Map<
string,
{ username: string; avatar: string; speaking: boolean }
>();
let wsClients = new Set<any>();
// Upsample 24kHz mono s16le → 48kHz stereo s16le (pure JS)
function upsample(mono24k: Buffer): Buffer {
const out = Buffer.alloc(mono24k.length * 4);
for (let i = 0; i < mono24k.length / 2; i++) {
const s = mono24k.readInt16LE(i * 2);
out.writeInt16LE(s, i * 8);
out.writeInt16LE(s, i * 8 + 2);
out.writeInt16LE(s, i * 8 + 4);
out.writeInt16LE(s, i * 8 + 6);
}
return out;
const out = Buffer.alloc(mono24k.length * 4);
for (let i = 0; i < mono24k.length / 2; i++) {
const s = mono24k.readInt16LE(i * 2);
out.writeInt16LE(s, i * 8);
out.writeInt16LE(s, i * 8 + 2);
out.writeInt16LE(s, i * 8 + 4);
out.writeInt16LE(s, i * 8 + 6);
}
return out;
}
// Calculate RMS dB level of a PCM s16le buffer
function rmsDb(pcm: Buffer): number {
let sum = 0;
const samples = pcm.length / 2;
for (let i = 0; i < samples; i++) {
const s = pcm.readInt16LE(i * 2) / 32768;
sum += s * s;
}
const rms = Math.sqrt(sum / samples);
return 20 * Math.log10(Math.max(rms, 1e-10));
let sum = 0;
const samples = pcm.length / 2;
for (let i = 0; i < samples; i++) {
const s = pcm.readInt16LE(i * 2) / 32768;
sum += s * s;
}
const rms = Math.sqrt(sum / samples);
return 20 * Math.log10(Math.max(rms, 1e-10));
}
export function startWebserver(port: number = 3000) {
const app = express();
const server = http.createServer(app);
const app = express();
const server = http.createServer(app);
const wsPort = port + 1;
const wss = new WebSocketServer({ port: wsPort, host: "0.0.0.0" });
console.log(`[webserver] WebSocket server listening on ws://0.0.0.0:${wsPort}`);
const wsPort = port + 1;
const wss = new WebSocketServer({ port: wsPort, host: "0.0.0.0" });
console.log(
`[webserver] WebSocket server listening on ws://0.0.0.0:${wsPort}`,
);
app.use(express.static(path.join(__dirname, "../public")));
app.use(express.static(path.join(__dirname, "../public")));
// Inbound: Discord PCM → tagged chunks → browser
(global as any).broadcastPcmToWeb = (chunk: Buffer, userId: string) => {
let hash = 0;
for (let i = 0; i < userId.length; i++) {
hash = ((hash << 5) - hash) + userId.charCodeAt(i);
hash |= 0;
}
const header = Buffer.alloc(4);
header.writeInt32LE(hash, 0);
const packet = Buffer.concat([header, chunk]);
wsClients.forEach(client => {
if (client.readyState === 1) client.send(packet);
});
};
// Inbound: Discord PCM → tagged chunks → browser
(global as any).broadcastPcmToWeb = (chunk: Buffer, userId: string) => {
let hash = 0;
for (let i = 0; i < userId.length; i++) {
hash = (hash << 5) - hash + userId.charCodeAt(i);
hash |= 0;
}
const header = Buffer.alloc(4);
header.writeInt32LE(hash, 0);
const packet = Buffer.concat([header, chunk]);
wsClients.forEach((client) => {
if (client.readyState === 1) client.send(packet);
});
};
(global as any).updateActiveUser = (userId: string, data: { username: string, avatar: string, speaking: boolean }) => {
activeUsers.set(userId, data);
broadcastUserState();
};
(global as any).updateActiveUser = (
userId: string,
data: { username: string; avatar: string; speaking: boolean },
) => {
activeUsers.set(userId, data);
broadcastUserState();
};
function broadcastUserState() {
const payload = JSON.stringify({
type: "user_state",
users: Array.from(activeUsers.entries()).map(([id, data]) => ({ id, ...data }))
});
wsClients.forEach(client => {
if (client.readyState === 1) client.send(payload);
});
function broadcastUserState() {
const payload = JSON.stringify({
type: "user_state",
users: Array.from(activeUsers.entries()).map(([id, data]) => ({
id,
...data,
})),
});
wsClients.forEach((client) => {
if (client.readyState === 1) client.send(payload);
});
}
// --- Outbound: browser PCM (24kHz mono) → Opus → Discord ---
const RATE = 48000;
const CHANNELS = 2;
const FRAME_SIZE = 960;
const BYTES_PER_FRAME = FRAME_SIZE * CHANNELS * 2; // 3840 bytes = 20ms
const SILENCE_TAIL_MS = 300; // continue sending silence for 300ms after browser stops
const MAX_BUF_BYTES = BYTES_PER_FRAME * 50; // cap at 1 second to avoid runaway buffer
const opusEncoder = new prism.opus.Encoder({
rate: RATE,
channels: CHANNELS,
frameSize: FRAME_SIZE,
});
const oggBitstream = new prism.opus.OggLogicalBitstream({
opusHead: new prism.opus.OpusHead({
channelCount: CHANNELS,
sampleRate: RATE,
}),
pageSizeControl: { maxPackets: 1 }, // 1 packet per page = 20ms latency
crc: true,
});
opusEncoder.on("error", () => {});
opusEncoder.pipe(oggBitstream);
// Prime OGG headers before player starts reading
opusEncoder.write(Buffer.alloc(BYTES_PER_FRAME, 0));
discordPlayer.playStream(oggBitstream);
discordPlayer.pause();
let pcmBuffer = Buffer.alloc(0);
let lastBrowserAudioTime = 0;
let playerPaused = true;
const SILENCE_FRAME = Buffer.alloc(BYTES_PER_FRAME, 0);
// Log level every 2 seconds
let dbAccum = 0,
dbCount = 0;
setInterval(() => {
if (dbCount > 0) {
const avg = dbAccum / dbCount;
console.log(
`[transmit] Audio level: ${avg.toFixed(1)} dBFS (${dbCount} frames/2s)`,
);
dbAccum = 0;
dbCount = 0;
}
}, 2000);
// PULL-BASED encode loop: fires every 20ms, pulls exactly one frame from buffer.
// This avoids the timing conflict where browser bursts and silence timer collide.
setInterval(() => {
const msSinceAudio = Date.now() - lastBrowserAudioTime;
let frame: Buffer | null = null;
if (pcmBuffer.length >= BYTES_PER_FRAME) {
// Real audio available
frame = pcmBuffer.slice(0, BYTES_PER_FRAME);
pcmBuffer = pcmBuffer.slice(BYTES_PER_FRAME);
// Track level for logging
dbAccum += rmsDb(frame);
dbCount++;
if (playerPaused) {
discordPlayer.unpause();
playerPaused = false;
console.log("[transmit] Transmitting — Discord indicator ON");
}
} else if (msSinceAudio < SILENCE_TAIL_MS && msSinceAudio > 0) {
// Buffer drained but audio was recent — pad silence to avoid OGG gap
frame = SILENCE_FRAME;
} else if (!playerPaused && msSinceAudio >= SILENCE_TAIL_MS) {
// No audio for a while — pause Discord indicator
discordPlayer.pause();
playerPaused = true;
console.log("[transmit] Stopped — Discord indicator OFF");
return;
} else {
return; // already paused, nothing to do
}
// --- Outbound: browser PCM (24kHz mono) → Opus → Discord ---
const RATE = 48000;
const CHANNELS = 2;
const FRAME_SIZE = 960;
const BYTES_PER_FRAME = FRAME_SIZE * CHANNELS * 2; // 3840 bytes = 20ms
const SILENCE_TAIL_MS = 300; // continue sending silence for 300ms after browser stops
const MAX_BUF_BYTES = BYTES_PER_FRAME * 50; // cap at 1 second to avoid runaway buffer
// Write one frame. If encoder is backpressured, skip this tick to avoid stalling.
const ok = opusEncoder.write(frame);
if (!ok) {
opusEncoder.once("drain", () => {}); // re-arm drain without blocking
}
}, 20);
const opusEncoder = new prism.opus.Encoder({ rate: RATE, channels: CHANNELS, frameSize: FRAME_SIZE });
const oggBitstream = new prism.opus.OggLogicalBitstream({
opusHead: new prism.opus.OpusHead({ channelCount: CHANNELS, sampleRate: RATE }),
pageSizeControl: { maxPackets: 1 }, // 1 packet per page = 20ms latency
crc: true,
});
opusEncoder.on('error', () => {});
opusEncoder.pipe(oggBitstream);
wss.on("connection", (ws) => {
console.log("[webserver] New WebSocket connection on port " + wsPort);
wsClients.add(ws);
// Prime OGG headers before player starts reading
opusEncoder.write(Buffer.alloc(BYTES_PER_FRAME, 0));
discordPlayer.playStream(oggBitstream);
discordPlayer.pause();
ws.send(
JSON.stringify({
type: "user_state",
users: Array.from(activeUsers.entries()).map(([id, data]) => ({
id,
...data,
})),
}),
);
let pcmBuffer = Buffer.alloc(0);
let lastBrowserAudioTime = 0;
let playerPaused = true;
const SILENCE_FRAME = Buffer.alloc(BYTES_PER_FRAME, 0);
ws.on("message", (data: any) => {
if (!Buffer.isBuffer(data)) return;
lastBrowserAudioTime = Date.now();
// Log level every 2 seconds
let dbAccum = 0, dbCount = 0;
setInterval(() => {
if (dbCount > 0) {
const avg = dbAccum / dbCount;
console.log(`[transmit] Audio level: ${avg.toFixed(1)} dBFS (${dbCount} frames/2s)`);
dbAccum = 0; dbCount = 0;
}
}, 2000);
// Upsample 24kHz mono → 48kHz stereo and add to buffer
const upsampled = upsample(data);
// PULL-BASED encode loop: fires every 20ms, pulls exactly one frame from buffer.
// This avoids the timing conflict where browser bursts and silence timer collide.
setInterval(() => {
const msSinceAudio = Date.now() - lastBrowserAudioTime;
let frame: Buffer | null = null;
if (pcmBuffer.length >= BYTES_PER_FRAME) {
// Real audio available
frame = pcmBuffer.slice(0, BYTES_PER_FRAME);
pcmBuffer = pcmBuffer.slice(BYTES_PER_FRAME);
// Track level for logging
dbAccum += rmsDb(frame);
dbCount++;
if (playerPaused) {
discordPlayer.unpause();
playerPaused = false;
console.log("[transmit] Transmitting — Discord indicator ON");
}
} else if (msSinceAudio < SILENCE_TAIL_MS && msSinceAudio > 0) {
// Buffer drained but audio was recent — pad silence to avoid OGG gap
frame = SILENCE_FRAME;
} else if (!playerPaused && msSinceAudio >= SILENCE_TAIL_MS) {
// No audio for a while — pause Discord indicator
discordPlayer.pause();
playerPaused = true;
console.log("[transmit] Stopped — Discord indicator OFF");
return;
} else {
return; // already paused, nothing to do
}
// Write one frame. If encoder is backpressured, skip this tick to avoid stalling.
const ok = opusEncoder.write(frame);
if (!ok) {
opusEncoder.once('drain', () => {}); // re-arm drain without blocking
}
}, 20);
wss.on("connection", (ws) => {
console.log("[webserver] New WebSocket connection on port " + wsPort);
wsClients.add(ws);
ws.send(JSON.stringify({
type: "user_state",
users: Array.from(activeUsers.entries()).map(([id, data]) => ({ id, ...data }))
}));
ws.on("message", (data: any) => {
if (!Buffer.isBuffer(data)) return;
lastBrowserAudioTime = Date.now();
// Upsample 24kHz mono → 48kHz stereo and add to buffer
const upsampled = upsample(data);
// Cap buffer to avoid runaway growth during stall
if (pcmBuffer.length < MAX_BUF_BYTES) {
pcmBuffer = Buffer.concat([pcmBuffer, upsampled]);
}
});
ws.on("close", () => { wsClients.delete(ws); });
ws.on("error", () => { wsClients.delete(ws); });
// Cap buffer to avoid runaway growth during stall
if (pcmBuffer.length < MAX_BUF_BYTES) {
pcmBuffer = Buffer.concat([pcmBuffer, upsampled]);
}
});
server.listen(port, "0.0.0.0", () => {
console.log(`[webserver] Web interface listening on http://0.0.0.0:${port}`);
ws.on("close", () => {
wsClients.delete(ws);
});
ws.on("error", () => {
wsClients.delete(ws);
});
});
server.listen(port, "0.0.0.0", () => {
console.log(
`[webserver] Web interface listening on http://0.0.0.0:${port}`,
);
});
}