index.js (view raw)
1const fs = require('node:fs');
2const path = require('node:path');
3const { Client, Collection, GatewayIntentBits } = require('discord.js');
4const { token } = require('./config.json');
5
6const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });
7
8client.commands = new Collection();
9const commandsPath = path.join(__dirname, 'commands');
10const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
11
12for (const file of commandFiles) {
13 const filePath = path.join(commandsPath, file);
14 const command = require(filePath);
15 client.commands.set(command.data.name, command);
16}
17
18client.once('ready', () => {
19 console.log('Ready!');
20});
21
22client.on('interactionCreate', async interaction => {
23 if (!interaction.isChatInputCommand()) return;
24
25 const command = client.commands.get(interaction.commandName);
26
27 if (!command) return;
28
29 try {
30 await command.execute(interaction);
31 }
32 catch (error) {
33 console.error(error);
34 await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
35 }
36});
37
38client.login(token);