From d1e26ac9bb2d554d530fd3d412429e55534bcbca Mon Sep 17 00:00:00 2001 From: steev Date: Sun, 25 Dec 2022 22:04:23 +0100 Subject: [PATCH] moved command register function to seperate file --- Commons/Commands.js | 47 +++++++++++++++++++++++++++++++++++++++++++++ index.js | 3 --- sharding.js | 33 +++++++------------------------ 3 files changed, 54 insertions(+), 29 deletions(-) create mode 100644 Commons/Commands.js diff --git a/Commons/Commands.js b/Commons/Commands.js new file mode 100644 index 0000000..5718bb4 --- /dev/null +++ b/Commons/Commands.js @@ -0,0 +1,47 @@ +const fs = require('fs'); +const { REST, Routes } = require('discord.js'); + +/** + * handles reactions in both ways + * @param {string} Path path to Commands folder + */ +const Register = async (path) => { + + if (typeof(path) != "string") { + console.error("ERROR in commands.Register Commands path was not of type string") + process.exit(1) + } + + if (path == "") { + console.error("ERROR in commands.Register Commands path was not of type string") + process.exit(1) + } + + const commands = []; + + /* =============== + * File Import * + =============== */ + const commandFiles = fs.readdirSync(path).filter((file) => file.endsWith(".js")); + for (const file of commandFiles) { + console.log(`loading: ${path}${file}`) + const command = require(`.${path}${file}`); + console.log(command.CommandCreator()) + commands.push(command.CommandCreator()); + } + + console.log(commands) + const rest = new REST({ version: '10' }).setToken(process.env.TOKEN); + (async () => { + try { + console.log('Started refreshing application (/) commands.'); + await rest.put(Routes.applicationCommands(process.env.APPID), { body: commands }); + console.log('Successfully reloaded application (/) commands.'); + } catch (error) { + console.error(error); + } + })(); + console.log("commands registered") +} + +module.exports = {Register} \ No newline at end of file diff --git a/index.js b/index.js index 35dadf2..bf24caf 100644 --- a/index.js +++ b/index.js @@ -33,9 +33,6 @@ bot.on(Events.InteractionCreate, async interaction => { // Handles Reactions bot.on(Events.MessageReactionAdd, async (reaction, user) => { - console.log("reaction") - console.log(reaction) - console.log(reaction._emoji.name, reaction.message.id) HandleReaction(reaction, user, undefined, "add") }) diff --git a/sharding.js b/sharding.js index e878dbe..762763c 100644 --- a/sharding.js +++ b/sharding.js @@ -1,9 +1,9 @@ -// Include discord.js ShardingMana -const { ShardingManager, REST, Routes, Events } = require('discord.js'); -const fs = require('fs'); - +// load .env config require("dotenv").config(); -const commands = []; + +// Include discord.js ShardingMana +const { ShardingManager} = require('discord.js'); +const { Register } = require("./Commons/Commands"); // Create your ShardingManager instance const manager = new ShardingManager("./index.js", { @@ -13,27 +13,8 @@ const manager = new ShardingManager("./index.js", { token: process.env.TOKEN }); -/* =============== -* File Import * -=============== */ -const commandFiles = fs.readdirSync("./Commands/").filter((file) => file.endsWith(".js")); -for (const file of commandFiles) { - const command = require(`./Commands/${file}`); - console.log(command.CommandCreator()) - commands.push(command.CommandCreator()); -} - -console.log(commands) -const rest = new REST({ version: '10' }).setToken(process.env.TOKEN); -(async () => { - try { - console.log('Started refreshing application (/) commands.'); - await rest.put(Routes.applicationCommands(process.env.APPID), { body: commands }); - console.log('Successfully reloaded application (/) commands.'); - } catch (error) { - console.error(error); - } -})(); +// register slash commands +Register("./Commands/") // Emitted when a shard is created manager.on("shardCreate", shard => console.log(`Shard ${shard.id} launched`));