mirror of
https://github.com/warengroup/eximiabots-radiox.git
synced 2024-12-23 06:33:18 +00:00
Create Streamer class
This commit is contained in:
parent
45923319a6
commit
8bf79c2744
61
src/client/classes/Streamer.js
Normal file
61
src/client/classes/Streamer.js
Normal file
@ -0,0 +1,61 @@
|
||||
const {
|
||||
createAudioPlayer,
|
||||
createAudioResource
|
||||
} = require("@discordjs/voice");
|
||||
const {
|
||||
logger
|
||||
} = require("../funcs/logger.js");
|
||||
|
||||
module.exports = class {
|
||||
constructor() {
|
||||
this.map = new Map();
|
||||
this.stations = null;
|
||||
}
|
||||
|
||||
init(client){
|
||||
if(!client.stations) return;
|
||||
|
||||
client.stations.forEach(station => {
|
||||
const url = station.stream[station.stream.default];
|
||||
this.play(station);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
play(station) {
|
||||
const audioPlayer = createAudioPlayer();
|
||||
const resource = createAudioResource(url);
|
||||
audioPlayer.play(resource);
|
||||
resource.playStream
|
||||
.on("readable", () => {
|
||||
logger('Streamer', station.name + " / " + "Readable");
|
||||
this.map.set(station.name, audioPlayer);
|
||||
})
|
||||
.on("finish", () => {
|
||||
logger('Streamer', station.name + " / " + "Finished");
|
||||
this.map.delete(station.name);
|
||||
})
|
||||
.on("error", error => {
|
||||
logger('Streamer', station.name + " / " + "Error");
|
||||
this.map.delete(station.name);
|
||||
});
|
||||
}
|
||||
|
||||
listen(station) {
|
||||
let audioPlayer = this.map.get(station.name);
|
||||
if(!audioPlayer){
|
||||
this.play(station);
|
||||
}
|
||||
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);
|
||||
});
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user