Files
dc-recorder/src/player.ts
T
MythEclipseandClaude Opus 4.7 29fcde69e4 fix: adapt code to modernized libraries
- Migrate validation.ts from class-transformer/class-validator to Zod
- Apply Biome auto-fixes (import sorting, nodejs protocol)
- Fix duplicate type identifier in validation.ts

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-15 04:25:06 +07:00

56 lines
1.1 KiB
TypeScript

import { Readable } from "node:stream";
import {
AudioPlayer,
AudioPlayerStatus,
createAudioPlayer,
createAudioResource,
StreamType,
VoiceConnection,
} from "@discordjs/voice";
export class DiscordPlayer {
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!");
});
this.player.on("error", (error) => {
console.error(`[player] Error: ${error.message}`);
});
}
public setConnection(connection: VoiceConnection) {
this.connection = connection;
this.connection.subscribe(this.player);
}
public playStream(stream: Readable) {
console.log("[player] Starting new audio stream...");
const resource = createAudioResource(stream, {
inputType: StreamType.OggOpus,
});
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();