Haha-Yes/commands/fun/curses.js

95 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-07-26 11:52:23 +02:00
const { Command } = require('discord-akairo');
const path = require('path');
const os = require('os');
const fs = require('fs');
const { Buffer } = require('buffer');
const attachment = require('../../utils/attachment');
const downloader = require('../../utils/download');
class cursesCommand extends Command {
constructor() {
super('curses', {
aliases: ['curses', 'curse'],
2020-07-26 11:52:23 +02:00
category: 'fun',
clientPermissions: ['SEND_MESSAGES', 'ATTACH_FILES'],
args: [
{
id: 'link',
type: 'url',
},
{
id: 'webm',
match: 'flag',
flag: ['--webm']
},
2020-07-26 11:52:23 +02:00
],
description: {
content: 'Mess with video length. webm = expanding length and mp4 = very long length',
usage: '[link or attachment]',
examples: ['https://www.youtube.com/watch?v=QLCPgZ51poU']
}
});
}
async exec(message, args) {
let replaceAt = function(string, index, replacement) {
return string.substr(0, index) + replacement + string.substr(index + replacement.length);
};
let link;
if (args.link)
link = args.link.href;
else
link = await attachment(message);
2020-08-08 23:17:37 +02:00
if (!link) return message.channel.send('Please specify the URL of the video you want to curse.');
2020-07-26 11:52:23 +02:00
let ext = path.extname(link.toLowerCase());
console.log(ext);
if (ext !== '.webm' && ext !== '.mp4')
2020-07-26 11:52:23 +02:00
ext = '.mp4';
if (args.webm) ext = '.webm';
2020-07-26 11:52:23 +02:00
let loadingmsg = await message.channel.send('Processing <a:loadingmin:527579785212329984>');
downloader(link, [`--format=${ext.replace('.', '')}`], `${os.tmpdir()}/${message.id}${ext}`)
2020-07-26 11:52:23 +02:00
.on('error', async err => {
loadingmsg.delete();
console.error(err);
return message.channel.send(err, { code: true });
})
.on('end', output => {
let file = fs.readFileSync(output).toString('hex');
let searchHex = '6d766864';
let replaceHex = '0000180FFFFFF7F';
2020-07-26 11:52:23 +02:00
let skipByte = 34;
let endResult;
if (ext === '.webm') {
searchHex = '2ad7b1';
replaceHex = '42FFB060';
2020-07-26 11:52:23 +02:00
skipByte = 8;
endResult = replaceAt(file, file.indexOf(searchHex) + file.substring(file.indexOf(searchHex)).indexOf('4489') + skipByte, replaceHex);
} else {
endResult = replaceAt(file, file.indexOf(searchHex) + skipByte, replaceHex);
}
fs.writeFileSync(`${os.tmpdir()}/cursed${message.id}${ext}`, Buffer.from(endResult, 'hex'));
message.delete();
return message.channel.send(`Cursed by ${message.author}`, {files: [`${os.tmpdir()}/cursed${message.id}${ext}`]})
2020-07-26 11:52:23 +02:00
.catch(err => {
console.error(err);
return message.channel.send('Video is too big! try again with something smaller');
})
.then(() => {
loadingmsg.delete();
});
});
}
}
module.exports = cursesCommand;