use local file too

This commit is contained in:
loicbersier 2019-07-17 23:31:08 +02:00
parent 540b15c4ad
commit 4be84cb798

View file

@ -1,6 +1,6 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const axios = require('axios'); const util = require('util');
const fs = require('fs'); const exec = util.promisify(require('child_process').exec);
const rand = require('../../../rand.js'); const rand = require('../../../rand.js');
class dectalkvcCommand extends Command { class dectalkvcCommand extends Command {
@ -31,31 +31,47 @@ class dectalkvcCommand extends Command {
args.decMessage = args.decMessage.replace('\n', ' '); args.decMessage = args.decMessage.replace('\n', ' ');
args.decMessage = encodeURI(args.decMessage); args.decMessage = encodeURI(args.decMessage);
return axios.request({ if (process.platform == 'win32') {
responseType: 'arraybuffer', exec(`cd .\\dectalk && .\\say.exe -w dectalk.wav "${args.decMessage}"`)
url: `http://127.0.0.1:8080/api/gen.wav?dectalk=${args.decMessage}`, .catch(err => {
method: 'get', return console.error(err);
headers: { })
'Content-Type': 'audio/wav', .then(async () => {
}, const voiceChannel = message.member.voice.channel;
}).then(async (result) => { if (!voiceChannel) return message.say('Please enter a voice channel first.');
const outputFilename = './dectalkvc.wav'; try {
fs.writeFile(outputFilename, result.data, async function(err) { const connection = await voiceChannel.join();
if (err) console.error(err); const dispatcher = connection.play('./dectalkvc.wav');
const voiceChannel = message.member.voice.channel; dispatcher.once('finish', () => voiceChannel.leave());
if (!voiceChannel) return message.say('Please enter a voice channel first.'); dispatcher.once('error', () => voiceChannel.leave());
try { return null;
const connection = await voiceChannel.join(); } catch (err) {
const dispatcher = connection.play('./dectalkvc.wav'); voiceChannel.leave();
dispatcher.once('finish', () => voiceChannel.leave()); return message.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
dispatcher.once('error', () => voiceChannel.leave()); }
return null; });
} catch (err) {
voiceChannel.leave(); } else if (process.platform == 'linux') {
return message.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); exec(`wine dectalk/say.exe -w ../../dectalk.wav "${args.decMessage}"`)
} .catch(err => {
}); return console.error(err);
}); })
.then(async () => {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.say('Please enter a voice channel first.');
try {
const connection = await voiceChannel.join();
const dispatcher = connection.play('./dectalkvc.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}\`. Try again later!`);
}
});
}
} }
} }