Fix formatting
parent
158ed0372f
commit
12a9f8fc53
20
index.js
20
index.js
|
@ -16,13 +16,13 @@ const db = new sqlite.Database(process.env.DB_FILE);
|
|||
const api = {
|
||||
db: db,
|
||||
ttsEngines: (() => {
|
||||
let 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;
|
||||
if (dir.startsWith('.')) return;
|
||||
eng = require(`./tts/${dir}/index.js`);
|
||||
engines[dir] = new eng;
|
||||
console.log(`Loading ./tts/${dir}/index.js`)
|
||||
})
|
||||
return engines;
|
||||
|
@ -31,8 +31,8 @@ const api = {
|
|||
announcementEngine: undefined,
|
||||
|
||||
respond: (message, text, voiceText) => {
|
||||
let toSend = message.member.displayName+", "+(voiceText ? voiceText : text);
|
||||
if(message.member.voice.channel) {
|
||||
let toSend = message.member.displayName + ", " + (voiceText ? voiceText : text);
|
||||
if (message.member.voice.channel) {
|
||||
api.speak(message.member.voice.channel, toSend);
|
||||
} else {
|
||||
message.reply(text);
|
||||
|
@ -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);
|
||||
|
@ -85,7 +85,7 @@ function registerModules() {
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
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);
|
||||
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.");
|
||||
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.");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
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);
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
};
|
|
@ -1,11 +1,11 @@
|
|||
const BaseEngine=require('../BaseEngine');
|
||||
const BaseEngine = require('../BaseEngine');
|
||||
const fetch = require('node-fetch');
|
||||
const querystring = require('querystring');
|
||||
|
||||
module.exports= class extends BaseEngine {
|
||||
module.exports = class extends BaseEngine {
|
||||
constructor() {
|
||||
super("IBM Watson TTS","ogg");
|
||||
this.voices={
|
||||
super("IBM Watson TTS", "ogg");
|
||||
this.voices = {
|
||||
'Michael': 'en-US_MichaelV3Voice',
|
||||
'Allison': 'en-US_AllisonV3Voice',
|
||||
'Kevin': 'en-US_KevinV3Voice',
|
||||
|
@ -14,12 +14,12 @@ module.exports= class extends BaseEngine {
|
|||
getDefaultVoice() {
|
||||
return 'Michael';
|
||||
}
|
||||
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;
|
||||
const opts={
|
||||
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;
|
||||
const opts = {
|
||||
method: "post",
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
|
@ -29,6 +29,6 @@ module.exports= class extends BaseEngine {
|
|||
text: text
|
||||
})
|
||||
};
|
||||
return fetch(url,opts);
|
||||
return fetch(url, opts);
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue