|
| 1 | +// Require the necessary discord.js classes |
| 2 | +const { Client, Collection, GatewayIntentBits } = require('discord.js'); |
| 3 | +const { token } = require('./config.json'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('node:path'); |
| 6 | +const mongoose = require('mongoose'); |
| 7 | + |
| 8 | +// Create a new client instance |
| 9 | +const client = new Client({ intents: [GatewayIntentBits.Guilds] }); |
| 10 | + |
| 11 | + |
| 12 | +client.commands = new Collection(); |
| 13 | +const commandsPath = path.join(__dirname, 'commands'); |
| 14 | +const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); |
| 15 | + |
| 16 | +for (const file of commandFiles) { |
| 17 | + const filePath = path.join(commandsPath, file); |
| 18 | + const command = require(filePath); |
| 19 | + // Set a new item in the Collection |
| 20 | + // With the key as the command name and the value as the exported module |
| 21 | + client.commands.set(command.data.name, command); |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +// When the client is ready, run this code (only once) |
| 26 | +client.once('ready', async () => { |
| 27 | + console.log('Ready!'); |
| 28 | +}); |
| 29 | + |
| 30 | +client.on('interactionCreate', async interaction => { |
| 31 | + if (!interaction.isChatInputCommand()) return; |
| 32 | + |
| 33 | + const command = client.commands.get(interaction.commandName); |
| 34 | + |
| 35 | + if (!command) return; |
| 36 | + |
| 37 | + try { |
| 38 | + await command.execute(interaction); |
| 39 | + } catch (error) { |
| 40 | + console.error(error); |
| 41 | + await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); |
| 42 | + } |
| 43 | +}); |
| 44 | + |
| 45 | +run(); |
| 46 | + |
| 47 | +async function run() { |
| 48 | +// connect to mongoose |
| 49 | + await mongoose.connect('mongodb://localhost:27017'); |
| 50 | +// Login to Discord with your client's token |
| 51 | + client.login(token); |
| 52 | +} |
0 commit comments