This commit is contained in:
2021-04-06 02:16:09 +02:00
parent b444508fb8
commit 110b78c762
11 changed files with 88 additions and 22 deletions

View File

@@ -1,6 +1,5 @@
const Discord = require('discord.js');
require('dotenv').config();
const tts = require('google-tts-api');
const fetch = require('node-fetch');
const fs = require('fs');
const sha1 = require('sha1');
@@ -16,7 +15,19 @@ 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;
})(),
isInVoiceChannel: (channel) => {
return joinedVoiceChannels.includes(channel);
},
@@ -25,16 +36,11 @@ const api = {
return bot.voice.connections.find((conn) => conn.channel === channel);
},
generateVoice: async (string) => {
const hash = sha1(string);
const filepath = process.env.VOICE_TMP_PATH + hash + ".mp3";
generateVoice: async (string, engine, voice, params) => {
const hash = sha1(voice+string);
const filepath = process.env.VOICE_TMP_PATH + hash + '.' + engine.fileExtension;
if (!fs.existsSync(filepath)) {
const url = tts.getAudioUrl(string, {lang: "en-us"});
console.log("Generated url: " + url);
const data = await fetch(url);
const contents = await data.arrayBuffer();
const buf = Buffer.from(contents);
fs.writeFileSync(filepath, buf);
await engine.getSpeechFile(string, filepath, voice, params);
}
return filepath;
},
@@ -53,9 +59,9 @@ const api = {
}
},
speak: async (channel, message) => {
speak: async (channel, message, engine=api.ttsEngines.gtranslate, voice='en-us', params={}) => {
const conn = api.getConnectionForVoiceChannel(channel);
const filepath = await api.generateVoice(message);
const filepath = await api.generateVoice(message, engine, voice, params);
if (conn) conn.play(filepath);
},
@@ -65,9 +71,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;
modules.push(require(`./modules/${dir}/index.js`));
console.log(`Loading ./modules/${dir}/index.js`)
})