From 19919252130bf7038fd3fc6b9c092ea8b9ac0cd0 Mon Sep 17 00:00:00 2001
From: Supositware <sup@libtar.de>
Date: Tue, 11 Apr 2023 14:34:01 +0200
Subject: [PATCH] Download and load a command

---
 commands/owner/download&load.js | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 commands/owner/download&load.js

diff --git a/commands/owner/download&load.js b/commands/owner/download&load.js
new file mode 100644
index 0000000..48e8930
--- /dev/null
+++ b/commands/owner/download&load.js
@@ -0,0 +1,30 @@
+import { SlashCommandBuilder } from 'discord.js';
+import util from 'node:util';
+import stream from 'node:stream';
+import fs from 'node:fs';
+
+export default {
+	data: new SlashCommandBuilder()
+		.setName('downloadandload')
+		.setDescription('Download a command and load it.')
+		.addAttachmentOption(option =>
+			option.setName('file')
+				.setDescription('The .js file that will be loaded by the bot.')
+				.setRequired(true)),
+	category: 'owner',
+	ownerOnly: true,
+	async execute(interaction, args, client) {
+		await interaction.deferReply();
+
+		const streamPipeline = util.promisify(stream.pipeline);
+		const res = await fetch(args.file.url);
+		if (!res.ok) return interaction.editReply('An error has occured while trying to download your image.');
+		await streamPipeline(res.body, fs.createWriteStream(`./tmp/${args.file.name}`));
+
+		let command = await import(`../../tmp/${args.file.name}`);
+		command = command.default;
+
+		client.commands.set(command.data.name, command);
+		return await interaction.editReply(`${command.data.name} has been loaded.`);
+	},
+};