Change event handling to scanning files
This commit is contained in:
parent
d715ca02e6
commit
8d1d93ad3f
35
index.js
35
index.js
|
@ -1,7 +1,7 @@
|
||||||
// Require the necessary discord.js classes
|
// Require the necessary discord.js classes
|
||||||
const fs = require('node:fs');
|
const fs = require('node:fs');
|
||||||
const path = require('node:path');
|
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');
|
const { token } = require('./config.json');
|
||||||
|
|
||||||
// Create a new client instance
|
// Create a new client instance
|
||||||
|
@ -29,35 +29,18 @@ for (const folder of commandFolders) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client.on(Events.InteractionCreate, async interaction => {
|
const eventsPath = path.join(__dirname, 'events');
|
||||||
if (!interaction.isChatInputCommand()) return;
|
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
|
||||||
|
|
||||||
console.log(interaction);
|
for (const file of eventFiles) {
|
||||||
|
const filePath = path.join(eventsPath, file);
|
||||||
const command = client.commands.get(interaction.commandName);
|
const event = require(filePath);
|
||||||
|
if (event.once) {
|
||||||
if (!command) {
|
client.once(event.name, (...args) => event.execute(...args));
|
||||||
console.error(`No command matching ${interaction.commandName} was found.`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
} else {
|
||||||
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
|
client.on(event.name, (...args) => event.execute(...args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
// 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
|
// Log in to Discord with your client's token
|
||||||
client.login(token);
|
client.login(token);
|
Loading…
Reference in New Issue