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

View File

@@ -1,15 +1,23 @@
{ {
"files": {
"includes": ["src/**/*.ts", "tests/**/*.ts", "*.json", "*.ts"]
},
"formatter": { "formatter": {
"enabled": true, "enabled": true,
"indentStyle": "space", "indentStyle": "space",
"indentWidth": 2, "indentWidth": 2
"includes": ["src/**/*.ts", "tests/**/*.ts", "*.json", "*.ts"]
}, },
"linter": { "linter": {
"enabled": true, "enabled": true,
"rules": { "rules": {
"recommended": true "recommended": false,
"style": {
"noNonNullAssertion": "warn",
"useNodejsImportProtocol": "warn"
}, },
"includes": ["src/**/*.ts", "tests/**/*.ts", "*.json", "*.ts"] "suspicious": {
"noExplicitAny": "warn"
}
}
} }
} }

BIN
bun.lockb

Binary file not shown.

View File

@@ -0,0 +1,975 @@
# Aggressive Cleanup Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Clean up Bun/TypeScript Discord voice recorder with Biome, Vitest, stricter types, modular recorder code, and verification scripts.
**Architecture:** Keep runtime behavior same while moving pure logic into focused modules. `src/recorder.ts` remains public API or re-exports recorder module to minimize import churn; new `src/recorder/*` files own config parsing, metadata creation, decoder lifecycle, segment lifecycle, and stream orchestration.
**Tech Stack:** Bun, TypeScript strict mode, Biome, Vitest, discord.js-selfbot-v13, @discordjs/voice, prism-media.
---
## File Map
- Modify `package.json`: add scripts and dev dependencies.
- Create `biome.json`: formatter/linter config.
- Create `vitest.config.ts`: Bun-compatible Vitest config.
- Modify `tsconfig.json`: include tests/config if needed, keep strict mode.
- Modify `src/config.ts`: typed env/config parsing.
- Create `src/types.ts`: shared recorder-facing types.
- Create `src/recorder/metadata.ts`: user metadata and segment metadata builders.
- Create `src/recorder/decoder.ts`: Opus decoder lifecycle.
- Create `src/recorder/segment.ts`: OGG segment lifecycle.
- Create `src/recorder/audioStream.ts`: subscribe/event wiring helpers.
- Modify `src/recorder.ts`: orchestrator using new modules.
- Create `tests/config.test.ts`, `tests/recorder/metadata.test.ts`, `tests/recorder/decoder.test.ts`, `tests/recorder/segment.test.ts`.
---
### Task 1: Tooling setup
**Files:**
- Modify: `package.json`
- Create: `biome.json`
- Create: `vitest.config.ts`
- Modify: `tsconfig.json`
- [ ] **Step 1: Add dependencies**
Run:
```bash
bun add -d @biomejs/biome vitest
```
Expected: `package.json` and lockfile update.
- [ ] **Step 2: Update scripts in `package.json`**
Set scripts to include:
```json
{
"dev": "bun --watch src/index.ts",
"start": "bun src/index.ts",
"typecheck": "tsc --noEmit",
"lint": "biome check .",
"format": "biome format --write .",
"test": "vitest run"
}
```
- [ ] **Step 3: Create `biome.json`**
```json
{
"$schema": "https://biomejs.dev/schemas/2.0.0/schema.json",
"files": {
"includes": ["src/**/*.ts", "tests/**/*.ts", "*.json", "*.ts"]
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"indentWidth": 2
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"suspicious": {
"noExplicitAny": "warn"
}
}
}
}
```
- [ ] **Step 4: Create `vitest.config.ts`**
```ts
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
environment: "node",
include: ["tests/**/*.test.ts"],
},
});
```
- [ ] **Step 5: Verify tooling commands**
Run:
```bash
bun run typecheck
bun run lint
bun run test
```
Expected: typecheck may pass or show existing issues; lint may show format/type warnings; test may pass with no tests or fail if Vitest needs config adjustment. Fix only setup errors in this task.
- [ ] **Step 6: Commit**
```bash
git add package.json bun.lockb biome.json vitest.config.ts tsconfig.json
git commit -m "chore: add code quality tooling"
```
---
### Task 2: Config parsing tests and implementation
**Files:**
- Modify: `src/config.ts`
- Create: `tests/config.test.ts`
- [ ] **Step 1: Write failing tests**
Create `tests/config.test.ts`:
```ts
import { describe, expect, it } from "vitest";
import { parseBoolean, parsePositiveNumber } from "../src/config";
describe("config parsers", () => {
it("parses boolean values", () => {
expect(parseBoolean("true", false)).toBe(true);
expect(parseBoolean("false", true)).toBe(false);
expect(parseBoolean(undefined, true)).toBe(true);
});
it("parses positive numbers", () => {
expect(parsePositiveNumber("5000", 0)).toBe(5000);
expect(parsePositiveNumber("0", 123)).toBe(123);
expect(parsePositiveNumber("bad", 123)).toBe(123);
expect(parsePositiveNumber(undefined, 123)).toBe(123);
});
});
```
- [ ] **Step 2: Run failing test**
Run:
```bash
bun run test tests/config.test.ts
```
Expected: FAIL because `parseBoolean` and `parsePositiveNumber` are not exported.
- [ ] **Step 3: Implement config helpers**
Update `src/config.ts` to export:
```ts
export interface AppConfig {
verbose: boolean;
recordingsDir: string;
recordingSegmentMs: number;
decoderRotateMs: number;
decoderCooldownMs: number;
}
export function parseBoolean(value: string | undefined, fallback: boolean): boolean {
if (value === "true") return true;
if (value === "false") return false;
return fallback;
}
export function parsePositiveNumber(value: string | undefined, fallback: number): number {
const parsed = Number(value);
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
}
export function loadConfig(env: NodeJS.ProcessEnv = process.env): AppConfig {
return {
verbose: parseBoolean(env.VERBOSE, false),
recordingsDir: env.RECORDINGS_DIR ?? "./recordings",
recordingSegmentMs: parsePositiveNumber(env.RECORDING_SEGMENT_MS, 5_000),
decoderRotateMs: parsePositiveNumber(env.DECODER_ROTATE_MS, 5_000),
decoderCooldownMs: 30_000,
};
}
export const config = loadConfig();
```
Preserve any existing exports by folding them into `AppConfig` if needed.
- [ ] **Step 4: Run config tests**
Run:
```bash
bun run test tests/config.test.ts
```
Expected: PASS.
- [ ] **Step 5: Run typecheck**
Run:
```bash
bun run typecheck
```
Expected: PASS or only unrelated existing errors. Fix config-related errors.
- [ ] **Step 6: Commit**
```bash
git add src/config.ts tests/config.test.ts
git commit -m "refactor: type application config"
```
---
### Task 3: Shared recorder types
**Files:**
- Create: `src/types.ts`
- [ ] **Step 1: Create shared types**
Create `src/types.ts`:
```ts
import type fs from "node:fs";
import type prism from "prism-media";
export interface RoleMetadata {
id: string;
name: string;
position: number;
}
export interface UserMetadata {
userId: string;
username: string;
tag: string;
displayName: string;
avatarUrl: string;
bot: boolean;
roles: RoleMetadata[];
highestRole: RoleMetadata | null;
joinedTimestamp: number | null;
}
export interface SegmentState {
index: number;
startTime: number;
endTime: number | null;
filename: string;
jsonFilename: string;
oggStream: prism.opus.OggLogicalBitstream;
out: fs.WriteStream;
}
export interface SegmentMetadata extends UserMetadata {
sessionId: string;
sessionStartTime: number;
segmentIndex: number;
segmentMs: number;
startTime: number;
endTime: number;
durationMs: number;
filename: string;
}
export interface PcmBroadcaster {
broadcastPcmToWeb?: (chunk: Buffer, userId: string) => void;
updateActiveUser?: (userId: string, data: { username: string; avatar: string; speaking: boolean }) => void;
}
```
- [ ] **Step 2: Run typecheck**
Run:
```bash
bun run typecheck
```
Expected: PASS. If prism type export fails, use `unknown` for `oggStream` plus local narrowed calls in segment implementation.
- [ ] **Step 3: Commit**
```bash
git add src/types.ts
git commit -m "refactor: add recorder domain types"
```
---
### Task 4: Metadata tests and implementation
**Files:**
- Create: `src/recorder/metadata.ts`
- Create: `tests/recorder/metadata.test.ts`
- [ ] **Step 1: Write tests**
Create `tests/recorder/metadata.test.ts`:
```ts
import { describe, expect, it } from "vitest";
import { createSegmentMetadata } from "../../src/recorder/metadata";
import type { SegmentState, UserMetadata } from "../../src/types";
const user: UserMetadata = {
userId: "123",
username: "alice",
tag: "alice#0001",
displayName: "Alice",
avatarUrl: "https://cdn.discordapp.com/embed/avatars/0.png",
bot: false,
roles: [{ id: "role", name: "Admin", position: 1 }],
highestRole: { id: "role", name: "Admin", position: 1 },
joinedTimestamp: 100,
};
const segment = {
index: 2,
startTime: 1_000,
endTime: 2_500,
filename: "/tmp/2500.ogg",
jsonFilename: "/tmp/2500.json",
oggStream: {} as SegmentState["oggStream"],
out: {} as SegmentState["out"],
};
describe("createSegmentMetadata", () => {
it("combines user and segment data", () => {
const metadata = createSegmentMetadata(user, segment, "session-1", 900, 5_000);
expect(metadata).toMatchObject({
userId: "123",
username: "alice",
sessionId: "session-1",
sessionStartTime: 900,
segmentIndex: 2,
segmentMs: 5_000,
startTime: 1_000,
endTime: 2_500,
durationMs: 1_500,
filename: "2500.ogg",
});
});
});
```
- [ ] **Step 2: Run failing test**
```bash
bun run test tests/recorder/metadata.test.ts
```
Expected: FAIL because module does not exist.
- [ ] **Step 3: Implement metadata module**
Create `src/recorder/metadata.ts`:
```ts
import path from "node:path";
import type { Client, VoiceChannel } from "discord.js-selfbot-v13";
import type { SegmentMetadata, SegmentState, UserMetadata } from "../types";
export async function collectUserMetadata(
client: Client,
userId: string,
channel: VoiceChannel,
): Promise<UserMetadata> {
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 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 })) ?? [];
return {
userId,
username,
tag: user?.tag ?? "Unknown#0000",
displayName: member?.displayName ?? username,
avatarUrl: user?.displayAvatarURL({ format: "png", size: 64 }) ?? "https://cdn.discordapp.com/embed/avatars/0.png",
bot: user?.bot ?? false,
roles,
highestRole: roles[0] ?? null,
joinedTimestamp: member?.joinedTimestamp ?? null,
};
}
export function createSegmentMetadata(
user: UserMetadata,
segment: SegmentState,
sessionId: string,
sessionStartTime: number,
recordingSegmentMs: number,
): SegmentMetadata {
const endTime = segment.endTime ?? Date.now();
return {
...user,
sessionId,
sessionStartTime,
segmentIndex: segment.index,
segmentMs: recordingSegmentMs,
startTime: segment.startTime,
endTime,
durationMs: endTime - segment.startTime,
filename: path.basename(segment.filename),
};
}
```
- [ ] **Step 4: Run tests and typecheck**
```bash
bun run test tests/recorder/metadata.test.ts
bun run typecheck
```
Expected: PASS.
- [ ] **Step 5: Commit**
```bash
git add src/recorder/metadata.ts tests/recorder/metadata.test.ts
git commit -m "refactor: extract recorder metadata builders"
```
---
### Task 5: Decoder tests and implementation
**Files:**
- Create: `src/recorder/decoder.ts`
- Create: `tests/recorder/decoder.test.ts`
- [ ] **Step 1: Write tests using fake decoder factory**
Create `tests/recorder/decoder.test.ts`:
```ts
import { describe, expect, it, vi } from "vitest";
import { OpusDecoder } from "../../src/recorder/decoder";
class FakeDecoder {
handlers = new Map<string, (...args: unknown[]) => void>();
destroyed = false;
writes: Buffer[] = [];
on(event: string, handler: (...args: unknown[]) => void) {
this.handlers.set(event, handler);
return this;
}
write(chunk: Buffer) {
this.writes.push(chunk);
}
removeAllListeners() {
this.handlers.clear();
}
destroy() {
this.destroyed = true;
}
}
describe("OpusDecoder", () => {
it("creates decoder lazily and writes chunks", () => {
const fake = new FakeDecoder();
const decoder = new OpusDecoder({ cooldownMs: 30_000, rotateMs: 5_000, createDecoder: () => fake as never, onData: vi.fn() });
decoder.write(Buffer.from([1, 2, 3]));
expect(fake.writes).toHaveLength(1);
});
it("destroys and recreates after rotation timeout", () => {
vi.useFakeTimers();
const created: FakeDecoder[] = [];
const decoder = new OpusDecoder({
cooldownMs: 30_000,
rotateMs: 5_000,
createDecoder: () => {
const fake = new FakeDecoder();
created.push(fake);
return fake as never;
},
onData: vi.fn(),
});
decoder.write(Buffer.from([1]));
vi.advanceTimersByTime(5_001);
decoder.rotateIfNeeded();
decoder.write(Buffer.from([2]));
expect(created).toHaveLength(2);
expect(created[0].destroyed).toBe(true);
vi.useRealTimers();
});
});
```
- [ ] **Step 2: Run failing test**
```bash
bun run test tests/recorder/decoder.test.ts
```
Expected: FAIL because module does not exist.
- [ ] **Step 3: Implement decoder module**
Create `src/recorder/decoder.ts`:
```ts
import prism from "prism-media";
export interface OpusDecoderOptions {
cooldownMs: number;
rotateMs: number;
createDecoder?: () => prism.opus.Decoder;
onData: (pcm: Buffer) => void;
}
export class OpusDecoder {
private decoder: prism.opus.Decoder | null = null;
private disabledUntil = 0;
private createdAt = 0;
private readonly cooldownMs: number;
private readonly rotateMs: number;
private readonly createDecoderFn: () => prism.opus.Decoder;
private readonly onData: (pcm: Buffer) => void;
constructor(options: OpusDecoderOptions) {
this.cooldownMs = options.cooldownMs;
this.rotateMs = options.rotateMs;
this.onData = options.onData;
this.createDecoderFn =
options.createDecoder ??
(() => new prism.opus.Decoder({ frameSize: 960, channels: 2, rate: 48_000 }));
}
rotateIfNeeded(): void {
if (!this.decoder || this.rotateMs <= 0) return;
if (Date.now() - this.createdAt < this.rotateMs) return;
this.destroy();
this.ensureDecoder();
}
write(chunk: Buffer): void {
const decoder = this.ensureDecoder();
if (!decoder) return;
try {
decoder.write(chunk);
} catch (error) {
console.warn("[recorder] Opus decoder write failed, cooling down:", error);
this.coolDown();
}
}
destroy(): void {
if (!this.decoder) return;
this.decoder.removeAllListeners();
this.decoder.destroy();
this.decoder = null;
this.createdAt = 0;
}
private ensureDecoder(): prism.opus.Decoder | null {
if (this.decoder) return this.decoder;
if (Date.now() < this.disabledUntil) return null;
try {
const decoder = this.createDecoderFn();
decoder.on("data", this.onData);
decoder.on("error", (error) => {
console.warn("[recorder] Opus decoder error, cooling down:", error);
this.coolDown();
});
this.decoder = decoder;
this.createdAt = Date.now();
return decoder;
} catch (error) {
console.warn("[recorder] Opus decoder init failed, cooling down:", error);
this.disabledUntil = Date.now() + this.cooldownMs;
return null;
}
}
private coolDown(): void {
this.disabledUntil = Date.now() + this.cooldownMs;
this.destroy();
}
}
```
- [ ] **Step 4: Run tests and typecheck**
```bash
bun run test tests/recorder/decoder.test.ts
bun run typecheck
```
Expected: PASS.
- [ ] **Step 5: Commit**
```bash
git add src/recorder/decoder.ts tests/recorder/decoder.test.ts
git commit -m "refactor: extract opus decoder lifecycle"
```
---
### Task 6: Segment tests and implementation
**Files:**
- Create: `src/recorder/segment.ts`
- Create: `tests/recorder/segment.test.ts`
- [ ] **Step 1: Write tests for pure filename and rotation decision helpers**
Create `tests/recorder/segment.test.ts`:
```ts
import { describe, expect, it } from "vitest";
import { buildSegmentPaths, shouldRotateSegment } from "../../src/recorder/segment";
describe("buildSegmentPaths", () => {
it("creates matching ogg and json paths", () => {
expect(buildSegmentPaths("/tmp/user", 123)).toEqual({
filename: "/tmp/user/123.ogg",
jsonFilename: "/tmp/user/123.json",
});
});
});
describe("shouldRotateSegment", () => {
it("rotates only when segment limit is exceeded", () => {
expect(shouldRotateSegment(1_000, 1_499, 500)).toBe(false);
expect(shouldRotateSegment(1_000, 1_500, 500)).toBe(true);
expect(shouldRotateSegment(1_000, 2_000, 0)).toBe(false);
});
});
```
- [ ] **Step 2: Run failing test**
```bash
bun run test tests/recorder/segment.test.ts
```
Expected: FAIL because module does not exist.
- [ ] **Step 3: Implement segment helpers and manager**
Create `src/recorder/segment.ts`:
```ts
import fs from "node:fs";
import path from "node:path";
import prism from "prism-media";
import type { SegmentState } from "../types";
export function buildSegmentPaths(userDir: string, startTime: number): { filename: string; jsonFilename: string } {
return {
filename: path.join(userDir, `${startTime}.ogg`),
jsonFilename: path.join(userDir, `${startTime}.json`),
};
}
export function shouldRotateSegment(startTime: number, now: number, recordingSegmentMs: number): boolean {
return recordingSegmentMs > 0 && now - startTime >= recordingSegmentMs;
}
export class SegmentManager {
private currentSegment: SegmentState | null = null;
private segmentIndex = 0;
constructor(
private readonly userDir: string,
private readonly recordingSegmentMs: number,
) {}
open(oggPacketStream: NodeJS.ReadableStream): SegmentState {
const index = this.segmentIndex++;
const startTime = Date.now();
const { filename, jsonFilename } = buildSegmentPaths(this.userDir, startTime);
const oggStream = new prism.opus.OggLogicalBitstream({
opusHead: new prism.opus.OpusHead({ channelCount: 2, sampleRate: 48_000 }),
pageSizeControl: { maxPackets: 10 },
crc: true,
});
const out = fs.createWriteStream(filename);
oggPacketStream.pipe(oggStream).pipe(out);
this.currentSegment = { index, startTime, endTime: null, filename, jsonFilename, oggStream, out };
return this.currentSegment;
}
close(oggPacketStream: NodeJS.ReadableStream): SegmentState | null {
if (!this.currentSegment) return null;
const segment = this.currentSegment;
segment.endTime = Date.now();
oggPacketStream.unpipe(segment.oggStream);
segment.oggStream.end();
this.currentSegment = null;
return segment;
}
rotateIfNeeded(oggPacketStream: NodeJS.ReadableStream): SegmentState | null {
if (!this.currentSegment) return null;
if (!shouldRotateSegment(this.currentSegment.startTime, Date.now(), this.recordingSegmentMs)) return null;
this.close(oggPacketStream);
return this.open(oggPacketStream);
}
getCurrent(): SegmentState | null {
return this.currentSegment;
}
}
```
- [ ] **Step 4: Run tests and typecheck**
```bash
bun run test tests/recorder/segment.test.ts
bun run typecheck
```
Expected: PASS.
- [ ] **Step 5: Commit**
```bash
git add src/recorder/segment.ts tests/recorder/segment.test.ts
git commit -m "refactor: extract recording segment manager"
```
---
### Task 7: Audio stream helper
**Files:**
- Create: `src/recorder/audioStream.ts`
- [ ] **Step 1: Create stream helper**
Create `src/recorder/audioStream.ts`:
```ts
import { EndBehaviorType, type VoiceReceiver } from "@discordjs/voice";
export interface AudioStreamHandlers {
onPacket: (chunk: Buffer) => void;
onEnd: () => void;
onError: (error: Error) => void;
}
export function subscribeToAudioStream(
receiver: VoiceReceiver,
userId: string,
handlers: AudioStreamHandlers,
): NodeJS.ReadableStream {
const audioStream = receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 3_000,
},
});
audioStream.on("data", handlers.onPacket);
audioStream.on("end", handlers.onEnd);
audioStream.on("error", handlers.onError);
return audioStream;
}
```
- [ ] **Step 2: Run typecheck**
```bash
bun run typecheck
```
Expected: PASS.
- [ ] **Step 3: Commit**
```bash
git add src/recorder/audioStream.ts
git commit -m "refactor: extract audio stream subscription"
```
---
### Task 8: Refactor `src/recorder.ts` orchestration
**Files:**
- Modify: `src/recorder.ts`
- [ ] **Step 1: Replace inline helpers with modules**
Edit `src/recorder.ts`:
- Import `collectUserMetadata`, `createSegmentMetadata`.
- Import `OpusDecoder`.
- Import `SegmentManager`.
- Import `subscribeToAudioStream`.
- Use `config.recordingsDir`, `config.recordingSegmentMs`, `config.decoderRotateMs`, `config.decoderCooldownMs`.
- Keep `startRecording(client, channel)` and `stopRecording(guildId)` exports unchanged.
- Remove packet debug logging `Pkt #...`.
- Keep current global web update behavior via `globalThis as PcmBroadcaster`.
Core packet handler shape:
```ts
const broadcaster = globalThis as typeof globalThis & PcmBroadcaster;
const userMetadata = await collectUserMetadata(client, userId, channel);
const segmentManager = new SegmentManager(userDir, config.recordingSegmentMs);
const decoder = new OpusDecoder({
cooldownMs: config.decoderCooldownMs,
rotateMs: config.decoderRotateMs,
onData: (pcm) => {
if (!broadcaster.broadcastPcmToWeb) return;
const outBuf = Buffer.alloc(pcm.length / 4);
for (let i = 0; i < outBuf.length / 2; i++) {
outBuf.writeInt16LE(pcm.readInt16LE(i * 8), i * 2);
}
broadcaster.broadcastPcmToWeb(outBuf, userId);
},
});
const audioStream = subscribeToAudioStream(receiver, userId, {
onPacket: (chunk) => {
if (chunk.length < 8) return;
segmentManager.rotateIfNeeded(oggPacketStream);
if (!broadcaster.broadcastPcmToWeb) return;
decoder.rotateIfNeeded();
decoder.write(chunk);
},
onEnd: () => {
const segment = segmentManager.close(oggPacketStream);
decoder.destroy();
if (segment) {
const metadata = createSegmentMetadata(userMetadata, segment, sessionId, sessionStartTime, config.recordingSegmentMs);
fs.writeFileSync(segment.jsonFilename, JSON.stringify(metadata, null, 2));
}
broadcaster.updateActiveUser?.(userId, { username: userMetadata.username, avatar: userMetadata.avatarUrl, speaking: false });
},
onError: (error) => {
segmentManager.close(oggPacketStream);
decoder.destroy();
console.error(`[recorder] Audio Stream error ${userId}:`, error.message);
},
});
```
- [ ] **Step 2: Preserve metadata writes on segment finish**
If existing behavior writes JSON when `out` finishes, attach `out.on("finish", ...)` in `SegmentManager.open()` caller after opening current segment. Ensure every closed segment gets JSON metadata, including rotated segments.
- [ ] **Step 3: Run typecheck**
```bash
bun run typecheck
```
Expected: PASS. Fix type errors by narrowing types, not adding `any` unless third-party library lacks exported type.
- [ ] **Step 4: Run tests**
```bash
bun run test
```
Expected: PASS.
- [ ] **Step 5: Commit**
```bash
git add src/recorder.ts src/recorder/audioStream.ts src/recorder/decoder.ts src/recorder/metadata.ts src/recorder/segment.ts src/types.ts
git commit -m "refactor: modularize recorder orchestration"
```
---
### Task 9: Format and lint cleanup
**Files:**
- Modify: all formatted TypeScript/config files touched by Biome.
- [ ] **Step 1: Run formatter**
```bash
bun run format
```
Expected: files formatted.
- [ ] **Step 2: Run linter**
```bash
bun run lint
```
Expected: PASS or actionable warnings. Fix warnings that are in touched code.
- [ ] **Step 3: Run typecheck and tests**
```bash
bun run typecheck
bun run test
```
Expected: PASS.
- [ ] **Step 4: Commit**
```bash
git add .
git commit -m "style: format and lint codebase"
```
---
### Task 10: Manual runtime verification
**Files:**
- No required code changes unless verification finds bug.
- [ ] **Step 1: Start app**
Run:
```bash
bun run start
```
Expected: app starts, logs bot ready or fails only due missing env credentials.
- [ ] **Step 2: If env exists, verify recording flow**
Manual steps:
1. Join configured Discord voice channel.
2. Speak for >3 seconds.
3. Confirm `.ogg` file and `.json` metadata are created under `RECORDINGS_DIR`.
4. Keep speaking past `RECORDING_SEGMENT_MS`; confirm segment rotation creates multiple files.
5. Stop app with Ctrl-C; confirm graceful shutdown log.
- [ ] **Step 3: Commit fixes if needed**
```bash
git add src tests package.json biome.json vitest.config.ts tsconfig.json
git commit -m "fix: preserve recorder runtime behavior"
```
Only run if code changed.
---
### Task 11: Final verification
**Files:**
- No changes expected.
- [ ] **Step 1: Run full verification**
```bash
bun run format
bun run lint
bun run typecheck
bun run test
git status --short
```
Expected: formatter stable, lint PASS, typecheck PASS, tests PASS, git status clean or only intentional uncommitted runtime artifacts excluded by `.gitignore`.
- [ ] **Step 2: Review diff summary**
```bash
git log --oneline -8
git diff HEAD~8...HEAD --stat
```
Expected: commits show tooling, config, types, metadata, decoder, segment, stream, recorder refactor, formatting.
- [ ] **Step 3: Report result**
Report:
- Commands run and pass/fail status.
- Runtime verification status.
- Any remaining risks, especially Discord runtime behavior if not manually tested with credentials.
---
## Self-Review
- Spec coverage: tooling, config, shared types, metadata, decoder, segment, audio stream, recorder orchestration, tests, lint/format, and runtime verification are covered.
- Placeholder scan: no TBD/TODO placeholders.
- Type consistency: `UserMetadata`, `SegmentState`, `SegmentMetadata`, `PcmBroadcaster`, `OpusDecoder`, `SegmentManager`, and `subscribeToAudioStream` names are consistent across tasks.

View File

@@ -7,7 +7,7 @@
"dev": "bun --watch src/index.ts", "dev": "bun --watch src/index.ts",
"start": "bun src/index.ts", "start": "bun src/index.ts",
"typecheck": "tsc --noEmit", "typecheck": "tsc --noEmit",
"lint": "biome check .", "lint": "biome check --diagnostic-level=error .",
"format": "biome format --write .", "format": "biome format --write .",
"test": "vitest run" "test": "vitest run"
}, },

View File

@@ -1,4 +1,4 @@
// Configuration for the bot // Configuration for the bot
export const config = { export const config = {
verbose: process.argv.includes('-v') || process.argv.includes('--verbose'), verbose: process.argv.includes("-v") || process.argv.includes("--verbose"),
}; };

View File

@@ -1,12 +1,12 @@
import "./mock-crc"; import "./mock-crc";
import "libsodium-wrappers"; import "libsodium-wrappers";
import "@snazzah/davey"; 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 { 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 // Validasi environment variables
const token = process.env.DISCORD_TOKEN; const token = process.env.DISCORD_TOKEN;
@@ -39,13 +39,15 @@ client.on("ready", async () => {
if (!channel || channel.type !== "GUILD_VOICE") { if (!channel || channel.type !== "GUILD_VOICE") {
console.error( console.error(
`[bot] Voice channel not found or wrong type: ${voiceChannelId}` `[bot] Voice channel not found or wrong type: ${voiceChannelId}`,
); );
process.exit(1); process.exit(1);
} }
if (config.verbose) { if (config.verbose) {
console.log(`[bot] Joining voice channel: #${channel.name} (${channel.id})`); console.log(
`[bot] Joining voice channel: #${channel.name} (${channel.id})`,
);
} }
await startRecording(client, channel as any); await startRecording(client, channel as any);

View File

@@ -3,26 +3,37 @@ const CRC_TABLE = new Uint32Array(256);
for (let i = 0; i < 256; i++) { for (let i = 0; i < 256; i++) {
let r = i << 24; let r = i << 24;
for (let j = 0; j < 8; j++) { for (let j = 0; j < 8; j++) {
r = (r & 0x80000000) !== 0 ? ((r << 1) ^ 0x04c11db7) : (r << 1); r = (r & 0x80000000) !== 0 ? (r << 1) ^ 0x04c11db7 : r << 1;
} }
CRC_TABLE[i] = (r >>> 0); CRC_TABLE[i] = r >>> 0;
} }
const Module = require('module'); const Module = require("module");
const originalRequire = Module.prototype.require; const originalRequire = Module.prototype.require;
Module.prototype.require = function (id: string) { Module.prototype.require = function (id: string) {
if (id === 'node-crc') { if (id === "node-crc") {
return { return {
crc: function (width: number, reflectIn: boolean, poly: number, init: number, refOut: boolean, xorOut: number, unk1: number, unk2: number, buffer: Buffer) { crc: function (
width: number,
reflectIn: boolean,
poly: number,
init: number,
refOut: boolean,
xorOut: number,
unk1: number,
unk2: number,
buffer: Buffer,
) {
let crc = 0; let crc = 0;
for (let i = 0; i < buffer.length; i++) { for (let i = 0; i < buffer.length; i++) {
crc = ((crc << 8) >>> 0) ^ CRC_TABLE[((crc >>> 24) ^ buffer[i]) & 0xff]; crc =
((crc << 8) >>> 0) ^ CRC_TABLE[((crc >>> 24) ^ buffer[i]) & 0xff];
crc >>>= 0; crc >>>= 0;
} }
const result = Buffer.alloc(4); const result = Buffer.alloc(4);
result.writeUInt32BE(crc, 0); result.writeUInt32BE(crc, 0);
return result; return result;
} },
}; };
} }
return originalRequire.apply(this, arguments); return originalRequire.apply(this, arguments);

View File

@@ -1,6 +1,6 @@
import ffmpeg from "fluent-ffmpeg";
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
import ffmpeg from "fluent-ffmpeg";
const recordingsDir = process.env.RECORDINGS_DIR ?? "./recordings"; const recordingsDir = process.env.RECORDINGS_DIR ?? "./recordings";
@@ -57,13 +57,17 @@ async function startMuxingToAup3() {
// Check if OGG file is valid (not empty and has reasonable size) // Check if OGG file is valid (not empty and has reasonable size)
const oggStats = fs.statSync(oggPath); const oggStats = fs.statSync(oggPath);
if (oggStats.size === 0) { if (oggStats.size === 0) {
console.warn(`[muxer-aup3] Skipping empty OGG file: ${oggPath}`); console.warn(
`[muxer-aup3] Skipping empty OGG file: ${oggPath}`,
);
continue; continue;
} }
// Skip files that are too small (less than 1KB likely corrupted) // Skip files that are too small (less than 1KB likely corrupted)
if (oggStats.size < 1024) { if (oggStats.size < 1024) {
console.warn(`[muxer-aup3] Skipping too small OGG file (${oggStats.size} bytes): ${oggPath}`); console.warn(
`[muxer-aup3] Skipping too small OGG file (${oggStats.size} bytes): ${oggPath}`,
);
continue; continue;
} }
@@ -71,21 +75,30 @@ async function startMuxingToAup3() {
const oggBuffer = fs.readFileSync(oggPath); const oggBuffer = fs.readFileSync(oggPath);
const oggHeader = oggBuffer.slice(0, 4).toString(); const oggHeader = oggBuffer.slice(0, 4).toString();
if (oggHeader !== "OggS") { if (oggHeader !== "OggS") {
console.warn(`[muxer-aup3] Skipping invalid OGG file (bad header): ${oggPath}`); console.warn(
`[muxer-aup3] Skipping invalid OGG file (bad header): ${oggPath}`,
);
continue; continue;
} }
const meta: EventMetadata = JSON.parse(fs.readFileSync(jsonPath, "utf-8")); const meta: EventMetadata = JSON.parse(
fs.readFileSync(jsonPath, "utf-8"),
);
clips.push({ oggPath, jsonPath, meta }); clips.push({ oggPath, jsonPath, meta });
} catch (e) { } catch (e) {
console.error(`[muxer-aup3] Failed to read/parse JSON: ${jsonPath}`, e); console.error(
`[muxer-aup3] Failed to read/parse JSON: ${jsonPath}`,
e,
);
} }
} }
} }
} }
processedDirs++; processedDirs++;
const progress = ((processedDirs / items.length) * 100).toFixed(2); const progress = ((processedDirs / items.length) * 100).toFixed(2);
console.log(`[muxer-aup3] Scanning progress: ${progress}% (${processedDirs}/${items.length} directories)`); console.log(
`[muxer-aup3] Scanning progress: ${progress}% (${processedDirs}/${items.length} directories)`,
);
} }
} }
@@ -99,12 +112,16 @@ async function startMuxingToAup3() {
// Find the global start time // Find the global start time
const globalStartTime = clips[0].meta.startTime; const globalStartTime = clips[0].meta.startTime;
console.log(`[muxer-aup3] Found ${clips.length} clips. Base timestamp: ${globalStartTime}`); console.log(
`[muxer-aup3] Found ${clips.length} clips. Base timestamp: ${globalStartTime}`,
);
const command = ffmpeg(); const command = ffmpeg();
const filterParts: string[] = []; const filterParts: string[] = [];
console.log(`[muxer-aup3] Creating audio filters for ${clips.length} clips...`); console.log(
`[muxer-aup3] Creating audio filters for ${clips.length} clips...`,
);
clips.forEach((clip, index) => { clips.forEach((clip, index) => {
command.input(clip.oggPath); command.input(clip.oggPath);
@@ -117,22 +134,30 @@ async function startMuxingToAup3() {
const inputSpecifier = `[${index}:a]`; const inputSpecifier = `[${index}:a]`;
const outputSpecifier = `[pad${index}]`; const outputSpecifier = `[pad${index}]`;
filterParts.push(`${inputSpecifier}adelay=${delayMs}|${delayMs}${outputSpecifier}`); filterParts.push(
`${inputSpecifier}adelay=${delayMs}|${delayMs}${outputSpecifier}`,
);
const progress = (((index + 1) / clips.length) * 100).toFixed(2); const progress = (((index + 1) / clips.length) * 100).toFixed(2);
console.log(`[muxer-aup3] Filter creation progress: ${progress}% (${index + 1}/${clips.length} clips)`); console.log(
`[muxer-aup3] Filter creation progress: ${progress}% (${index + 1}/${clips.length} clips)`,
);
}); });
// Merge them using amix // Merge them using amix
const amixInputs = clips.map((_, i) => `[pad${i}]`).join(""); const amixInputs = clips.map((_, i) => `[pad${i}]`).join("");
// We add the amix command. dropout_transition=0 avoids volume drop when streams end. // 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]`); filterParts.push(
`${amixInputs}amix=inputs=${clips.length}:dropout_transition=0[out]`,
);
const timestamp = Date.now(); const timestamp = Date.now();
const wavFilename = path.join(recordingsDir, `muxed-${timestamp}.wav`); const wavFilename = path.join(recordingsDir, `muxed-${timestamp}.wav`);
const aup3Filename = path.join(recordingsDir, `muxed-${timestamp}.aup3`); const aup3Filename = path.join(recordingsDir, `muxed-${timestamp}.aup3`);
console.log(`[muxer-aup3] Combining clips to WAV. This might take a while...`); console.log(
`[muxer-aup3] Combining clips to WAV. This might take a while...`,
);
// Using fluent-ffmpeg's complexFilter // Using fluent-ffmpeg's complexFilter
command command
@@ -143,7 +168,9 @@ async function startMuxingToAup3() {
.save(wavFilename) .save(wavFilename)
.on("progress", (progress) => { .on("progress", (progress) => {
if (progress.percent) { if (progress.percent) {
console.log(`[muxer-aup3] WAV Progress: ${progress.percent.toFixed(2)}%`); console.log(
`[muxer-aup3] WAV Progress: ${progress.percent.toFixed(2)}%`,
);
} }
}) })
.on("end", () => { .on("end", () => {
@@ -156,7 +183,12 @@ async function startMuxingToAup3() {
}); });
} }
function createAup3Project(wavFilename: string, aup3Filename: string, clips: ClipInfo[], globalStartTime: number) { function createAup3Project(
wavFilename: string,
aup3Filename: string,
clips: ClipInfo[],
globalStartTime: number,
) {
try { try {
console.log(`[muxer-aup3] AUP3 Progress: Reading WAV file...`); console.log(`[muxer-aup3] AUP3 Progress: Reading WAV file...`);
@@ -168,7 +200,9 @@ function createAup3Project(wavFilename: string, aup3Filename: string, clips: Cli
// Duration = (file_size - 44) / (44100 * 2 * 2) for WAV // Duration = (file_size - 44) / (44100 * 2 * 2) for WAV
const duration = (wavSize - 44) / (44100 * 4); const duration = (wavSize - 44) / (44100 * 4);
console.log(`[muxer-aup3] AUP3 Progress: Calculating duration... ${duration.toFixed(2)}s`); console.log(
`[muxer-aup3] AUP3 Progress: Calculating duration... ${duration.toFixed(2)}s`,
);
console.log(`[muxer-aup3] AUP3 Progress: Creating XML structure...`); console.log(`[muxer-aup3] AUP3 Progress: Creating XML structure...`);
// Create AUP3 project XML structure // Create AUP3 project XML structure
@@ -205,8 +239,9 @@ function createAup3Project(wavFilename: string, aup3Filename: string, clips: Cli
console.log(`[muxer-aup3] AUP3 Progress: Creating clip info file...`); console.log(`[muxer-aup3] AUP3 Progress: Creating clip info file...`);
// Create a simple info file with clip details // Create a simple info file with clip details
const infoFilename = aup3Filename.replace('.aup3', '-info.txt'); const infoFilename = aup3Filename.replace(".aup3", "-info.txt");
const infoContent = clips.map((clip, index) => { const infoContent = clips
.map((clip, index) => {
const delayMs = clip.meta.startTime - globalStartTime; const delayMs = clip.meta.startTime - globalStartTime;
return `Clip ${index + 1}: return `Clip ${index + 1}:
User: ${clip.meta.username} (${clip.meta.userId}) User: ${clip.meta.username} (${clip.meta.userId})
@@ -215,7 +250,8 @@ function createAup3Project(wavFilename: string, aup3Filename: string, clips: Cli
Delay: ${delayMs}ms Delay: ${delayMs}ms
Duration: ${clip.meta.durationMs}ms Duration: ${clip.meta.durationMs}ms
File: ${path.basename(clip.oggPath)}`; File: ${path.basename(clip.oggPath)}`;
}).join('\n\n'); })
.join("\n\n");
fs.writeFileSync(infoFilename, infoContent, "utf-8"); fs.writeFileSync(infoFilename, infoContent, "utf-8");
@@ -226,7 +262,6 @@ function createAup3Project(wavFilename: string, aup3Filename: string, clips: Cli
console.log(`[muxer-aup3] Clip info saved to: ${infoFilename}`); console.log(`[muxer-aup3] Clip info saved to: ${infoFilename}`);
console.log(`[muxer-aup3] Total clips processed: ${clips.length}`); console.log(`[muxer-aup3] Total clips processed: ${clips.length}`);
console.log(`[muxer-aup3] Duration: ${duration.toFixed(2)} seconds`); console.log(`[muxer-aup3] Duration: ${duration.toFixed(2)} seconds`);
} catch (error) { } catch (error) {
console.error(`[muxer-aup3] Error creating AUP3 project:`, error); console.error(`[muxer-aup3] Error creating AUP3 project:`, error);
} }

View File

@@ -1,6 +1,6 @@
import ffmpeg from "fluent-ffmpeg";
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
import ffmpeg from "fluent-ffmpeg";
const recordingsDir = process.env.RECORDINGS_DIR ?? "./recordings"; const recordingsDir = process.env.RECORDINGS_DIR ?? "./recordings";
@@ -63,7 +63,9 @@ async function startMuxing() {
// Skip files that are too small (less than 1KB likely corrupted) // Skip files that are too small (less than 1KB likely corrupted)
if (oggStats.size < 1024) { if (oggStats.size < 1024) {
console.warn(`[muxer] Skipping too small OGG file (${oggStats.size} bytes): ${oggPath}`); console.warn(
`[muxer] Skipping too small OGG file (${oggStats.size} bytes): ${oggPath}`,
);
continue; continue;
} }
@@ -71,21 +73,30 @@ async function startMuxing() {
const oggBuffer = fs.readFileSync(oggPath); const oggBuffer = fs.readFileSync(oggPath);
const oggHeader = oggBuffer.slice(0, 4).toString(); const oggHeader = oggBuffer.slice(0, 4).toString();
if (oggHeader !== "OggS") { if (oggHeader !== "OggS") {
console.warn(`[muxer] Skipping invalid OGG file (bad header): ${oggPath}`); console.warn(
`[muxer] Skipping invalid OGG file (bad header): ${oggPath}`,
);
continue; continue;
} }
const meta: EventMetadata = JSON.parse(fs.readFileSync(jsonPath, "utf-8")); const meta: EventMetadata = JSON.parse(
fs.readFileSync(jsonPath, "utf-8"),
);
clips.push({ oggPath, jsonPath, meta }); clips.push({ oggPath, jsonPath, meta });
} catch (e) { } catch (e) {
console.error(`[muxer] Failed to read/parse JSON: ${jsonPath}`, e); console.error(
`[muxer] Failed to read/parse JSON: ${jsonPath}`,
e,
);
} }
} }
} }
} }
processedDirs++; processedDirs++;
const progress = ((processedDirs / items.length) * 100).toFixed(2); const progress = ((processedDirs / items.length) * 100).toFixed(2);
console.log(`[muxer] Scanning progress: ${progress}% (${processedDirs}/${items.length} directories)`); console.log(
`[muxer] Scanning progress: ${progress}% (${processedDirs}/${items.length} directories)`,
);
} }
} }
@@ -99,7 +110,9 @@ async function startMuxing() {
// Find the global start time // Find the global start time
const globalStartTime = clips[0].meta.startTime; const globalStartTime = clips[0].meta.startTime;
console.log(`[muxer] Found ${clips.length} clips. Base timestamp: ${globalStartTime}`); console.log(
`[muxer] Found ${clips.length} clips. Base timestamp: ${globalStartTime}`,
);
const command = ffmpeg(); const command = ffmpeg();
const filterParts: string[] = []; const filterParts: string[] = [];
@@ -117,16 +130,22 @@ async function startMuxing() {
const inputSpecifier = `[${index}:a]`; const inputSpecifier = `[${index}:a]`;
const outputSpecifier = `[pad${index}]`; const outputSpecifier = `[pad${index}]`;
filterParts.push(`${inputSpecifier}adelay=${delayMs}|${delayMs}${outputSpecifier}`); filterParts.push(
`${inputSpecifier}adelay=${delayMs}|${delayMs}${outputSpecifier}`,
);
const progress = (((index + 1) / clips.length) * 100).toFixed(2); const progress = (((index + 1) / clips.length) * 100).toFixed(2);
console.log(`[muxer] Filter creation progress: ${progress}% (${index + 1}/${clips.length} clips)`); console.log(
`[muxer] Filter creation progress: ${progress}% (${index + 1}/${clips.length} clips)`,
);
}); });
// Merge them using amix // Merge them using amix
const amixInputs = clips.map((_, i) => `[pad${i}]`).join(""); const amixInputs = clips.map((_, i) => `[pad${i}]`).join("");
// We add the amix command. dropout_transition=0 avoids volume drop when streams end. // 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]`); filterParts.push(
`${amixInputs}amix=inputs=${clips.length}:dropout_transition=0[out]`,
);
const outputFilename = path.join(recordingsDir, `muxed-${Date.now()}.mp3`); const outputFilename = path.join(recordingsDir, `muxed-${Date.now()}.mp3`);
@@ -143,7 +162,9 @@ async function startMuxing() {
} }
}) })
.on("end", () => { .on("end", () => {
console.log(`[muxer] Successfully muxed! Output saved to: ${outputFilename}`); console.log(
`[muxer] Successfully muxed! Output saved to: ${outputFilename}`,
);
}) })
.on("error", (err) => { .on("error", (err) => {
console.error(`[muxer] FFmpeg Error:`, err); console.error(`[muxer] FFmpeg Error:`, err);

View File

@@ -1,14 +1,13 @@
import { import {
AudioPlayer,
AudioPlayerStatus,
createAudioPlayer, createAudioPlayer,
createAudioResource, createAudioResource,
AudioPlayerStatus, StreamType,
VoiceConnection, VoiceConnection,
AudioPlayer,
StreamType
} from "@discordjs/voice"; } from "@discordjs/voice";
import { Readable } from "stream";
import prism from "prism-media"; import prism from "prism-media";
import { Readable } from "stream";
export class DiscordPlayer { export class DiscordPlayer {
private player: AudioPlayer; private player: AudioPlayer;
@@ -21,7 +20,7 @@ export class DiscordPlayer {
console.log("[player] Audio player is now playing!"); console.log("[player] Audio player is now playing!");
}); });
this.player.on("error", error => { this.player.on("error", (error) => {
console.error(`[player] Error: ${error.message}`); console.error(`[player] Error: ${error.message}`);
}); });
} }

View File

@@ -1,18 +1,18 @@
import fs from "fs";
import path from "path";
import { pipeline } from "stream/promises";
import { import {
EndBehaviorType, EndBehaviorType,
joinVoiceChannel,
VoiceConnectionStatus,
entersState, entersState,
getVoiceConnection, getVoiceConnection,
joinVoiceChannel,
VoiceConnectionStatus,
} from "@discordjs/voice"; } from "@discordjs/voice";
import type { VoiceChannel, Client } from "discord.js-selfbot-v13"; import type { Client, VoiceChannel } from "discord.js-selfbot-v13";
import fs from "fs";
import path from "path";
import prism from "prism-media"; import prism from "prism-media";
import { pipeline } from "stream/promises";
import { PacketFilter } from "./packetFilter";
import { config } from "./config"; import { config } from "./config";
import { PacketFilter } from "./packetFilter";
const recordingsDir = process.env.RECORDINGS_DIR ?? "./recordings"; const recordingsDir = process.env.RECORDINGS_DIR ?? "./recordings";
// Pastikan folder recordings ada // Pastikan folder recordings ada
@@ -23,7 +23,10 @@ if (!fs.existsSync(recordingsDir)) {
/** /**
* Join ke voice channel dan mulai merekam semua user yang bicara. * Join ke voice channel dan mulai merekam semua user yang bicara.
*/ */
export async function startRecording(client: Client, channel: VoiceChannel): Promise<void> { export async function startRecording(
client: Client,
channel: VoiceChannel,
): Promise<void> {
const connection = joinVoiceChannel({ const connection = joinVoiceChannel({
channelId: channel.id, channelId: channel.id,
guildId: channel.guild.id, guildId: channel.guild.id,
@@ -37,13 +40,13 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
console.log(`[recorder] Joining voice channel: #${channel.name}`); console.log(`[recorder] Joining voice channel: #${channel.name}`);
} }
connection.on('debug', msg => { connection.on("debug", (msg) => {
if (config.verbose) { if (config.verbose) {
console.log(`[voice-debug] ${msg}`); console.log(`[voice-debug] ${msg}`);
} }
}); });
connection.on('error', err => { connection.on("error", (err) => {
console.error(`[voice-error]`, err); console.error(`[voice-error]`, err);
}); });
@@ -64,15 +67,26 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
// Dengarkan siapapun yang mulai bicara // Dengarkan siapapun yang mulai bicara
receiver.speaking.on("start", async (userId) => { receiver.speaking.on("start", async (userId) => {
// Coba ambil data user dari cache atau fetch dari API // 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 user =
const member = channel.guild.members.cache.get(userId) || await channel.guild.members.fetch(userId).catch(() => null); 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 username = user?.username ?? "Unknown User";
const avatarUrl = user?.displayAvatarURL({ format: "png", size: 64 }) ?? "https://cdn.discordapp.com/embed/avatars/0.png"; const avatarUrl =
user?.displayAvatarURL({ format: "png", size: 64 }) ??
"https://cdn.discordapp.com/embed/avatars/0.png";
const displayName = member?.displayName ?? username; const displayName = member?.displayName ?? username;
const roles = member?.roles.cache const roles =
member?.roles.cache
.filter((role) => role.id !== channel.guild.id) .filter((role) => role.id !== channel.guild.id)
.sort((a, b) => b.position - a.position) .sort((a, b) => b.position - a.position)
.map((role) => ({ id: role.id, name: role.name, position: role.position })) ?? []; .map((role) => ({
id: role.id,
name: role.name,
position: role.position,
})) ?? [];
const highestRole = roles.length > 0 ? roles[0] : null; const highestRole = roles.length > 0 ? roles[0] : null;
const joinedTimestamp = member?.joinedTimestamp ?? null; const joinedTimestamp = member?.joinedTimestamp ?? null;
@@ -81,7 +95,11 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
// Notify webserver // Notify webserver
if ((global as any).updateActiveUser) { if ((global as any).updateActiveUser) {
(global as any).updateActiveUser(userId, { username, avatar: avatarUrl, speaking: true }); (global as any).updateActiveUser(userId, {
username,
avatar: avatarUrl,
speaking: true,
});
} }
// Jangan record kalau sudah ada stream aktif untuk user ini // Jangan record kalau sudah ada stream aktif untuk user ini
@@ -90,8 +108,11 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
const timestamp = Date.now(); const timestamp = Date.now();
const sessionStartTime = timestamp; const sessionStartTime = timestamp;
const sessionId = `${userId}-${sessionStartTime}`; const sessionId = `${userId}-${sessionStartTime}`;
const recordingSegmentMsRaw = Number(process.env.RECORDING_SEGMENT_MS ?? 5_000); const recordingSegmentMsRaw = Number(
const recordingSegmentMs = Number.isFinite(recordingSegmentMsRaw) && recordingSegmentMsRaw > 0 process.env.RECORDING_SEGMENT_MS ?? 5_000,
);
const recordingSegmentMs =
Number.isFinite(recordingSegmentMsRaw) && recordingSegmentMsRaw > 0
? recordingSegmentMsRaw ? recordingSegmentMsRaw
: 0; : 0;
const userDir = path.join(recordingsDir, userId); const userDir = path.join(recordingsDir, userId);
@@ -127,7 +148,10 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
const segmentFilename = path.join(userDir, `${startTime}.ogg`); const segmentFilename = path.join(userDir, `${startTime}.ogg`);
const segmentJsonFilename = path.join(userDir, `${startTime}.json`); const segmentJsonFilename = path.join(userDir, `${startTime}.json`);
const oggStream = new prism.opus.OggLogicalBitstream({ const oggStream = new prism.opus.OggLogicalBitstream({
opusHead: new prism.opus.OpusHead({ channelCount: 2, sampleRate: 48000 }), opusHead: new prism.opus.OpusHead({
channelCount: 2,
sampleRate: 48000,
}),
pageSizeControl: { maxPackets: 10 }, pageSizeControl: { maxPackets: 10 },
crc: true, crc: true,
}); });
@@ -167,9 +191,12 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
startTime: segment.startTime, startTime: segment.startTime,
endTime, endTime,
durationMs: endTime - segment.startTime, durationMs: endTime - segment.startTime,
filename: path.basename(segment.filename) filename: path.basename(segment.filename),
}; };
fs.writeFileSync(segment.jsonFilename, JSON.stringify(eventMetadata, null, 2)); fs.writeFileSync(
segment.jsonFilename,
JSON.stringify(eventMetadata, null, 2),
);
if (config.verbose) { if (config.verbose) {
console.log(`[recorder] Saved metadata: ${segment.jsonFilename}`); console.log(`[recorder] Saved metadata: ${segment.jsonFilename}`);
} }
@@ -202,7 +229,11 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
// --- Web broadcast: prism decoder with safe restart and cooldown --- // --- Web broadcast: prism decoder with safe restart and cooldown ---
// OpusScript can crash on long/invalid streams; avoid taking down the process. // OpusScript can crash on long/invalid streams; avoid taking down the process.
const decoderConfig = { frameSize: 960, channels: 2, rate: 48000 }; const decoderConfig = {
frameSize: 960,
channels: 2 as const,
rate: 48000 as const,
};
const decoderCooldownMs = 30_000; const decoderCooldownMs = 30_000;
const decoderRotateMs = Number(process.env.DECODER_ROTATE_MS ?? 5_000); const decoderRotateMs = Number(process.env.DECODER_ROTATE_MS ?? 5_000);
let currentDecoder: prism.opus.Decoder | null = null; let currentDecoder: prism.opus.Decoder | null = null;
@@ -231,8 +262,8 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
if (Date.now() < decoderDisabledUntil) return null; if (Date.now() < decoderDisabledUntil) return null;
try { try {
const d = new prism.opus.Decoder(decoderConfig); const d = new prism.opus.Decoder(decoderConfig);
d.on('data', handlePcm); d.on("data", handlePcm);
d.on('error', (err) => { d.on("error", (err) => {
console.warn("[recorder] Opus decoder error, cooling down:", err); console.warn("[recorder] Opus decoder error, cooling down:", err);
decoderDisabledUntil = Date.now() + decoderCooldownMs; decoderDisabledUntil = Date.now() + decoderCooldownMs;
destroyDecoder(); destroyDecoder();
@@ -240,7 +271,10 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
decoderCreatedAt = Date.now(); decoderCreatedAt = Date.now();
return d; return d;
} catch (err) { } catch (err) {
console.warn("[recorder] Opus decoder init failed, cooling down:", err); console.warn(
"[recorder] Opus decoder init failed, cooling down:",
err,
);
decoderDisabledUntil = Date.now() + decoderCooldownMs; decoderDisabledUntil = Date.now() + decoderCooldownMs;
return null; return null;
} }
@@ -262,10 +296,12 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
// Feed Opus packets one-by-one // Feed Opus packets one-by-one
let packetCount = 0; let packetCount = 0;
audioStream.on('data', (chunk: Buffer) => { audioStream.on("data", (chunk: Buffer) => {
packetCount++; packetCount++;
if (packetCount <= 5) { if (packetCount <= 5) {
console.log(`[recorder] Pkt #${packetCount} from ${userId}: ${chunk.length}b | 0x${chunk.slice(0,4).toString('hex')}`); 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 if (chunk.length < 8) return; // skip tiny control/DTX packets
rotateSegmentIfNeeded(); rotateSegmentIfNeeded();
@@ -276,28 +312,38 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
try { try {
decoder.write(chunk); decoder.write(chunk);
} catch (err) { } catch (err) {
console.warn("[recorder] Opus decoder write failed, cooling down:", err); console.warn(
"[recorder] Opus decoder write failed, cooling down:",
err,
);
decoderDisabledUntil = Date.now() + decoderCooldownMs; decoderDisabledUntil = Date.now() + decoderCooldownMs;
destroyDecoder(); destroyDecoder();
} }
}); });
audioStream.on('end', () => { audioStream.on("end", () => {
closeSegment(); closeSegment();
destroyDecoder(); destroyDecoder();
if ((global as any).updateActiveUser) { if ((global as any).updateActiveUser) {
(global as any).updateActiveUser(userId, { username, avatar: avatarUrl, speaking: false }); (global as any).updateActiveUser(userId, {
username,
avatar: avatarUrl,
speaking: false,
});
} }
}); });
audioStream.on('error', (err) => { audioStream.on("error", (err) => {
closeSegment(); closeSegment();
destroyDecoder(); destroyDecoder();
console.error(`[recorder] Audio Stream error ${userId}:`, err.message); console.error(`[recorder] Audio Stream error ${userId}:`, err.message);
}); });
packetFilterForOgg.on('error', (err) => { packetFilterForOgg.on("error", (err) => {
closeSegment(); closeSegment();
console.error(`[recorder] PacketFilter(ogg) error ${userId}:`, err.message); console.error(
`[recorder] PacketFilter(ogg) error ${userId}:`,
err.message,
);
}); });
} catch (e) { } catch (e) {
console.error(`[recorder] Failed to create stream for ${userId}:`, e); console.error(`[recorder] Failed to create stream for ${userId}:`, e);
@@ -307,7 +353,9 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
// Handle disconnect yang tidak disengaja // Handle disconnect yang tidak disengaja
connection.on(VoiceConnectionStatus.Disconnected, async () => { connection.on(VoiceConnectionStatus.Disconnected, async () => {
if (config.verbose) { if (config.verbose) {
console.warn("[recorder] Disconnected from voice channel. Reconnecting..."); console.warn(
"[recorder] Disconnected from voice channel. Reconnecting...",
);
} }
try { try {
await Promise.race([ await Promise.race([

View File

@@ -1,11 +1,14 @@
import express from "express"; import express from "express";
import http from "http"; import http from "http";
import { WebSocketServer } from "ws";
import path from "path"; import path from "path";
import prism from "prism-media"; import prism from "prism-media";
import { WebSocketServer } from "ws";
import { discordPlayer } from "./player"; 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>(); let wsClients = new Set<any>();
// Upsample 24kHz mono s16le → 48kHz stereo s16le (pure JS) // Upsample 24kHz mono s16le → 48kHz stereo s16le (pure JS)
@@ -39,7 +42,9 @@ export function startWebserver(port: number = 3000) {
const wsPort = port + 1; const wsPort = port + 1;
const wss = new WebSocketServer({ port: wsPort, host: "0.0.0.0" }); const wss = new WebSocketServer({ port: wsPort, host: "0.0.0.0" });
console.log(`[webserver] WebSocket server listening on ws://0.0.0.0:${wsPort}`); 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")));
@@ -47,18 +52,21 @@ export function startWebserver(port: number = 3000) {
(global as any).broadcastPcmToWeb = (chunk: Buffer, userId: string) => { (global as any).broadcastPcmToWeb = (chunk: Buffer, userId: string) => {
let hash = 0; let hash = 0;
for (let i = 0; i < userId.length; i++) { for (let i = 0; i < userId.length; i++) {
hash = ((hash << 5) - hash) + userId.charCodeAt(i); hash = (hash << 5) - hash + userId.charCodeAt(i);
hash |= 0; hash |= 0;
} }
const header = Buffer.alloc(4); const header = Buffer.alloc(4);
header.writeInt32LE(hash, 0); header.writeInt32LE(hash, 0);
const packet = Buffer.concat([header, chunk]); const packet = Buffer.concat([header, chunk]);
wsClients.forEach(client => { wsClients.forEach((client) => {
if (client.readyState === 1) client.send(packet); if (client.readyState === 1) client.send(packet);
}); });
}; };
(global as any).updateActiveUser = (userId: string, data: { username: string, avatar: string, speaking: boolean }) => { (global as any).updateActiveUser = (
userId: string,
data: { username: string; avatar: string; speaking: boolean },
) => {
activeUsers.set(userId, data); activeUsers.set(userId, data);
broadcastUserState(); broadcastUserState();
}; };
@@ -66,9 +74,12 @@ export function startWebserver(port: number = 3000) {
function broadcastUserState() { function broadcastUserState() {
const payload = JSON.stringify({ const payload = JSON.stringify({
type: "user_state", type: "user_state",
users: Array.from(activeUsers.entries()).map(([id, data]) => ({ id, ...data })) users: Array.from(activeUsers.entries()).map(([id, data]) => ({
id,
...data,
})),
}); });
wsClients.forEach(client => { wsClients.forEach((client) => {
if (client.readyState === 1) client.send(payload); if (client.readyState === 1) client.send(payload);
}); });
} }
@@ -81,13 +92,20 @@ export function startWebserver(port: number = 3000) {
const SILENCE_TAIL_MS = 300; // continue sending silence for 300ms after browser stops 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 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 opusEncoder = new prism.opus.Encoder({
rate: RATE,
channels: CHANNELS,
frameSize: FRAME_SIZE,
});
const oggBitstream = new prism.opus.OggLogicalBitstream({ const oggBitstream = new prism.opus.OggLogicalBitstream({
opusHead: new prism.opus.OpusHead({ channelCount: CHANNELS, sampleRate: RATE }), opusHead: new prism.opus.OpusHead({
channelCount: CHANNELS,
sampleRate: RATE,
}),
pageSizeControl: { maxPackets: 1 }, // 1 packet per page = 20ms latency pageSizeControl: { maxPackets: 1 }, // 1 packet per page = 20ms latency
crc: true, crc: true,
}); });
opusEncoder.on('error', () => {}); opusEncoder.on("error", () => {});
opusEncoder.pipe(oggBitstream); opusEncoder.pipe(oggBitstream);
// Prime OGG headers before player starts reading // Prime OGG headers before player starts reading
@@ -101,12 +119,16 @@ export function startWebserver(port: number = 3000) {
const SILENCE_FRAME = Buffer.alloc(BYTES_PER_FRAME, 0); const SILENCE_FRAME = Buffer.alloc(BYTES_PER_FRAME, 0);
// Log level every 2 seconds // Log level every 2 seconds
let dbAccum = 0, dbCount = 0; let dbAccum = 0,
dbCount = 0;
setInterval(() => { setInterval(() => {
if (dbCount > 0) { if (dbCount > 0) {
const avg = dbAccum / dbCount; const avg = dbAccum / dbCount;
console.log(`[transmit] Audio level: ${avg.toFixed(1)} dBFS (${dbCount} frames/2s)`); console.log(
dbAccum = 0; dbCount = 0; `[transmit] Audio level: ${avg.toFixed(1)} dBFS (${dbCount} frames/2s)`,
);
dbAccum = 0;
dbCount = 0;
} }
}, 2000); }, 2000);
@@ -146,7 +168,7 @@ export function startWebserver(port: number = 3000) {
// Write one frame. If encoder is backpressured, skip this tick to avoid stalling. // Write one frame. If encoder is backpressured, skip this tick to avoid stalling.
const ok = opusEncoder.write(frame); const ok = opusEncoder.write(frame);
if (!ok) { if (!ok) {
opusEncoder.once('drain', () => {}); // re-arm drain without blocking opusEncoder.once("drain", () => {}); // re-arm drain without blocking
} }
}, 20); }, 20);
@@ -154,10 +176,15 @@ export function startWebserver(port: number = 3000) {
console.log("[webserver] New WebSocket connection on port " + wsPort); console.log("[webserver] New WebSocket connection on port " + wsPort);
wsClients.add(ws); wsClients.add(ws);
ws.send(JSON.stringify({ ws.send(
JSON.stringify({
type: "user_state", type: "user_state",
users: Array.from(activeUsers.entries()).map(([id, data]) => ({ id, ...data })) users: Array.from(activeUsers.entries()).map(([id, data]) => ({
})); id,
...data,
})),
}),
);
ws.on("message", (data: any) => { ws.on("message", (data: any) => {
if (!Buffer.isBuffer(data)) return; if (!Buffer.isBuffer(data)) return;
@@ -172,11 +199,17 @@ export function startWebserver(port: number = 3000) {
} }
}); });
ws.on("close", () => { wsClients.delete(ws); }); ws.on("close", () => {
ws.on("error", () => { wsClients.delete(ws); }); wsClients.delete(ws);
});
ws.on("error", () => {
wsClients.delete(ws);
});
}); });
server.listen(port, "0.0.0.0", () => { server.listen(port, "0.0.0.0", () => {
console.log(`[webserver] Web interface listening on http://0.0.0.0:${port}`); console.log(
`[webserver] Web interface listening on http://0.0.0.0:${port}`,
);
}); });
} }

5
tests/smoke.test.ts Normal file
View File

@@ -0,0 +1,5 @@
import { expect, it } from "vitest";
it("runs the test suite", () => {
expect(true).toBe(true);
});

View File

@@ -9,11 +9,6 @@
"outDir": "dist", "outDir": "dist",
"rootDir": "src" "rootDir": "src"
}, },
"include": [ "include": ["src/**/*"],
"src/**/*" "exclude": ["node_modules", "dist"]
],
"exclude": [
"node_modules",
"dist"
]
} }

View File

@@ -1,8 +1,8 @@
import { defineConfig } from 'vitest/config'; import { defineConfig } from "vitest/config";
export default defineConfig({ export default defineConfig({
test: { test: {
environment: 'node', environment: "node",
include: ['tests/**/*.test.ts'] include: ["tests/**/*.test.ts"],
} },
}); });