waaay better

Signed-off-by: loicbersier <loic.bersier1@gmail.com>
This commit is contained in:
loicbersier 2020-09-04 13:36:08 +02:00
parent 1b3fa24062
commit 265757e2f5
2 changed files with 71 additions and 71 deletions

View file

@ -1,6 +1,6 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const util = require('util'); const { execFile } = require('child_process');
const exec = util.promisify(require('child_process').exec); const os = require('os');
const rand = require('../../../rand.js'); const rand = require('../../../rand.js');
class dectalkCommand extends Command { class dectalkCommand extends Command {
@ -29,30 +29,35 @@ class dectalkCommand extends Command {
async exec(message, args) { async exec(message, args) {
args.decMessage = rand.random(args.decMessage, message); args.decMessage = rand.random(args.decMessage, message);
args.decMessage = args.decMessage.replace('\n', ' '); let output = `${os.tmpdir()}/${message.id}_dectalk.wav`;
let decMessage = '[:phoneme on] ' + args.decMessage.replace(/(["'$`\\])/g,'\\$1'); let decMessage = '[:phoneme on]' + args.decMessage;
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(() => {
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 ) <a:loadingmin:527579785212329984>'); let loadingmsg = await message.channel.send('Processing ( this can take some time ) <a:loadingmin:527579785212329984>');
exec(`cd dectalk && DISPLAY=:0.0 wine say.exe -w ${message.id}_dectalk.wav "${decMessage}"`) if (process.platform === 'win32') {
.catch(err => { execFile('say.exe', ['-w', output, `${decMessage}`], {cwd: './dectalk/'}, (error, stdout, stderr) => {
if (error) {
loadingmsg.delete(); loadingmsg.delete();
console.error(err); console.error(stdout);
console.error(stderr);
console.error(error);
return message.channel.send('Oh no! an error has occurred!'); return message.channel.send('Oh no! an error has occurred!');
}) }
.then(() => {
loadingmsg.delete(); loadingmsg.delete();
return message.channel.send({files: [`./dectalk/${message.id}_dectalk.wav`]}); 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(stdout);
console.error(stderr);
console.error(error);
return message.channel.send('Oh no! an error has occurred!');
}
loadingmsg.delete();
return message.channel.send({files: [output]});
}); });
} }
} }

View file

@ -1,6 +1,6 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const util = require('util'); const { execFile } = require('child_process');
const exec = util.promisify(require('child_process').exec); const os = require('os');
const rand = require('../../../rand.js'); const rand = require('../../../rand.js');
class dectalkvcCommand extends Command { class dectalkvcCommand extends Command {
@ -29,58 +29,53 @@ class dectalkvcCommand extends Command {
async exec(message, args) { async exec(message, args) {
args.decMessage = rand.random(args.decMessage, message); args.decMessage = rand.random(args.decMessage, message);
args.decMessage = args.decMessage.replace('\n', ' '); let output = `${os.tmpdir()}/${message.id}_dectalk.wav`;
let decMessage = '[:phoneme on] ' + args.decMessage.replace(/(["'$`\\])/g,'\\$1'); let decMessage = '[:phoneme on] ' + args.decMessage;
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 ) <a:loadingmin:527579785212329984>'); let loadingmsg = await message.channel.send('Processing ( this can take some time ) <a:loadingmin:527579785212329984>');
exec(`cd dectalk && DISPLAY=:0.0 wine say.exe -w ${message.id}_dectalk.wav "${decMessage}"`) if (process.platform === 'win32') {
.catch(err => { execFile('say.exe', ['-w', output, `${decMessage}`], {cwd: './dectalk/'}, async (error, stdout, stderr) => {
if (error) {
loadingmsg.delete(); loadingmsg.delete();
console.error(err); console.error(stdout);
console.error(stderr);
console.error(error);
return message.channel.send('Oh no! an error has occurred!'); return message.channel.send('Oh no! an error has occurred!');
}) }
.then(async () => {
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; const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.channel.send('Please enter a voice channel first.'); if (!voiceChannel) return message.channel.send('Please enter a voice channel first.');
try { try {
loadingmsg.delete();
const connection = await voiceChannel.join(); const connection = await voiceChannel.join();
const dispatcher = connection.play(`./dectalk/${message.id}_dectalk.wav`); const dispatcher = connection.play(file);
dispatcher.once('finish', () => voiceChannel.leave()); dispatcher.once('finish', () => voiceChannel.leave());
dispatcher.once('error', () => voiceChannel.leave()); dispatcher.once('error', () => voiceChannel.leave());
return null; return null;
} catch (err) { } catch (err) {
voiceChannel.leave(); voiceChannel.leave();
loadingmsg.delete();
return message.reply(`Oh no, an error occurred: \`${err.message}\`.`); return message.reply(`Oh no, an error occurred: \`${err.message}\`.`);
} }
});
} }
} }
} }