Fix Streamer class

This commit is contained in:
Christer Warén 2023-06-07 17:43:39 +03:00
parent d260c6ea4f
commit 9e165713ce

View File

@ -1,14 +1,15 @@
import logger from "../funcs/logger"; import logger from "../funcs/logger";
import { createAudioPlayer, createAudioResource, NoSubscriberBehavior } from "@discordjs/voice"; import { AudioPlayer, AudioPlayerStatus, createAudioPlayer, createAudioResource, NoSubscriberBehavior } from "@discordjs/voice";
import RadioClient from "../../Client"; import RadioClient from "../../Client";
import { station } from "./Stations"; import { station } from "./Stations";
export default class Streamer { export default class Streamer {
map: Map<string, any>; map: Map<string, AudioPlayer>;
mode: "auto" | "manual" = "manual"; mode: "auto" | "manual";
constructor() { constructor() {
this.map = new Map(); this.map = new Map();
this.mode = "manual";
} }
init(client: RadioClient){ init(client: RadioClient){
@ -28,9 +29,9 @@ export default class Streamer {
if(this.mode == "auto"){ if(this.mode == "auto"){
if(!client.stations) return; if(!client.stations) return;
client.stations.forEach((station: station) => { for(const station of client.stations){
this.play(station); this.play(station);
}); }
} }
} }
@ -52,44 +53,40 @@ export default class Streamer {
behaviors: { behaviors: {
noSubscriber: NoSubscriberBehavior.Play, noSubscriber: NoSubscriberBehavior.Play,
maxMissedFrames: Math.round(5000 / 20), maxMissedFrames: Math.round(5000 / 20),
},
});
} }
if(this.mode == "manual"){ });
} else {
audioPlayer = createAudioPlayer({ audioPlayer = createAudioPlayer({
behaviors: { behaviors: {
noSubscriber: NoSubscriberBehavior.Stop, noSubscriber: NoSubscriberBehavior.Stop,
maxMissedFrames: Math.round(5000 / 20), maxMissedFrames: Math.round(5000 / 20),
}, }
}); });
} }
this.map.set(station.name, audioPlayer); this.map.set(station.name, audioPlayer);
} }
const url = station.stream[station.stream.default]; const url = station.stream[station.stream.default];
const resource = createAudioResource(url); const resource = createAudioResource(url);
audioPlayer.play(resource); audioPlayer.play(resource);
audioPlayer audioPlayer
.on('playing', () => { .on(AudioPlayerStatus.Playing, () => {
logger('Streamer', station.name + " / " + "Playing"); logger('Streamer', station.name + " / " + "Playing");
}) })
.on('idle', () => { .on(AudioPlayerStatus.Idle, () => {
logger('Streamer', station.name + " / " + "Idle"); logger('Streamer', station.name + " / " + "Idle");
audioPlayer.removeAllListeners();
if(this.mode == "manual" && audioPlayer.subscribers.length == 0) return;
this.play(station);
}) })
.on('paused', () => { .on(AudioPlayerStatus.Paused, () => {
logger('Streamer', station.name + " / " + "Paused"); logger('Streamer', station.name + " / " + "Paused");
}) })
.on('buffering', () => { .on(AudioPlayerStatus.Buffering, () => {
logger('Streamer', station.name + " / " + "Buffering"); logger('Streamer', station.name + " / " + "Buffering");
}) })
.on('autopaused', () => { .on(AudioPlayerStatus.AutoPaused, () => {
logger('Streamer', station.name + " / " + "AutoPaused"); logger('Streamer', station.name + " / " + "AutoPaused");
}) })
.on('error', (error: string) => {
logger('Streamer', station.name + " / " + "Error" + "\n" + error);
});
return audioPlayer; return audioPlayer;
} }
@ -105,14 +102,14 @@ export default class Streamer {
listen(station: station) { listen(station: station) {
let audioPlayer = this.map.get(station.name); let audioPlayer = this.map.get(station.name);
if(!audioPlayer || this.mode == "manual" && audioPlayer.subscribers.length == 0) audioPlayer = this.play(station); if(!audioPlayer) audioPlayer = this.play(station);
return audioPlayer; return audioPlayer;
} }
leave(client: RadioClient) { leave(client: RadioClient) {
if(!client.stations) return; if(!client.stations) return;
client.stations.forEach((station: station) => { for(const station of client.stations){
this.stop(station.name); this.stop(station.name);
}); }
} }
}; };