From 0c351c1037f4715c23a1ff0a447eb5c383215f1f Mon Sep 17 00:00:00 2001 From: Minzkraut Date: Wed, 13 Apr 2022 16:53:26 +0200 Subject: [PATCH] Move eventhandlers to separate files --- events/interactionCreate.js | 24 +++++++++++++++++ events/ready.js | 31 ++++++++++++++++++++++ index.js | 53 +++++++------------------------------ 3 files changed, 64 insertions(+), 44 deletions(-) create mode 100644 events/interactionCreate.js create mode 100644 events/ready.js diff --git a/events/interactionCreate.js b/events/interactionCreate.js new file mode 100644 index 0000000..4898248 --- /dev/null +++ b/events/interactionCreate.js @@ -0,0 +1,24 @@ +require("dotenv").config(); +const { REST } = require("@discordjs/rest"); +const { Routes } = require("discord-api-types/v9") + +module.exports = { + name: "interactionCreate", + async execute (interaction) { + if (!interaction.isCommand()) return; + + const command = interaction.client.commands.get(interaction.commandName); + + if (!command) return; + + try { + await command.execute(interaction); + } catch (err) { + if (err) console.log(err); + await interaction.reply({ + content: "An error occured processing the command", + ephemeral: true + }); + } + } +} \ No newline at end of file diff --git a/events/ready.js b/events/ready.js new file mode 100644 index 0000000..bc152a4 --- /dev/null +++ b/events/ready.js @@ -0,0 +1,31 @@ +require("dotenv").config(); +const { REST } = require("@discordjs/rest"); +const { Routes } = require("discord-api-types/v9") + +module.exports = { + name: "ready", + once: true, + execute (client, commands) { + console.log("Bot started."); + + const CLIENT_ID = client.user.id; + const rest = new REST({ + version: 9 + }).setToken(process.env.TOKEN); + + (async () => { + try { + console.log("Registering commands..."); + if(process.env.ENV === "production") { + await rest.put(Routes.applicationCommands(CLIENT_ID), {body: commands }); + console.log("Global commands registered"); + } else { + await rest.put(Routes.applicationGuildCommands(CLIENT_ID, process.env.GUILD_ID), {body: commands }); + console.log("Local commands registered"); + } + } catch (err){ + if (err) console.log(err); + } + })(); + } +} \ No newline at end of file diff --git a/index.js b/index.js index c1ee48d..1f1e982 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,6 @@ require("dotenv").config(); const fs = require("fs"); -const { REST } = require("@discordjs/rest"); -const { Routes } = require("discord-api-types/v9") const {Client, Intents, Collection} = require("discord.js"); const client = new Client({intents: [ @@ -13,9 +11,7 @@ const client = new Client({intents: [ ]}); const commandFiles = fs.readdirSync("./commands").filter(file => file.endsWith(".js")); - const commands = []; - client.commands = new Collection(); for (const file of commandFiles) { @@ -24,47 +20,16 @@ for (const file of commandFiles) { client.commands.set(command.data.name, command); } -client.once("ready", () => { - console.log("Bot started."); - - const CLIENT_ID = client.user.id; - const rest = new REST({ - version: 9 - }).setToken(process.env.TOKEN); - - (async () => { - try { - console.log("Registering commands..."); - if(process.env.ENV === "production") { - await rest.put(Routes.applicationCommands(CLIENT_ID), {body: commands }); - console.log("Global commands registered"); - } else { - await rest.put(Routes.applicationGuildCommands(CLIENT_ID, process.env.GUILD_ID), {body: commands }); - console.log("Local commands registered"); - } - } catch (err){ - if (err) console.log(err); - } - })(); -}); - -client.on("interactionCreate", async interaction => { - if (!interaction.isCommand()) return; - - const command = client.commands.get(interaction.commandName); - - if (!command) return; - - try { - await command.execute(interaction); - } catch (err) { - if (err) console.log(err); - await interaction.reply({ - content: "An error occured processing the command", - ephemeral: true - }); +const eventFiles = fs.readdirSync("./events").filter(file => file.endsWith(".js")); +for (const file of eventFiles) { + const event = require(`./events/${file}`); + + if (event.once) { + client.once(event.name, (...args) => event.execute(...args, commands)); + } else { + client.on(event.name, (...args) => event.execute(...args, commands)); } -}); +} client.login(process.env.TOKEN);