2021-09-11 14:10:50 +00:00
|
|
|
const {
|
|
|
|
createAudioPlayer,
|
|
|
|
createAudioResource
|
|
|
|
} = require("@discordjs/voice");
|
|
|
|
|
|
|
|
module.exports = class {
|
|
|
|
constructor() {
|
|
|
|
this.map = new Map();
|
2021-09-12 14:39:23 +00:00
|
|
|
this.mode = null;
|
2021-09-11 15:27:01 +00:00
|
|
|
this.logger = require("../funcs/logger.js");
|
2021-09-11 14:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init(client){
|
2021-09-12 14:09:32 +00:00
|
|
|
if(!client.config.streamerMode) return;
|
|
|
|
|
|
|
|
switch(client.config.streamerMode){
|
|
|
|
case "manual":
|
|
|
|
this.mode = "manual";
|
2021-09-12 14:39:23 +00:00
|
|
|
break;
|
2021-09-12 14:09:32 +00:00
|
|
|
default:
|
|
|
|
this.mode = "auto";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.mode == "auto"){
|
|
|
|
if(!client.stations) return;
|
|
|
|
|
|
|
|
client.stations.forEach(station => {
|
|
|
|
this.play(station);
|
|
|
|
});
|
|
|
|
}
|
2021-09-11 14:10:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 20:03:20 +00:00
|
|
|
refresh(client){
|
|
|
|
this.init(client);
|
2021-09-11 20:12:39 +00:00
|
|
|
|
|
|
|
let streamers = this.map.keys();
|
|
|
|
streamers.forEach(streamer => {
|
|
|
|
if(client.stations.findIndex(station => station.name == streamer)) return;
|
2021-09-11 20:19:49 +00:00
|
|
|
this.stop(streamer);
|
2021-09-11 20:12:39 +00:00
|
|
|
});
|
2021-09-11 20:03:20 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 14:10:50 +00:00
|
|
|
play(station) {
|
2021-09-12 14:39:23 +00:00
|
|
|
let audioPlayer = this.map.get(station.name);
|
|
|
|
if(!audioPlayer) {
|
|
|
|
audioPlayer = createAudioPlayer();
|
|
|
|
this.map.set(station.name, audioPlayer);
|
|
|
|
}
|
2021-09-11 14:34:50 +00:00
|
|
|
const url = station.stream[station.stream.default];
|
2021-09-11 14:10:50 +00:00
|
|
|
const resource = createAudioResource(url);
|
|
|
|
audioPlayer.play(resource);
|
|
|
|
resource.playStream
|
|
|
|
.on("readable", () => {
|
2021-09-11 15:27:01 +00:00
|
|
|
this.logger('Streamer', station.name + " / " + "Readable");
|
2021-09-11 14:10:50 +00:00
|
|
|
this.map.set(station.name, audioPlayer);
|
|
|
|
})
|
|
|
|
.on("finish", () => {
|
2021-09-11 15:27:01 +00:00
|
|
|
this.logger('Streamer', station.name + " / " + "Finished");
|
2021-09-11 19:35:06 +00:00
|
|
|
this.play(station);
|
2021-09-11 14:10:50 +00:00
|
|
|
})
|
|
|
|
.on("error", error => {
|
2021-09-11 15:27:01 +00:00
|
|
|
this.logger('Streamer', station.name + " / " + "Error");
|
2021-09-11 19:35:06 +00:00
|
|
|
this.play(station);
|
2021-09-11 14:10:50 +00:00
|
|
|
});
|
2021-09-11 15:01:53 +00:00
|
|
|
return audioPlayer;
|
2021-09-11 14:10:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 20:19:49 +00:00
|
|
|
stop(station){
|
|
|
|
let audioPlayer = this.map.get(station.name);
|
2021-09-14 14:12:30 +00:00
|
|
|
if(audioPlayer){
|
|
|
|
this.logger('Streamer', station.name + " / " + "Stop");
|
|
|
|
audioPlayer.stop();
|
|
|
|
}
|
2021-09-11 20:19:49 +00:00
|
|
|
this.map.delete(station.name);
|
|
|
|
}
|
|
|
|
|
2021-09-11 14:10:50 +00:00
|
|
|
listen(station) {
|
|
|
|
let audioPlayer = this.map.get(station.name);
|
|
|
|
if(!audioPlayer){
|
2021-09-11 15:01:53 +00:00
|
|
|
audioPlayer = this.play(station);
|
2021-09-11 14:10:50 +00:00
|
|
|
}
|
|
|
|
return audioPlayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
leave(client) {
|
|
|
|
if(!client.stations) return;
|
|
|
|
|
|
|
|
client.stations.forEach(station => {
|
2021-09-11 20:19:49 +00:00
|
|
|
this.stop(station);
|
2021-09-11 14:10:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|