Add audio queue

pull/1/head
guilevi 2021-04-09 13:23:13 +02:00
parent 1fe978eec6
commit 75dc0c2da2
5 changed files with 33 additions and 3 deletions

BIN
.DS_Store vendored

Binary file not shown.

23
AudioQueue.js 100644
View File

@ -0,0 +1,23 @@
module.exports = class AudioQueue {
constructor(connection) {
this.connection = connection;
this.queue = [];
this.current = undefined;
}
playNext() {
if (this.queue.length == 0) {
this.current = undefined;
return;
}
this.current = this.connection.play(this.queue[0]);
this.current.on('speaking', (val) => {if (val == 0) this.handleStop(this.current)});
}
handleStop(current) {
this.queue.shift();
this.playNext();
}
add(element) {
this.queue.push(element);
if (this.queue.length == 1) this.playNext();
}
}

View File

@ -21,6 +21,7 @@ async function initDB() {
const api = { const api = {
db: undefined, db: undefined,
queue: undefined,
ttsEngines: (() => { ttsEngines: (() => {
let engines = {}; let engines = {};
console.log(`Registering TTS engines...`); console.log(`Registering TTS engines...`);
@ -77,9 +78,8 @@ const api = {
}, },
speak: async (channel, message, engine = api.announcementEngine, voice = api.announcementVoice, params = {}) => { speak: async (channel, message, engine = api.announcementEngine, voice = api.announcementVoice, params = {}) => {
const conn = api.getConnectionForVoiceChannel(channel);
const filepath = await api.generateVoice(message, engine, voice, params); const filepath = await api.generateVoice(message, engine, voice, params);
if (conn) conn.play(filepath); api.queue.add(filepath);
}, },
registerCommand: async (commandString, commandFunc) => { registerCommand: async (commandString, commandFunc) => {

View File

@ -1,3 +1,5 @@
const AudioQueue=require('../../AudioQueue.js')
module.exports = function (bot, api) { module.exports = function (bot, api) {
bot.on('voiceStateUpdate', async (oldState, newState) => { bot.on('voiceStateUpdate', async (oldState, newState) => {
if (newState.member.user.bot) return; if (newState.member.user.bot) return;
@ -11,6 +13,8 @@ module.exports = function (bot, api) {
let joined = false; let joined = false;
if (!oldState.channel) { if (!oldState.channel) {
joined = true; joined = true;
let conn=api.getConnectionForVoiceChannel(channel);
api.queue=new AudioQueue(conn);
} }
let username = newState.member.displayName; let username = newState.member.displayName;

View File

@ -1,10 +1,13 @@
const AudioQueue=require('../../AudioQueue.js')
module.exports = function (bot, api) { module.exports = function (bot, api) {
bot.on('ready', async () => { bot.on('ready', async () => {
console.log("Bot initialized and listening"); console.log("Bot initialized and listening");
const guild = await bot.guilds.fetch(process.env.GUILD); const guild = await bot.guilds.fetch(process.env.GUILD);
const channel = await bot.channels.fetch(process.env.CHANNEL); const channel = await bot.channels.fetch(process.env.CHANNEL);
await api.joinChannel(channel); await api.joinChannel(channel);
let conn=api.getConnectionForVoiceChannel(channel);
api.queue=new AudioQueue(conn);
api.speak(channel, `Hey buddies! How does it feel like to be a buddy? Are you proud of being a buddy? Well that's great!'`); api.speak(channel, `Hey buddies! How does it feel like to be a buddy? Are you proud of being a buddy? Well that's great!'`);
}) })
} }