2026-05-15 04:25:06 +07:00
|
|
|
import { Readable } from "node:stream";
|
2026-05-13 15:28:25 +07:00
|
|
|
import {
|
|
|
|
|
AudioPlayer,
|
|
|
|
|
AudioPlayerStatus,
|
|
|
|
|
createAudioPlayer,
|
|
|
|
|
createAudioResource,
|
|
|
|
|
StreamType,
|
|
|
|
|
VoiceConnection,
|
2026-05-13 00:32:27 +07:00
|
|
|
} from "@discordjs/voice";
|
2026-05-13 01:03:46 +07:00
|
|
|
|
2026-05-13 00:32:27 +07:00
|
|
|
export class DiscordPlayer {
|
2026-05-13 15:28:25 +07:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-15 17:17:17 +07:00
|
|
|
public isConnected(): boolean {
|
|
|
|
|
return this.connection !== null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
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();
|
|
|
|
|
}
|
2026-05-13 00:32:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const discordPlayer = new DiscordPlayer();
|