diff --git a/events/interactionCreate.js b/events/interactionCreate.js new file mode 100644 index 0000000..e364cf2 --- /dev/null +++ b/events/interactionCreate.js @@ -0,0 +1,20 @@ +module.exports = { + name: 'interactionCreate', + once: false, + async execute(interaction) { + const client = interaction.client; + if (!interaction.isCommand()) return; + + const command = client.commands.get(interaction.commandName); + + if (!command) return; + + try { + await command.execute(interaction); + } + catch (error) { + console.error(error); + await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + } + }, +}; diff --git a/events/ready.js b/events/ready.js new file mode 100644 index 0000000..e4a0aa7 --- /dev/null +++ b/events/ready.js @@ -0,0 +1,11 @@ +const db = require('../helpers/db'); +const { mongoUri } = require('../helpers/env'); + +module.exports = { + name: 'ready', + once: true, + async execute(client) { + await db.connect(mongoUri); + console.log(`Logged in at ${client.user.tag}!`); + }, +}; diff --git a/index.js b/index.js index d56cca6..3104c0f 100755 --- a/index.js +++ b/index.js @@ -1,9 +1,8 @@ #!/usr/bin/env node const fs = require('node:fs'); -const db = require('./helpers/db'); const { Client, Collection, Intents } = require('discord.js'); -const { token, mongoUri } = require('./helpers/env'); +const { token } = require('./helpers/env'); const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.DIRECT_MESSAGE_REACTIONS], @@ -12,30 +11,21 @@ const client = new Client({ client.commands = new Collection(); const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); +const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js')); for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(command.data.name, command); } -client.once('ready', async () => { - db.connect(mongoUri); -}); - -client.on('interactionCreate', async interaction => { - if (!interaction.isCommand()) return; - - const command = client.commands.get(interaction.commandName); - - if (!command) return; - - try { - await command.execute(interaction); +for (const file of eventFiles) { + const event = require(`./events/${file}`); + if (event.once) { + client.once(event.name, (...args) => event.execute(...args)); } - catch (error) { - console.error(error); - await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + else { + client.on(event.name, (...args) => event.execute(...args)); } -}); +} client.login(token);