From 8d1d93ad3f6bd00862109ce7c34ff6421adfab7a Mon Sep 17 00:00:00 2001 From: baz Date: Fri, 17 Nov 2023 23:48:17 +0000 Subject: [PATCH] Change event handling to scanning files --- index.js | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index b5ec6c6..6e06d54 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ // Require the necessary discord.js classes const fs = require('node:fs'); const path = require('node:path'); -const { Client, Collection, Events, GatewayIntentBits } = require('discord.js'); +const { Client, Collection, GatewayIntentBits } = require('discord.js'); const { token } = require('./config.json'); // Create a new client instance @@ -29,35 +29,18 @@ for (const folder of commandFolders) { } } -client.on(Events.InteractionCreate, async interaction => { - if (!interaction.isChatInputCommand()) return; +const eventsPath = path.join(__dirname, 'events'); +const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); - console.log(interaction); - - const command = client.commands.get(interaction.commandName); - - if (!command) { - console.error(`No command matching ${interaction.commandName} was found.`); - return; +for (const file of eventFiles) { + const filePath = path.join(eventsPath, file); + const event = require(filePath); + if (event.once) { + client.once(event.name, (...args) => event.execute(...args)); + } else { + client.on(event.name, (...args) => event.execute(...args)); } - - try { - await command.execute(interaction); - } catch (error) { - console.error(error); - if (interaction.replied || interaction.deferred) { - await interaction.followUp({ content: 'There was an error while executing the command!', ephemeral: true }); - } else { - await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); - } - } -}); - -// When the client is ready, run this code (only once) -// We use 'c' for the event parameter to keep it separate from the already defined 'client' -client.once(Events.ClientReady, c => { - console.log(`Ready! Logged in as ${c.user.tag}`); -}); +} // Log in to Discord with your client's token client.login(token); \ No newline at end of file