tardis-bot/AudioQueue.js

31 lines
841 B
JavaScript

const Voice = require("@discordjs/voice");
module.exports = class AudioQueue {
constructor(connection, api) {
this.connection = connection;
this.api = api;
this.queue = [];
this.current = undefined;
this.api.player.on(Voice.AudioPlayerStatus.Idle, this.handleStop.bind(this));
}
playNext() {
if (this.queue.length == 0) {
this.current = undefined;
return;
}
this.current = this.api.play(this.queue[0]);
}
handleStop(current) {
this.queue.shift();
this.playNext();
}
add(element) {
this.queue.push(element);
if (this.queue.length == 1) this.playNext();
}
flush() {
this.current.setVolume(0);
this.queue=[];
this.playNext();
}
}