Haha-Yes/commands/fun/ytp.js

268 lines
7.6 KiB
JavaScript
Raw Normal View History

2019-10-27 22:32:30 +01:00
const { Command } = require('discord-akairo');
const YTPGenerator = require('ytpplus-node');
const os = require('os');
const fs = require('fs');
2020-07-16 09:22:17 +02:00
const attachment = require('../../utils/attachment');
const downloader = require('../../utils/download');
const md5File = require('md5-file');
const ytpHash = require('../../models').ytpHash;
const MAX_CLIPS = 20;
2019-10-27 22:32:30 +01:00
class ytpCommand extends Command {
constructor() {
super('ytp', {
aliases: ['ytp', 'ytpplus', 'ytp+'],
category: 'fun',
clientPermissions: ['ATTACH_FILES', 'SEND_MESSAGES'],
2019-10-27 22:32:30 +01:00
args: [
{
id: 'add',
match: 'flag',
flag: ['--add']
},
{
id: 'pool',
match: 'flag',
flag: ['--pool']
},
{
id: 'force',
match: 'flag',
flag: ['--force']
},
2019-12-10 22:33:52 +01:00
{
id: 'randomSound',
match: 'flag',
flag: ['--randomSound']
},
{
id: 'randomSoundMute',
match: 'flag',
flag: ['--randomSoundMute']
},
{
id: 'reverse',
match: 'flag',
flag: ['--reverse']
},
{
id: 'chorus',
match: 'flag',
flag: ['--chorus']
},
{
id: 'vibrato',
match: 'flag',
flag: ['--vibrato']
},
{
id: 'highPitch',
match: 'flag',
flag: ['--highPitch']
},
{
id: 'lowPitch',
match: 'flag',
flag: ['--lowPitch']
},
{
id: 'speedUp',
match: 'flag',
flag: ['--speedUp']
},
{
id: 'slowDown',
match: 'flag',
flag: ['--slowDown']
},
{
id: 'dance',
match: 'flag',
flag: ['--dance']
},
{
id: 'squidward',
match: 'flag',
flag: ['--squidward']
},
{
id: 'how',
match: 'flag',
flag: ['--how']
},
2019-12-10 22:38:51 +01:00
{
id: 'debug',
match: 'flag',
flag: ['--debug']
},
2019-10-27 22:32:30 +01:00
{
id: 'link',
2020-07-16 09:22:17 +02:00
type: 'url',
prompt: {
start: 'Please send the URL of which video you want to download. Say `cancel` to stop the command',
retry: 'Please send a valid URL of the video you want to download. Say `cancel` to stop the command',
optional: true,
},
unordered: true
},
{
id: 'max',
2020-07-16 09:22:17 +02:00
type: 'string',
unordered: true
2019-10-27 22:32:30 +01:00
}
],
description: {
content: 'Generate random ytp | --add with a link or attachment to add a video to the pool, only .mp4 work | --pool to see how many vid there is currently in the pool | --force to make the command work outside of nsfw channel BE AWARE THAT IT WON\'T CHANGE THE FINAL RESULT SO NSFW CAN STILL HAPPEN',
usage: '(OPTIONAL) | [Minimum length of clip] [Max length of clip]',
examples: ['5 10']
2019-10-27 22:32:30 +01:00
}
});
}
async exec(message, args) {
if (args.pool) {
let mp4 = [];
fs.readdirSync('./asset/ytp/userVid/').forEach(file => {
if (file.endsWith('mp4')) {
mp4.push(file);
}
});
return message.channel.send(`There is currently ${mp4.length} videos, you can add yours by doing \`\`${this.client.commandHandler.prefix[0]}ytp --add (link or attachment)\`\``);
}
2019-10-27 22:32:30 +01:00
if (args.add) {
let loadingmsg = await message.channel.send('Downloading <a:loadingmin:527579785212329984>');
2020-07-16 09:22:17 +02:00
let url;
if (args.link)
url = args.link.href;
else
url = await attachment(message);
2019-10-27 22:32:30 +01:00
if (url) {
return downloader(url, ['--format=mp4'], `./asset/ytp/userVid/${message.id}.mp4`)
2020-07-11 01:33:12 +02:00
.on('error', (err) => {
2019-10-27 22:32:30 +01:00
loadingmsg.delete();
return message.channel.send(err, { code: true });
})
2020-07-11 01:33:12 +02:00
.on('end', async output => {
const hash = md5File.sync(output);
const ytphash = await ytpHash.findOne({where: {hash: hash}});
if (ytphash) {
fs.unlinkSync(output);
loadingmsg.delete();
return message.reply('This video is a duplicate... Not adding.');
} else {
const body = {hash: hash, link: args.link, messageID: message.id};
await ytpHash.create(body);
}
let mp4 = [];
fs.readdirSync('./asset/ytp/userVid/').forEach(file => {
if (file.endsWith('mp4')) {
mp4.push(file);
}
});
2019-10-27 22:51:38 +01:00
loadingmsg.delete();
return message.reply(`Video successfully added to the pool! There is now ${mp4.length} videos`);
});
2019-10-27 22:32:30 +01:00
} else {
2019-10-27 22:51:38 +01:00
loadingmsg.delete();
2019-10-27 22:32:30 +01:00
return message.channel.send('You need a valid video link!');
}
}
2019-10-27 22:32:30 +01:00
2019-12-22 15:52:11 +01:00
if (!message.channel.nsfw && !args.force) return message.channel.send(`Please execute this command in an NSFW channel ( Content might not be NSFW but since the video are user submitted better safe than sorry ) OR do \`\`${this.client.commandHandler.prefix[0]}ytp --force\`\` to make the command work outside of nsfw channel BE AWARE THAT IT WON'T CHANGE THE FINAL RESULT SO NSFW CAN STILL HAPPEN`);
2019-10-27 22:32:30 +01:00
// Read userVid folder and select random vid and only take .mp4
let mp4 = [];
2019-10-27 22:32:30 +01:00
let asset = [];
// Count number of total vid
fs.readdirSync('./asset/ytp/userVid/').forEach(file => {
if (file.endsWith('mp4')) {
mp4.push(file);
}
});
// Select random vid depending on the amount of MAX_CLIPS
for (let i = 0; i < MAX_CLIPS; i++) {
let random = Math.floor(Math.random() * mp4.length);
let vid = `./asset/ytp/userVid/${mp4[random]}`;
if (mp4[random].endsWith('mp4')) {
if (!asset.includes(vid)) {
asset.push(vid);
2019-10-27 22:32:30 +01:00
}
}
}
2020-06-15 17:56:51 +02:00
let loadingmsg = await message.channel.send(`Processing, this can take a ***long*** time, i'll ping you when i finished <a:loadingmin:527579785212329984>\nSome info: There are currently ${mp4.length} videos, you can add yours by doing \`\`${this.client.commandHandler.prefix[0]}ytp --add (link or attachment). Thanks for contributing!\`\``);
let options = {
2019-12-10 22:38:51 +01:00
debug: args.debug,
MIN_STREAM_DURATION: args.link ? Math.floor(args.link) : null,
MAX_STREAM_DURATION: args.link && args.max ? args.max : Math.floor((Math.random() * 3) + 1), // Random duration of video clip
sources: './asset/ytp/sources/',
sounds: './asset/ytp/sounds/',
music: './asset/ytp/music/',
resources: './asset/ytp/resources/',
temp: os.tmpdir(),
sourceList: asset,
2020-04-29 19:38:26 +02:00
intro: args.force ? './asset/ytp/intro.mp4' : null,
outro: './asset/ytp/outro.mp4',
OUTPUT_FILE: `${os.tmpdir()}/${message.id}_YTP.mp4`,
MAX_CLIPS: MAX_CLIPS,
transitions: true,
2020-04-18 18:23:53 +02:00
showFileNames: true,
effects: {
2019-12-10 22:33:52 +01:00
effect_RandomSound: !args.randomSound,
effect_RandomSoundMute: !args.randomSoundMute,
effect_Reverse: !args.reverse,
effect_Chorus: !args.chorus,
effect_Vibrato: !args.vibrato,
effect_HighPitch: !args.highPitch,
effect_LowPitch: !args.lowPitch,
effect_SpeedUp: !args.speedUp,
effect_SlowDown: !args.slowDown,
effect_Dance: !args.dance,
effect_Squidward: !args.squidward,
effect_How: !args.how
}
};
new YTPGenerator().configurateAndGo(options)
.then(() => {
md5File(`${os.tmpdir()}/${message.id}_YTP.mp4`).then(async hash => {
const body = {hash: hash, link: 'null', messageID: message.id};
await ytpHash.create(body);
});
loadingmsg.delete();
2020-06-15 17:56:51 +02:00
return message.reply('Here is your YTP! Remember, it might contain nsfw, so be careful!', {files: [`${os.tmpdir()}/${message.id}_YTP.mp4`]})
2020-04-18 18:23:53 +02:00
.catch(err => {
console.error(err);
return message.channel.send('Whoops, look like the vid might be too big for discord, my bad, please try again');
});
})
.catch(err => {
console.error(err);
loadingmsg.delete();
return message.reply({files: [Math.random() < 0.5 ? './asset/ytp/error1.mp4' : './asset/ytp/error2.mp4']})
2020-06-15 17:56:51 +02:00
.catch(err => { // In case it can't send the video for some reason
console.error(err);
return message.channel.send('Oh no, an error has occurred! please try again. If this happens alot, you should report this to the developers.');
});
});
2019-10-27 22:32:30 +01:00
}
}
2020-04-28 22:57:38 +02:00
module.exports = ytpCommand;