From 1d4c7feb5677e5a112a71af347fdd88fb53883da Mon Sep 17 00:00:00 2001
From: supositware <loic.bersier1@gmail.com>
Date: Fri, 17 Jun 2022 01:25:05 +0200
Subject: [PATCH] Convert to ES6

---
 .eslintrc.json                                |  1 +
 commands/download.js                          | 45 ++++---------------
 commands/ping.js                              |  4 +-
 ...d-commands.js => deploy-guild-commands.cjs | 17 +++++++
 events/interactionCreate.js                   |  2 +-
 events/ready.js                               |  9 ++--
 index.js                                      | 19 +++++---
 7 files changed, 48 insertions(+), 49 deletions(-)
 rename deploy-guild-commands.js => deploy-guild-commands.cjs (66%)

diff --git a/.eslintrc.json b/.eslintrc.json
index 4cd9b605..8debef1c 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -7,6 +7,7 @@
 	"parserOptions": {
 		"ecmaVersion": 2021
 	},
+	"sourceType": "module",
 	"rules": {
 		"arrow-spacing": ["warn", { "before": true, "after": true }],
 		"brace-style": ["error", "stroustrup", { "allowSingleLine": true }],
diff --git a/commands/download.js b/commands/download.js
index 9e773094..128e8ff2 100644
--- a/commands/download.js
+++ b/commands/download.js
@@ -1,10 +1,11 @@
-const { SlashCommandBuilder } = require('@discordjs/builders');
-const { MessageEmbed, MessageActionRow, MessageSelectMenu } = require('discord.js');
-const { exec } = require('node:child_process');
-const fs = require('node:fs');
-const os = require('node:os');
+import { SlashCommandBuilder } from '@discordjs/builders';
+import { MessageEmbed, MessageActionRow, MessageSelectMenu } from 'discord.js';
+import { exec } from 'node:child_process';
+import fs from 'node:fs';
+import os from 'node:os';
+import utils from '../utils/videos.js';
 
-module.exports = {
+export default {
 	data: new SlashCommandBuilder()
 		.setName('download')
 		.setDescription('Download a video.')
@@ -99,7 +100,7 @@ async function download(url, interaction) {
 		if (interaction.values[1]) format += '+' + interaction.values[1];
 	}
 
-	downloadVideo(url, interaction.id, format)
+	utils.downloadVideo(url, interaction.id, format)
 		.then(async () => {
 			const file = fs.readdirSync(os.tmpdir()).filter(fn => fn.startsWith(interaction.id));
 			const output = `${os.tmpdir()}/${file}`;
@@ -112,7 +113,7 @@ async function download(url, interaction) {
 				await interaction.followUp('Uh oh! The video you tried to download is too big!', { ephemeral: true });
 			}
 			else if (fileSize > 8) {
-				const fileURL = await upload(output)
+				const fileURL = await utils.upload(output)
 					.catch(err => {
 						console.error(err);
 					});
@@ -130,31 +131,3 @@ async function download(url, interaction) {
 		});
 	return;
 }
-
-async function downloadVideo(url, output, format) {
-	await new Promise((resolve, reject) => {
-		exec(`./bin/yt-dlp -f ${format} ${url} -o "${os.tmpdir()}/${output}.%(ext)s" --force-overwrites`, (err, stdout, stderr) => {
-			if (err) {
-				reject(stderr);
-			}
-			if (stderr) {
-				console.error(stderr);
-			}
-			resolve(stdout);
-		});
-	});
-}
-
-async function upload(file) {
-	return await new Promise((resolve, reject) => {
-		exec(`./bin/upload.sh ${file}`, (err, stdout, stderr) => {
-			if (err) {
-				reject(stderr);
-			}
-			if (stderr) {
-				console.error(stderr);
-			}
-			resolve(stdout);
-		});
-	});
-}
diff --git a/commands/ping.js b/commands/ping.js
index 72de7b41..2f6847c9 100644
--- a/commands/ping.js
+++ b/commands/ping.js
@@ -1,6 +1,6 @@
-const { SlashCommandBuilder } = require('@discordjs/builders');
+import { SlashCommandBuilder } from '@discordjs/builders';
 
-module.exports = {
+export default {
 	data: new SlashCommandBuilder()
 		.setName('ping')
 		.setDescription('Replies with Pong!'),
diff --git a/deploy-guild-commands.js b/deploy-guild-commands.cjs
similarity index 66%
rename from deploy-guild-commands.js
rename to deploy-guild-commands.cjs
index 09d59343..7a59120c 100644
--- a/deploy-guild-commands.js
+++ b/deploy-guild-commands.cjs
@@ -20,6 +20,23 @@ const commands = [
 			option.setName('advanced')
 				.setDescription('Choose the quality of the video.')
 				.setRequired(false)),
+
+
+	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)),
+
+	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)),
 ]
 	.map(command => command.toJSON());
 
diff --git a/events/interactionCreate.js b/events/interactionCreate.js
index f4ae3ab2..8cd1da62 100644
--- a/events/interactionCreate.js
+++ b/events/interactionCreate.js
@@ -1,4 +1,4 @@
-module.exports = {
+export default {
 	name: 'interactionCreate',
 	async execute(interaction) {
 		const client = interaction.client;
diff --git a/events/ready.js b/events/ready.js
index 5f81021d..cd6b3c2a 100644
--- a/events/ready.js
+++ b/events/ready.js
@@ -1,9 +1,10 @@
-const { exec } = require('node:child_process');
-const https = require('node:https');
-require('dotenv').config();
+import { exec } from 'node:child_process';
+import https from 'node:https';
+import dotenv from 'dotenv'
+dotenv.config();
 const { uptimeURL, uptimeInterval } = process.env;
 
-module.exports = {
+export default {
 	name: 'ready',
 	once: true,
 	async execute(client) {
diff --git a/index.js b/index.js
index 61461535..05b60003 100644
--- a/index.js
+++ b/index.js
@@ -1,9 +1,14 @@
-const fs = require('node:fs');
-const path = require('node:path');
-const { Client, Collection, Intents } = require('discord.js');
-require('dotenv').config();
+import fs from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { Client, Collection, Intents } from 'discord.js';
+import dotenv from 'dotenv'
+dotenv.config()
 const { token } = process.env;
 
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
+
 const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
 
 // Load commands from the commands folder
@@ -13,7 +18,8 @@ const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('
 
 for (const file of commandFiles) {
 	const filePath = path.join(commandsPath, file);
-	const command = require(filePath);
+	let command = await import(filePath);
+	command = command.default;
 
 	client.commands.set(command.data.name, command);
 }
@@ -24,7 +30,8 @@ const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'
 
 for (const file of eventFiles) {
 	const filePath = path.join(eventsPath, file);
-	const event = require(filePath);
+	let event = await import(filePath);
+	event = event.default;
 	if (event.once) {
 		client.once(event.name, (...args) => event.execute(...args));
 	}