Haha-Yes/commands/fun/image2audio.js

87 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2020-02-27 06:52:33 +01:00
const { Command } = require('discord-akairo');
2020-07-16 09:22:17 +02:00
const attachment = require('../../utils/attachment');
2020-02-27 06:52:33 +01:00
const ffmpeg = require('fluent-ffmpeg');
const fetch = require('node-fetch');
const fs = require('fs');
const os = require('os');
class image2audioCommand extends Command {
constructor() {
super('image2audio', {
aliases: ['image2audio', 'i2a'],
category: 'fun',
clientPermissions: ['SEND_MESSAGES', 'ATTACH_FILES'],
args: [
{
id: 'link',
2020-07-16 09:22:17 +02:00
type: 'url',
2020-02-28 16:53:49 +01:00
},
{
id: 'wav',
type: 'flag',
flag: '--wav'
2020-02-27 06:52:33 +01:00
}
],
description: {
2020-02-28 16:53:49 +01:00
content: 'Transform an image binary data into audio ( MIGHT BECOME EAR RAPE ) --wav to get wav output',
2020-02-27 06:52:33 +01:00
usage: '[link to image]',
2020-02-27 22:06:50 +01:00
examples: ['https://cdn.discordapp.com/attachments/532987690145021982/682694359313022987/a2i682694012309864452.png']
2020-02-27 06:52:33 +01:00
}
});
}
async exec(message, args) {
2020-07-16 09:22:17 +02:00
let url;
if (args.link)
url = args.link.href;
else
url = await attachment(message);
2020-02-27 06:52:33 +01:00
let loadingmsg = await message.channel.send('Processing <a:loadingmin:527579785212329984>');
if (!url) return message.channel.send('Please attach an image or use an url');
fetch(url)
.then(res => {
const dest = fs.createWriteStream(`${os.tmpdir()}/${message.id}.png`);
res.body.pipe(dest);
dest.on('finish', () => {
ffmpeg(`${os.tmpdir()}/${message.id}.png`)
.format('rawvideo')
.output(`${os.tmpdir()}/${message.id}1.png`)
2020-02-27 17:57:16 +01:00
.on('error', (err, stdout, stderr) => {
console.error(`${err}\n${stdout}\n${stderr}`);
return message.channel.send('Uh oh, an error has occurred!');
2020-02-27 17:57:16 +01:00
})
2020-02-27 06:52:33 +01:00
.on('end', () => {
ffmpeg()
.audioBitrate(44100)
.audioChannels(1)
.input(`${os.tmpdir()}/${message.id}1.png`)
.inputFormat('s16le')
2020-02-28 16:53:49 +01:00
.output(`${os.tmpdir()}/i2a_${message.id}.${args.wav ? 'wav' : 'mp3'}`)
2020-02-27 17:57:16 +01:00
.on('error', (err, stdout, stderr) => {
console.error(`${err}\n${stdout}\n${stderr}`);
return message.channel.send('Uh oh, an error has occurred!');
2020-02-27 17:57:16 +01:00
})
2020-02-27 06:52:33 +01:00
.on('end', () => {
console.log('finished');
loadingmsg.delete();
2020-02-28 16:53:49 +01:00
let file = fs.statSync(`${os.tmpdir()}/i2a_${message.id}.${args.wav ? 'wav' : 'mp3'}`);
2020-02-27 19:47:56 +01:00
let fileSize = (file.size / 1000000.0).toFixed(2);
2020-02-28 16:53:49 +01:00
return message.channel.send(`Audio file is ${fileSize} MB`, {files: [`${os.tmpdir()}/i2a_${message.id}.${args.wav ? 'wav' : 'mp3'}`]})
2020-02-27 16:20:30 +01:00
.catch(() => {
2020-02-27 19:47:56 +01:00
return message.channel.send(`End result is too big to fit on discord! File is ${fileSize} MB`);
2020-02-27 16:20:30 +01:00
});
2020-02-27 06:52:33 +01:00
})
.run();
})
.run();
});
});
}
}
module.exports = image2audioCommand;