diff --git a/index.js b/index.js index 5186a81..0c029ca 100644 --- a/index.js +++ b/index.js @@ -16,29 +16,29 @@ const db = new sqlite.Database(process.env.DB_FILE); const api = { db: db, ttsEngines: (() => { - let engines={}; - console.log(`Registering TTS engines...`); - const engineDirectories = fs.readdirSync('./tts'); - engineDirectories.forEach((dir) => { - if(dir.startsWith('.')) return; - eng=require(`./tts/${dir}/index.js`); - engines[dir]=new eng; - console.log(`Loading ./tts/${dir}/index.js`) - }) - return engines; + let engines = {}; + console.log(`Registering TTS engines...`); + const engineDirectories = fs.readdirSync('./tts'); + engineDirectories.forEach((dir) => { + if (dir.startsWith('.')) return; + eng = require(`./tts/${dir}/index.js`); + engines[dir] = new eng; + console.log(`Loading ./tts/${dir}/index.js`) + }) + return engines; })(), - announcementVoice: process.env.ANNOUNCEMENT_VOICE, - announcementEngine: undefined, - - respond: (message, text, voiceText) => { - let toSend = message.member.displayName+", "+(voiceText ? voiceText : text); - if(message.member.voice.channel) { - api.speak(message.member.voice.channel, toSend); - } else { - message.reply(text); - } - }, - + announcementVoice: process.env.ANNOUNCEMENT_VOICE, + announcementEngine: undefined, + + respond: (message, text, voiceText) => { + let toSend = message.member.displayName + ", " + (voiceText ? voiceText : text); + if (message.member.voice.channel) { + api.speak(message.member.voice.channel, toSend); + } else { + message.reply(text); + } + }, + isInVoiceChannel: (channel) => { return joinedVoiceChannels.includes(channel); }, @@ -48,7 +48,7 @@ const api = { }, generateVoice: async (string, engine, voice, params) => { - const hash = sha1(voice+string); + const hash = sha1(voice + string); const filepath = process.env.VOICE_TMP_PATH + hash + '.' + engine.fileExtension; if (!fs.existsSync(filepath)) { await engine.getSpeechFile(string, filepath, voice, params); @@ -70,7 +70,7 @@ 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); if (conn) conn.play(filepath); @@ -82,10 +82,10 @@ const api = { } function registerModules() { - console.log(`Registering modules...`); + console.log(`Registering modules...`); const moduleDirectories = fs.readdirSync('./modules'); moduleDirectories.forEach((dir) => { - if(dir.startsWith('.')) return; + if (dir.startsWith('.')) return; modules.push(require(`./modules/${dir}/index.js`)); console.log(`Loading ./modules/${dir}/index.js`) }) @@ -98,7 +98,7 @@ function handleMessage(message) { const command = args[0].substr(1, args[0].length); const execution = commandHandlers.get(command); if (command) { - if(execution) execution(args, message); + if (execution) execution(args, message); } } } diff --git a/modules/announcer/index.js b/modules/announcer/index.js index 5404380..a156bfe 100644 --- a/modules/announcer/index.js +++ b/modules/announcer/index.js @@ -1,4 +1,4 @@ -module.exports = function(bot, api) { +module.exports = function (bot, api) { bot.on('voiceStateUpdate', async (oldState, newState) => { if (newState.member.user.bot) return; if (oldState.channel && newState.channel) return; @@ -12,7 +12,7 @@ module.exports = function(bot, api) { if (!oldState.channel) { joined = true; } - + let username = newState.member.displayName; let str = ""; if (!joined) { diff --git a/modules/ttsSettings/index.js b/modules/ttsSettings/index.js index 1c52379..831b706 100644 --- a/modules/ttsSettings/index.js +++ b/modules/ttsSettings/index.js @@ -1,18 +1,18 @@ -module.exports = function(bot, api) { +module.exports = function (bot, api) { api.registerCommand('announcevoice', (args, message) => { let channel = bot.voice.connections.first().channel; - if(args.length>3) { + if (args.length > 3) { return api.respond(message, "You tried to change my voice with too many arguments."); } - if(api.ttsEngines[args[1]]) { - api.announcementEngine=api.ttsEngines[args[1]]; - if(api.announcementEngine.validateVoice(args[2])) { - api.announcementVoice=args[2]; - api.respond(message, "My new voice is "+api.announcementVoice+" from "+api.announcementEngine.longName); - } else { - api.announcementVoice=api.announcementEngine.getDefaultVoice(); - api.respond(message, "Your voice name was invalid, so I switched to the default voice ("+api.announcementVoice+") for "+api.announcementEngine.longName+" instead."); - } + if (api.ttsEngines[args[1]]) { + api.announcementEngine = api.ttsEngines[args[1]]; + if (api.announcementEngine.validateVoice(args[2])) { + api.announcementVoice = args[2]; + api.respond(message, "My new voice is " + api.announcementVoice + " from " + api.announcementEngine.longName); + } else { + api.announcementVoice = api.announcementEngine.getDefaultVoice(); + api.respond(message, "Your voice name was invalid, so I switched to the default voice (" + api.announcementVoice + ") for " + api.announcementEngine.longName + " instead."); + } } }); } \ No newline at end of file diff --git a/modules/welcomer/index.js b/modules/welcomer/index.js index 9dba07a..34db436 100644 --- a/modules/welcomer/index.js +++ b/modules/welcomer/index.js @@ -1,10 +1,10 @@ -module.exports = function(bot, api) { +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 guild = await bot.guilds.fetch(process.env.GUILD); const channel = await bot.channels.fetch(process.env.CHANNEL); await api.joinChannel(channel); - + 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 diff --git a/tts/BaseEngine/index.js b/tts/BaseEngine/index.js index 351feb3..b77af1a 100644 --- a/tts/BaseEngine/index.js +++ b/tts/BaseEngine/index.js @@ -1,21 +1,21 @@ -const fs=require('fs'); +const fs = require('fs'); -module.exports=class { - constructor(longName, fileExtension, supportedParameters=[]) { - this.longName=longName; - this.fileExtension=fileExtension; +module.exports = class { + constructor(longName, fileExtension, supportedParameters = []) { + this.longName = longName; + this.fileExtension = fileExtension; } getInternalVoiceName(str) { - return this.voices?this.voices[str]:str; + return this.voices ? this.voices[str] : str; } - getDefaultVoice() {} - validateVoice(voice) {return this.voices ? this.voices[voice] : true;} - async getSpeech(text, voice=this.getDefaultVoice(), params) {} + getDefaultVoice() { } + validateVoice(voice) { return this.voices ? this.voices[voice] : true; } + async getSpeech(text, voice = this.getDefaultVoice(), params) { } async getSpeechFile(text, filepath, voice, params) { const data = await this.getSpeech(text, voice, params); - const contents = await data.arrayBuffer(); - const buf = Buffer.from(contents); - fs.writeFileSync(filepath, buf); + const contents = await data.arrayBuffer(); + const buf = Buffer.from(contents); + fs.writeFileSync(filepath, buf); return filepath; } } \ No newline at end of file diff --git a/tts/espeak/index.js b/tts/espeak/index.js index bec0bf6..2af1502 100644 --- a/tts/espeak/index.js +++ b/tts/espeak/index.js @@ -1,15 +1,15 @@ -const BaseEngine=require('../BaseEngine') -const {spawn} = require('child_process') +const BaseEngine = require('../BaseEngine') +const { spawn } = require('child_process') -module.exports=class extends BaseEngine { +module.exports = class extends BaseEngine { constructor() { - super('ESpeak','wav') + super('ESpeak', 'wav') } getDefaultVoice() { return 'en'; } - async getSpeechFile(text, filepath, voice=this.getDefaultVoice(), params={}) { - let proc=await spawn('espeak', ['-v', voice, '-w',filepath, '--stdin']); + async getSpeechFile(text, filepath, voice = this.getDefaultVoice(), params = {}) { + let proc = await spawn('espeak', ['-v', voice, '-w', filepath, '--stdin']); proc.stdin.end(text); } } \ No newline at end of file diff --git a/tts/gtranslate/index.js b/tts/gtranslate/index.js index ab4174e..3134583 100644 --- a/tts/gtranslate/index.js +++ b/tts/gtranslate/index.js @@ -1,16 +1,16 @@ -const BaseEngine=require('../BaseEngine'); +const BaseEngine = require('../BaseEngine'); const fetch = require('node-fetch'); const tts = require('google-tts-api'); -module.exports= class extends BaseEngine { +module.exports = class extends BaseEngine { constructor() { - super("Google Translate TTS","mp3"); + super("Google Translate TTS", "mp3"); } getDefaultVoice() { return 'en-us'; } - async getSpeech(text, voice=this.getDefaultVoice(), params={}) { - const url = tts.getAudioUrl(text, {lang: voice}); + async getSpeech(text, voice = this.getDefaultVoice(), params = {}) { + const url = tts.getAudioUrl(text, { lang: voice }); return fetch(url); } }; \ No newline at end of file diff --git a/tts/watson/index.js b/tts/watson/index.js index c8cad47..86657e7 100644 --- a/tts/watson/index.js +++ b/tts/watson/index.js @@ -5,6 +5,7 @@ const querystring = require('querystring'); module.exports = class extends BaseEngine { constructor() { super("IBM Watson TTS", "ogg"); +<<<<<<< HEAD this.voices = {}; this.populateVoiceList(); } @@ -16,6 +17,12 @@ module.exports = class extends BaseEngine { headers: { 'Authorization': authorization }, +======= + this.voices = { + 'Michael': 'en-US_MichaelV3Voice', + 'Allison': 'en-US_AllisonV3Voice', + 'Kevin': 'en-US_KevinV3Voice', +>>>>>>> 12a9f8fc5374d912e595cea204998daa4d7220ba }; const res = await fetch(url, opts); const voices = await res.json(); @@ -27,6 +34,7 @@ module.exports = class extends BaseEngine { getDefaultVoice() { return 'Michael'; } +<<<<<<< HEAD IBMAuthString() { let buff = new Buffer('apikey:' + process.env.watsonAPIKey); let b64auth = buff.toString('base64'); @@ -35,6 +43,13 @@ module.exports = class extends BaseEngine { async getSpeech(text, voice = this.getSpeechVoice(), params = {}) { const url = process.env.watsonURL + "/v1/synthesize?voice=" + this.getInternalVoiceName(voice); const authorization = this.IBMAuthString(); +======= + async getSpeech(text, voice = this.getSpeechVoice(), params = {}) { + const url = process.env.watsonURL + "/v1/synthesize?voice=" + this.getInternalVoiceName(voice); + let buff = new Buffer('apikey:' + process.env.watsonAPIKey); + let b64auth = buff.toString('base64'); + const authorization = 'Basic ' + b64auth; +>>>>>>> 12a9f8fc5374d912e595cea204998daa4d7220ba const opts = { method: "post", headers: {