use tmpdir

This commit is contained in:
loicbersier 2020-02-01 02:36:48 +01:00
parent c3f4c9d786
commit f893b53ba0
6 changed files with 27 additions and 18 deletions

View file

@ -1,6 +1,7 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const util = require('util'); const util = require('util');
const exec = util.promisify(require('child_process').exec); 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 {
@ -28,23 +29,24 @@ class dectalkCommand extends Command {
} }
async exec(message, args) { async exec(message, args) {
let output = `${os.tmpdir()}/${message.id}_dectalk.wav`;
args.decMessage = rand.random(args.decMessage, message); args.decMessage = rand.random(args.decMessage, message);
args.decMessage = args.decMessage.replace('\n', ' '); args.decMessage = args.decMessage.replace('\n', ' ');
let decMessage = '[:phoneme on] ' + args.decMessage.replace(/(["'$`\\])/g,'\\$1'); let decMessage = '[:phoneme on] ' + args.decMessage.replace(/(["'$`\\])/g,'\\$1');
if (process.platform == 'win32') { if (process.platform == 'win32') {
exec(`cd .\\dectalk && .\\say.exe -w dectalk.wav "${decMessage}"`) exec(`cd .\\dectalk && .\\say.exe -w ${output} "${decMessage}"`)
.catch(err => { .catch(err => {
console.error(err); console.error(err);
return message.channel.send('Oh no! an error has occured!'); return message.channel.send('Oh no! an error has occured!');
}) })
.then(() => { .then(() => {
return message.channel.send({files: ['./dectalk/dectalk.wav']}); return message.channel.send({files: [output]});
}); });
} else if (process.platform == 'linux' || process.platform == 'darwin') { } 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 dectalk.wav "${decMessage}"`) exec(`cd dectalk && DISPLAY=:0.0 wine say.exe -w ${output} "${decMessage}"`)
.catch(err => { .catch(err => {
loadingmsg.delete(); loadingmsg.delete();
console.error(err); console.error(err);
@ -52,7 +54,7 @@ class dectalkCommand extends Command {
}) })
.then(() => { .then(() => {
loadingmsg.delete(); loadingmsg.delete();
return message.channel.send({files: ['./dectalk/dectalk.wav']}); return message.channel.send({files: [output]});
}); });
} }
} }

View file

@ -1,6 +1,7 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const util = require('util'); const util = require('util');
const exec = util.promisify(require('child_process').exec); 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 {
@ -28,12 +29,13 @@ class dectalkvcCommand extends Command {
} }
async exec(message, args) { async exec(message, args) {
let output = `${os.tmpdir()}/${message.id}_dectalk.wav`;
args.decMessage = rand.random(args.decMessage, message); args.decMessage = rand.random(args.decMessage, message);
args.decMessage = args.decMessage.replace('\n', ' '); args.decMessage = args.decMessage.replace('\n', ' ');
let decMessage = '[:phoneme on] ' + args.decMessage.replace(/(["'$`\\])/g,'\\$1'); let decMessage = '[:phoneme on] ' + args.decMessage.replace(/(["'$`\\])/g,'\\$1');
if (process.platform == 'win32') { if (process.platform == 'win32') {
exec(`cd .\\dectalk && .\\say.exe -w dectalkvc.wav "${decMessage}"`) exec(`cd .\\dectalk && .\\say.exe -w ${output} "${decMessage}"`)
.catch(err => { .catch(err => {
console.error(err); console.error(err);
return message.channel.send('Oh no! an error has occured!'); return message.channel.send('Oh no! an error has occured!');
@ -43,7 +45,7 @@ class dectalkvcCommand extends Command {
if (!voiceChannel) return message.say('Please enter a voice channel first.'); if (!voiceChannel) return message.say('Please enter a voice channel first.');
try { try {
const connection = await voiceChannel.join(); const connection = await voiceChannel.join();
const dispatcher = connection.play('./dectalk/dectalk.wav'); const dispatcher = connection.play(output);
dispatcher.once('finish', () => voiceChannel.leave()); dispatcher.once('finish', () => voiceChannel.leave());
dispatcher.once('error', () => voiceChannel.leave()); dispatcher.once('error', () => voiceChannel.leave());
return null; return null;
@ -56,7 +58,7 @@ class dectalkvcCommand extends Command {
} else if (process.platform == 'linux' || process.platform == 'darwin') { } 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 dectalkvc.wav "${decMessage}"`) exec(`cd dectalk && DISPLAY=:0.0 wine say.exe -w ${output} "${decMessage}"`)
.catch(err => { .catch(err => {
loadingmsg.delete(); loadingmsg.delete();
console.error(err); console.error(err);
@ -68,7 +70,7 @@ class dectalkvcCommand extends Command {
try { try {
loadingmsg.delete(); loadingmsg.delete();
const connection = await voiceChannel.join(); const connection = await voiceChannel.join();
const dispatcher = connection.play('./dectalk/dectalkvc.wav'); const dispatcher = connection.play(output);
dispatcher.once('finish', () => voiceChannel.leave()); dispatcher.once('finish', () => voiceChannel.leave());
dispatcher.once('error', () => voiceChannel.leave()); dispatcher.once('error', () => voiceChannel.leave());
return null; return null;

View file

@ -1,6 +1,7 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const axios = require('axios'); const axios = require('axios');
const fs = require('fs'); const fs = require('fs');
const os = require('os');
const rand = require('../../../rand.js'); const rand = require('../../../rand.js');
class samCommand extends Command { class samCommand extends Command {
@ -76,9 +77,9 @@ class samCommand extends Command {
'Content-Type': 'audio/mpeg', 'Content-Type': 'audio/mpeg',
}, },
}).then((result) => { }).then((result) => {
const outputFilename = './sam.wav'; const outputFilename = `${os.tmpdir}/${message.id}_sam.wav`;
fs.writeFileSync(outputFilename, result.data); fs.writeFileSync(outputFilename, result.data);
return message.channel.send({files: ['./sam.wav']}); return message.channel.send({files: [outputFilename]});
}); });
} }

View file

@ -1,6 +1,7 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const axios = require('axios'); const axios = require('axios');
const fs = require('fs'); const fs = require('fs');
const os = require('os');
const rand = require('../../../rand.js'); const rand = require('../../../rand.js');
class samvcCommand extends Command { class samvcCommand extends Command {
@ -76,7 +77,7 @@ class samvcCommand extends Command {
'Content-Type': 'audio/mpeg', 'Content-Type': 'audio/mpeg',
}, },
}).then(async (result) => { }).then(async (result) => {
const outputFilename = './samvc.wav'; const outputFilename = `${os.tmpdir}/${message.id}_sam.wav`;
fs.writeFile(outputFilename, result.data, async function(err) { fs.writeFile(outputFilename, result.data, async function(err) {
if (err) console.error(err); if (err) console.error(err);
@ -84,7 +85,7 @@ class samvcCommand extends Command {
if (!voiceChannel) return message.say('Please enter a voice channel first.'); if (!voiceChannel) return message.say('Please enter a voice channel first.');
try { try {
const connection = await voiceChannel.join(); const connection = await voiceChannel.join();
const dispatcher = connection.play('./samvc.wav'); const dispatcher = connection.play(outputFilename);
dispatcher.once('finish', () => voiceChannel.leave()); dispatcher.once('finish', () => voiceChannel.leave());
dispatcher.once('error', () => voiceChannel.leave()); dispatcher.once('error', () => voiceChannel.leave());
return null; return null;

View file

@ -3,6 +3,7 @@ const textToSpeech = require('@google-cloud/text-to-speech');
const rand = require('../../../rand.js'); const rand = require('../../../rand.js');
const gclient = new textToSpeech.TextToSpeechClient(); const gclient = new textToSpeech.TextToSpeechClient();
const fs = require('fs'); const fs = require('fs');
const os = require('os');
class TtsCommand extends Command { class TtsCommand extends Command {
constructor() { constructor() {
@ -30,6 +31,7 @@ class TtsCommand extends Command {
async exec(message, args) { async exec(message, args) {
let text = args.text; let text = args.text;
let output = `${os.tmpdir()}/${message.id}_tts.mp3`;
text = rand.random(text, message); text = rand.random(text, message);
@ -51,7 +53,7 @@ class TtsCommand extends Command {
} }
// Write the binary audio content to a local file // Write the binary audio content to a local file
fs.writeFile('tts.mp3', response.audioContent, 'binary', err => { fs.writeFile(output, response.audioContent, 'binary', err => {
if (err) { if (err) {
console.error('ERROR:', err); console.error('ERROR:', err);
message.channel.send('An error has occured, the message is probably too long'); message.channel.send('An error has occured, the message is probably too long');
@ -59,7 +61,7 @@ class TtsCommand extends Command {
return; return;
} }
console.log('Audio content written to file: tts.mp3'); console.log('Audio content written to file: tts.mp3');
message.channel.send({ files: ['./tts.mp3'] }); message.channel.send({ files: [output] });
}); });
}); });

View file

@ -3,6 +3,7 @@ const textToSpeech = require('@google-cloud/text-to-speech');
const rand = require('../../../rand.js'); const rand = require('../../../rand.js');
const gclient = new textToSpeech.TextToSpeechClient(); const gclient = new textToSpeech.TextToSpeechClient();
const fs = require('fs'); const fs = require('fs');
const os = require('os');
class TtsvcCommand extends Command { class TtsvcCommand extends Command {
constructor() { constructor() {
@ -30,6 +31,7 @@ class TtsvcCommand extends Command {
async exec(message, args) { async exec(message, args) {
let text = args.text; let text = args.text;
let output = `${os.tmpdir()}/${message.id}_tts.mp3`;
text = rand.random(text, message); text = rand.random(text, message);
@ -50,20 +52,19 @@ class TtsvcCommand extends Command {
} }
// Write the binary audio content to a local file // Write the binary audio content to a local file
fs.writeFile('ttsvc.mp3', response.audioContent, 'binary', async err => { fs.writeFile(output, response.audioContent, 'binary', async err => {
if (err) { if (err) {
console.error('ERROR:', err); console.error('ERROR:', err);
message.channel.send('An error has occured, the message is probably too long'); message.channel.send('An error has occured, the message is probably too long');
return; return;
} }
console.log('Audio content written to file: ttsvc.mp3');
const voiceChannel = message.member.voice.channel; const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.say('Please enter a voice channel first.'); if (!voiceChannel) return message.say('Please enter a voice channel first.');
try { try {
const connection = await voiceChannel.join(); const connection = await voiceChannel.join();
const dispatcher = connection.play('./ttsvc.mp3'); const dispatcher = connection.play(output);
dispatcher.once('finish', () => voiceChannel.leave()); dispatcher.once('finish', () => voiceChannel.leave());
dispatcher.once('error', () => voiceChannel.leave()); dispatcher.once('error', () => voiceChannel.leave());
return null; return null;