Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Require the necessary discord.js classes
- const fs = require('fs');
- const path = require('node:path');
- const { Client, Collection, Intents } = require('discord.js');
- const { token } = require('./config.json');
- const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
- client.commands = new Collection(); // Used for chat commands
- client.slash = new Collection(); // Used for slash commands only.
- const commandsPath = path.join(__dirname, 'commands');
- const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
- for (const file of commandFiles) {
- const filePath = path.join(commandsPath, file);
- const command = require(filePath);
- client.commands.set(command.data.name, command); // Add the chat command to the commands collection
- }
- const eventsPath = path.join(__dirname, 'events');
- const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
- for (const file of eventFiles) {
- const filePath = path.join(eventsPath, file);
- const event = require(filePath);
- if (event.once) {
- client.once(event.name, (...args) => event.execute(...args, client)); // This is where we will need to pass 'client'
- } else {
- client.on(event.name, (...args) => event.execute(...args, client)); // Same here. ...args will handle any arguments that come with the event.
- }
- }
- client.once('ready', c => {
- console.log(`Ready! Logged in as ${c.user.tag}`);
- });
- client.on('message', async msg => {
- if(!msg.content.startsWith(config.prefix)) return;
- var command = msg.content.substring(1);
- if(!client.commands.has(command)) return;
- try{
- await client.commands.get(command).execute(msg);
- } catch(error) {
- console.error(error);
- await msg.reply({content: "there was an error", epthemeral: true})
- }
- });
- // Login to Discord with your client's token
- client.login(token);
Add Comment
Please, Sign In to add comment