Haha-Yes/commands/fun/tts/samvc.js

107 lines
3 KiB
JavaScript
Raw Permalink Normal View History

2019-05-18 03:02:04 +02:00
const { Command } = require('discord-akairo');
const axios = require('axios');
const fs = require('fs');
2020-02-01 02:36:48 +01:00
const os = require('os');
2019-05-19 21:36:42 +02:00
const rand = require('../../../rand.js');
2019-05-18 03:02:04 +02:00
class samvcCommand extends Command {
constructor() {
super('samvc', {
aliases: ['samvc'],
category: 'fun',
2019-11-22 20:56:54 +01:00
clientPermissions: ['SPEAK'],
2019-05-18 03:02:04 +02:00
args: [
{
id: 'samMessage',
type: 'string',
2019-06-23 03:41:59 +02:00
prompt: {
start: 'Write something so i can say it back in sam',
},
2019-05-18 03:02:04 +02:00
match: 'rest'
}
],
description: {
2019-05-18 03:20:26 +02:00
content: 'Repeat what you said in voice chat with Microsoft Sam tts, can change speed and pitch with [speed:a number] and [pitch:a]',
2019-05-18 03:02:04 +02:00
usage: '[text]',
examples: ['Here comes the roflcopter soisoisoisoisoi']
}
});
}
async exec(message, args) {
2019-05-19 20:41:58 +02:00
args.samMessage = rand.random(args.samMessage, message);
2020-06-15 16:55:08 +02:00
let pitch;
2019-05-18 03:20:26 +02:00
if (args.samMessage.includes('[pitch:')) {
pitch = args.samMessage.split(/(\[pitch:.*?])/);
for (let i = 0, l = pitch.length; i < l; i++) {
if (pitch[i].includes('[pitch:')) {
pitch = pitch[i].replace('[pitch:', '').slice(0, -1);
args.samMessage = args.samMessage.replace(/(\[pitch:.*?])/g, '');
i = pitch.length;
}
}
2019-05-18 03:27:29 +02:00
if (pitch > 200)
pitch = 200;
else if (pitch < 50)
pitch = 50;
2019-05-18 03:20:26 +02:00
} else {
pitch = 100;
}
2020-06-15 16:55:08 +02:00
let speed;
2019-05-18 03:20:26 +02:00
if (args.samMessage.includes('[speed:')) {
speed = args.samMessage.split(/(\[speed:.*?])/);
for (let i = 0, l = speed.length; i < l; i++) {
if (speed[i].includes('[speed:')) {
speed = speed[i].replace('[speed:', '').slice(0, -1);
args.samMessage = args.samMessage.replace(/(\[speed:.*?])/g, '');
i = speed.length;
}
}
2019-05-18 03:27:29 +02:00
if (speed > 450)
speed = 450;
else if (speed < 30)
speed = 30;
2019-05-18 03:20:26 +02:00
} else {
2019-05-18 03:23:23 +02:00
speed = 150;
2019-05-18 03:20:26 +02:00
}
2019-05-18 03:02:04 +02:00
args.samMessage = args.samMessage.replace('\n', ' ');
args.samMessage = encodeURI(args.samMessage);
return axios.request({
responseType: 'arraybuffer',
2019-05-18 03:20:26 +02:00
url: `https://tetyys.com/SAPI4/SAPI4?text=${args.samMessage}&voice=Sam&pitch=${pitch}&speed=${speed}`,
2019-05-18 03:02:04 +02:00
method: 'get',
headers: {
'Content-Type': 'audio/mpeg',
},
2020-06-15 16:55:08 +02:00
})
.catch((err) => {
console.error(err);
return message.channel.send(`Uh oh, an error has occurred! please try again later.\n${err}`);
2020-06-15 16:55:08 +02:00
})
2019-05-20 00:23:15 +02:00
2020-06-15 16:55:08 +02:00
.then(async (result) => {
const outputFilename = `${os.tmpdir}/${message.id}_sam.wav`;
fs.writeFile(outputFilename, result.data, async function(err) {
if (err) console.error(err);
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(outputFilename);
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}\`.`);
}
});
2019-05-20 00:17:30 +02:00
});
2019-05-18 03:02:04 +02:00
}
}
module.exports = samvcCommand;