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

131 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-09-11 14:10:50 +00:00
const {
createAudioPlayer,
2021-09-15 14:25:55 +00:00
createAudioResource,
AudioPlayerStatus,
NoSubscriberBehavior
2021-09-11 14:10:50 +00:00
} = 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-15 22:21:33 +00:00
case "auto":
2021-09-12 14:09:32 +00:00
this.mode = "auto";
2021-09-15 22:21:33 +00:00
break;
default:
this.mode = "manual";
2021-09-12 14:09:32 +00:00
}
if(this.mode == "auto"){
2021-09-16 02:35:26 +00:00
if(!client.stations) return;
2021-09-12 14:09:32 +00:00
2021-09-16 02:35:26 +00:00
client.stations.forEach(station => {
2021-09-12 14:09:32 +00:00
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 => {
2021-09-18 12:09:34 +00:00
if(client.stations.findIndex(station => station.name == streamer) == -1){
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) {
2021-11-16 14:55:47 +00:00
if(this.mode == "auto"){
audioPlayer = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Play,
maxMissedFrames: Math.round(5000 / 20),
},
});
}
if(this.mode == "manual"){
audioPlayer = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Stop,
maxMissedFrames: Math.round(5000 / 20),
},
});
}
2021-09-12 14:39:23 +00:00
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
})
.on("finish", () => {
2021-09-11 15:27:01 +00:00
this.logger('Streamer', station.name + " / " + "Finished");
2021-09-11 14:10:50 +00:00
})
.on("error", error => {
2021-10-25 23:35:18 +00:00
this.logger('Streamer', station.name + " / " + "Error" + "\n" + error);
2021-09-11 14:10:50 +00:00
});
2021-09-15 14:25:55 +00:00
audioPlayer
.on('playing', () => {
this.logger('Streamer', station.name + " / " + "Playing");
})
.on('idle', () => {
this.logger('Streamer', station.name + " / " + "Idle");
})
.on('paused', () => {
this.logger('Streamer', station.name + " / " + "Paused");
})
.on('buffering', () => {
this.logger('Streamer', station.name + " / " + "Buffering");
})
.on('autopaused', () => {
this.logger('Streamer', station.name + " / " + "AutoPaused");
})
2021-09-15 22:11:31 +00:00
.on('error', error => {
2021-09-15 14:25:55 +00:00
this.logger('Streamer', station.name + " / " + "Error" + "\n" + error);
});
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) {
2021-09-16 02:35:26 +00:00
if(!client.stations) return;
2021-09-11 14:10:50 +00:00
2021-09-16 02:35:26 +00:00
client.stations.forEach(station => {
2021-09-11 20:19:49 +00:00
this.stop(station);
2021-09-11 14:10:50 +00:00
});
}
};