From 265757e2f50dce32ecb85267c2d6362a4a1ec792 Mon Sep 17 00:00:00 2001 From: loicbersier Date: Fri, 4 Sep 2020 13:36:08 +0200 Subject: [PATCH] waaay better Signed-off-by: loicbersier --- commands/fun/tts/dectalk.js | 49 +++++++++--------- commands/fun/tts/dectalkvc.js | 93 +++++++++++++++++------------------ 2 files changed, 71 insertions(+), 71 deletions(-) diff --git a/commands/fun/tts/dectalk.js b/commands/fun/tts/dectalk.js index 8353f378..d0ad28b2 100644 --- a/commands/fun/tts/dectalk.js +++ b/commands/fun/tts/dectalk.js @@ -1,6 +1,6 @@ const { Command } = require('discord-akairo'); -const util = require('util'); -const exec = util.promisify(require('child_process').exec); +const { execFile } = require('child_process'); +const os = require('os'); const rand = require('../../../rand.js'); class dectalkCommand extends Command { @@ -29,31 +29,36 @@ class dectalkCommand extends Command { async exec(message, args) { args.decMessage = rand.random(args.decMessage, message); - args.decMessage = args.decMessage.replace('\n', ' '); - let decMessage = '[:phoneme on] ' + args.decMessage.replace(/(["'$`\\])/g,'\\$1'); + let output = `${os.tmpdir()}/${message.id}_dectalk.wav`; + let decMessage = '[:phoneme on]' + args.decMessage; + let loadingmsg = await message.channel.send('Processing ( this can take some time ) '); - if (process.platform == 'win32') { - exec(`cd .\\dectalk && .\\say.exe -w ${message.id}_dectalk.wav "${decMessage}"`) - .catch(err => { - console.error(err); + if (process.platform === 'win32') { + execFile('say.exe', ['-w', output, `${decMessage}`], {cwd: './dectalk/'}, (error, stdout, stderr) => { + if (error) { + loadingmsg.delete(); + console.error(stdout); + console.error(stderr); + console.error(error); return message.channel.send('Oh no! an error has occurred!'); - }) - .then(() => { - return message.channel.send({files: [`./dectalk/${message.id}_dectalk.wav`]}); - }); - } else if (process.platform == 'linux' || process.platform == 'darwin') { - let loadingmsg = await message.channel.send('Processing ( this can take some time ) '); + } - exec(`cd dectalk && DISPLAY=:0.0 wine say.exe -w ${message.id}_dectalk.wav "${decMessage}"`) - .catch(err => { + loadingmsg.delete(); + return message.channel.send({files: [output]}); + }); + } else if (process.platform === 'linux' || process.platform === 'darwin') { + execFile('wine', ['say.exe', '-w', output, `${decMessage}`], {cwd: './dectalk/'}, (error, stdout, stderr) => { + if (error) { loadingmsg.delete(); - console.error(err); + console.error(stdout); + console.error(stderr); + console.error(error); return message.channel.send('Oh no! an error has occurred!'); - }) - .then(() => { - loadingmsg.delete(); - return message.channel.send({files: [`./dectalk/${message.id}_dectalk.wav`]}); - }); + } + + loadingmsg.delete(); + return message.channel.send({files: [output]}); + }); } } } diff --git a/commands/fun/tts/dectalkvc.js b/commands/fun/tts/dectalkvc.js index efb9b083..9218fbc8 100644 --- a/commands/fun/tts/dectalkvc.js +++ b/commands/fun/tts/dectalkvc.js @@ -1,6 +1,6 @@ const { Command } = require('discord-akairo'); -const util = require('util'); -const exec = util.promisify(require('child_process').exec); +const { execFile } = require('child_process'); +const os = require('os'); const rand = require('../../../rand.js'); class dectalkvcCommand extends Command { @@ -29,58 +29,53 @@ class dectalkvcCommand extends Command { async exec(message, args) { args.decMessage = rand.random(args.decMessage, message); - args.decMessage = args.decMessage.replace('\n', ' '); - let decMessage = '[:phoneme on] ' + args.decMessage.replace(/(["'$`\\])/g,'\\$1'); + let output = `${os.tmpdir()}/${message.id}_dectalk.wav`; + let decMessage = '[:phoneme on] ' + args.decMessage; + let loadingmsg = await message.channel.send('Processing ( this can take some time ) '); - if (process.platform == 'win32') { - exec(`cd .\\dectalk && .\\say.exe -w ${message.id}_dectalk.wav "${decMessage}"`) - .catch(err => { - console.error(err); - return message.channel.send('Oh no! an error has occurred!'); - }) - .then(async () => { - const voiceChannel = message.member.voice.channel; - if (!voiceChannel) return message.channel.send('Please enter a voice channel first.'); - try { - const connection = await voiceChannel.join(); - const dispatcher = connection.play(`./dectalk/${message.id}_dectalk.wav`); - dispatcher.once('finish', () => voiceChannel.leave()); - dispatcher.once('error', () => voiceChannel.leave()); - return null; - } catch (err) { - voiceChannel.leave(); - return message.reply(`Oh no, an error occurred: \`${err.message}\`.`); - } - }); - - } else if (process.platform == 'linux' || process.platform == 'darwin') { - let loadingmsg = await message.channel.send('Processing ( this can take some time ) '); - - exec(`cd dectalk && DISPLAY=:0.0 wine say.exe -w ${message.id}_dectalk.wav "${decMessage}"`) - .catch(err => { + if (process.platform === 'win32') { + execFile('say.exe', ['-w', output, `${decMessage}`], {cwd: './dectalk/'}, async (error, stdout, stderr) => { + if (error) { loadingmsg.delete(); - console.error(err); + console.error(stdout); + console.error(stderr); + console.error(error); return message.channel.send('Oh no! an error has occurred!'); - }) - .then(async () => { - const voiceChannel = message.member.voice.channel; - if (!voiceChannel) return message.channel.send('Please enter a voice channel first.'); - try { - loadingmsg.delete(); - const connection = await voiceChannel.join(); - const dispatcher = connection.play(`./dectalk/${message.id}_dectalk.wav`); - dispatcher.once('finish', () => voiceChannel.leave()); - dispatcher.once('error', () => voiceChannel.leave()); - return null; - } catch (err) { - voiceChannel.leave(); - loadingmsg.delete(); - return message.reply(`Oh no, an error occurred: \`${err.message}\`.`); - } - }); + } + + loadingmsg.delete(); + playinVC(output); + }); + + } else if (process.platform === 'linux' || process.platform === 'darwin') { + execFile('wine', ['say.exe', '-w', output, `${decMessage}`], {cwd: './dectalk/'}, async (error, stdout, stderr) => { + if (error) { + loadingmsg.delete(); + console.error(stdout); + console.error(stderr); + console.error(error); + return message.channel.send('Oh no! an error has occurred!'); + } + + loadingmsg.delete(); + playinVC(output); + }); } - + async function playinVC(file) { + const voiceChannel = message.member.voice.channel; + if (!voiceChannel) return message.channel.send('Please enter a voice channel first.'); + try { + const connection = await voiceChannel.join(); + const dispatcher = connection.play(file); + dispatcher.once('finish', () => voiceChannel.leave()); + dispatcher.once('error', () => voiceChannel.leave()); + return null; + } catch (err) { + voiceChannel.leave(); + return message.reply(`Oh no, an error occurred: \`${err.message}\`.`); + } + } } }