Browse Source

Start using separate files for events

pull/25/head
Abheek Dhawan 3 years ago
parent
commit
a419904683
Signed by: abheekd GPG Key ID: 7BE81B8C14475B67
  1. 20
      events/interactionCreate.js
  2. 11
      events/ready.js
  3. 28
      index.js

20
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 });
}
},
};

11
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}!`);
},
};

28
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);

Loading…
Cancel
Save