From c8fc925d3572888cc0d5a471f27324cddf0be848 Mon Sep 17 00:00:00 2001 From: supositware Date: Fri, 17 Jun 2022 01:25:35 +0200 Subject: [PATCH] Converted to slash --- commands/reddit.js | 41 +++++++++++++++++++++++ commands/vid2gif.js | 79 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 commands/reddit.js create mode 100644 commands/vid2gif.js diff --git a/commands/reddit.js b/commands/reddit.js new file mode 100644 index 0000000..6c91563 --- /dev/null +++ b/commands/reddit.js @@ -0,0 +1,41 @@ +import { SlashCommandBuilder } from '@discordjs/builders'; +import { MessageEmbed } from 'discord.js'; +import fetch from 'node-fetch' + +export default { + data: new SlashCommandBuilder() + .setName('reddit') + .setDescription('Send random images from the subreddit you choose') + .addStringOption(option => + option.setName('subreddit') + .setDescription('The subreddit you wish to see') + .setRequired(true)), + async execute(interaction) { + await interaction.deferReply({ ephemeral: false }); + + fetch('https://www.reddit.com/r/' + interaction.options.getString('subreddit') + '.json?limit=100').then((response) => { + return response.json(); + }).then((response) => { + if (response.error == 404) { + return interaction.editReply('Not a valid subreddit'); + } + if (response.data.dist == 0) { + return interaction.editReply('Not a valid subreddit'); + + } + const i = Math.floor((Math.random() * response.data.children.length)); + if (response.data.children[i].data.over_18 == true && !interaction.channel.nsfw) { + return interaction.editReply('No nsfw'); + } + const redditEmbed = new MessageEmbed() + .setColor(interaction.member ? interaction.member.displayHexColor : 'NAVY') + .setTitle(response.data.children[i].data.title) + .setDescription(response.data.children[i].data.selftext) + .setURL('https://reddit.com' + response.data.children[i].data.permalink) + .setFooter(`/r/${response.data.children[i].data.subreddit} | ⬆ ${response.data.children[i].data.ups} 🗨 ${response.data.children[i].data.num_comments}`); + + interaction.editReply({ embeds: [redditEmbed]}); + interaction.followUp(response.data.children[i].data.url); + }); + }, +}; diff --git a/commands/vid2gif.js b/commands/vid2gif.js new file mode 100644 index 0000000..6595807 --- /dev/null +++ b/commands/vid2gif.js @@ -0,0 +1,79 @@ +import { SlashCommandBuilder } from '@discordjs/builders'; +import utils from '../utils/videos.js'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { exec } from 'node:child_process'; + + +export default { + data: new SlashCommandBuilder() + .setName('vid2gif') + .setDescription('Convert your video into a gif.') + .addStringOption(option => + option.setName('url') + .setDescription('URL of the video you want to convert') + .setRequired(true)), + async execute(interaction) { + await interaction.deferReply({ ephemeral: false }); + const url = interaction.options.getString('url'); + + utils.downloadVideo(url, interaction.id) + .then(async () => { + const file = fs.readdirSync(os.tmpdir()).filter(fn => fn.startsWith(interaction.id)); + const output = `${os.tmpdir()}/${file}`; + const gifskiOutput = output.replace(path.extname(output), '.gif'); + const gifsicleOutput = output.replace(path.extname(output), 'gifsicle.gif'); + + await utils.ffmpeg(`-i ${output} ${os.tmpdir()}/frame${interaction.id}%04d.png`); // Extract every frame for gifski + await gifski(gifskiOutput, `${os.tmpdir()}/frame${interaction.id}*`); // Make it look better + await gifsicle(gifskiOutput, gifsicleOutput); // Optimize it + + const fileStat = fs.statSync(gifsicleOutput); + const fileSize = fileStat.size / 1000000.0; + + if (fileSize > 100) { + await interaction.deleteReply(); + await interaction.followUp('Uh oh! The video once converted is too big!', { ephemeral: true }); + } + else if (fileSize > 8) { + const fileURL = await utils.upload(gifsicleOutput) + .catch(err => { + console.error(err); + }); + await interaction.editReply({ content: `File was bigger than 8 mb. It has been uploaded to an external site.\n${fileURL}`, ephemeral: false }); + } + else { + await interaction.editReply({ files: [gifsicleOutput], ephemeral: false }); + } + }); + }, +}; + +async function gifski(output, input) { + return await new Promise((resolve, reject) => { + exec(`gifski -o ${output} ${input}`, (err, stdout, stderr) => { + if (err) { + reject(stderr); + } + if (stderr) { + console.error(stderr); + } + resolve(stdout); + }); + }); +} + +async function gifsicle(input, output) { + return await new Promise((resolve, reject) => { + exec(`gifsicle --scale 0.5 -O3 -i ${input} -o ${output}`, (err, stdout, stderr) => { + if (err) { + reject(stderr); + } + if (stderr) { + console.error(stderr); + } + resolve(stdout); + }); + }); +} \ No newline at end of file