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

81 lines
2 KiB
JavaScript
Raw Normal View History

2019-05-18 03:02:04 +02:00
const { Command } = require('discord-akairo');
const axios = require('axios');
const fs = require('fs');
class samCommand extends Command {
constructor() {
super('sam', {
aliases: ['sam'],
category: 'fun',
args: [
{
id: 'samMessage',
type: 'string',
match: 'rest'
}
],
description: {
2019-05-18 03:20:26 +02:00
content: 'Send a mp3 of what you wrote in 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-18 03:20:26 +02:00
let pitch = '';
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;
}
let speed = '';
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',
},
}).then((result) => {
const outputFilename = './sam.mp3';
fs.writeFileSync(outputFilename, result.data);
return message.channel.send({files: ['./sam.mp3']});
});
}
}
module.exports = samCommand;