eximiabots-radiox/src/client/classes/Streamer.js

71 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-09-11 14:10:50 +00:00
const {
createAudioPlayer,
createAudioResource
} = require("@discordjs/voice");
module.exports = class {
constructor() {
this.map = new Map();
this.stations = 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){
if(!client.stations) return;
client.stations.forEach(station => {
2021-09-11 20:03:20 +00:00
let audioPlayer = this.map.get(station.name);
if(!audioPlayer) {
audioPlayer = createAudioPlayer();
this.map.set(station.name, audioPlayer);
}
2021-09-11 14:10:50 +00:00
this.play(station);
});
}
2021-09-11 20:03:20 +00:00
refresh(client){
this.init(client);
}
2021-09-11 14:10:50 +00:00
play(station) {
2021-09-11 19:35:06 +00:00
const audioPlayer = this.map.get(station.name);
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 14:10:50 +00:00
this.map.delete(station.name);
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 14:10:50 +00:00
this.map.delete(station.name);
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
}
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 => {
let streamer = this.map.get(station.name);
streamer?.stop();
this.map.delete(station.name);
});
}
};