From 75dc0c2da215cd8705916d4411b59ea143f3df41 Mon Sep 17 00:00:00 2001 From: guilevi Date: Fri, 9 Apr 2021 13:23:13 +0200 Subject: [PATCH] Add audio queue --- .DS_Store | Bin 8196 -> 8196 bytes AudioQueue.js | 23 +++++++++++++++++++++++ index.js | 4 ++-- modules/announcer/index.js | 4 ++++ modules/welcomer/index.js | 5 ++++- 5 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 AudioQueue.js diff --git a/.DS_Store b/.DS_Store index 0e829f6ef9dea89922292951d8ea2d0c46b6211a..d116d56fef87c294dd28532e8cbb973f2609e239 100644 GIT binary patch delta 30 lcmZp1XmQxkE;4zsz}d|~qMMmEvr90tPF4~00rEC50RXeK3T^-Z delta 30 lcmZp1XmQxkE;9Lt=;_TNqMMmEvq~_tPOcR50`fL70RX-(3nTyl diff --git a/AudioQueue.js b/AudioQueue.js new file mode 100644 index 0000000..09771f7 --- /dev/null +++ b/AudioQueue.js @@ -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(); + } +} \ No newline at end of file diff --git a/index.js b/index.js index f21822e..6f0f03c 100644 --- a/index.js +++ b/index.js @@ -21,6 +21,7 @@ async function initDB() { const api = { db: undefined, + queue: undefined, ttsEngines: (() => { let engines = {}; console.log(`Registering TTS engines...`); @@ -77,9 +78,8 @@ const api = { }, 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); - if (conn) conn.play(filepath); + api.queue.add(filepath); }, registerCommand: async (commandString, commandFunc) => { diff --git a/modules/announcer/index.js b/modules/announcer/index.js index a156bfe..bc09fe4 100644 --- a/modules/announcer/index.js +++ b/modules/announcer/index.js @@ -1,3 +1,5 @@ +const AudioQueue=require('../../AudioQueue.js') + module.exports = function (bot, api) { bot.on('voiceStateUpdate', async (oldState, newState) => { if (newState.member.user.bot) return; @@ -11,6 +13,8 @@ module.exports = function (bot, api) { let joined = false; if (!oldState.channel) { joined = true; + let conn=api.getConnectionForVoiceChannel(channel); + api.queue=new AudioQueue(conn); } let username = newState.member.displayName; diff --git a/modules/welcomer/index.js b/modules/welcomer/index.js index 34db436..1deb600 100644 --- a/modules/welcomer/index.js +++ b/modules/welcomer/index.js @@ -1,10 +1,13 @@ +const AudioQueue=require('../../AudioQueue.js') + module.exports = function (bot, api) { bot.on('ready', async () => { console.log("Bot initialized and listening"); const guild = await bot.guilds.fetch(process.env.GUILD); const channel = await bot.channels.fetch(process.env.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!'`); }) } \ No newline at end of file