Advertisement
Guest User

for stackoverflow help

a guest
Jun 1st, 2022
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('node:fs');
  2. const path = require('node:path');
  3. const { Client, Collection, Intents } = require('discord.js');
  4. const { token } = require('./config.json');
  5.  
  6. const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
  7.  
  8. client.commands = new Collection(); // Used for chat commands
  9. client.slash = new Collection(); // Used for slash commands only.
  10.  
  11.  
  12. const commandsPath = path.join(__dirname, 'commands');
  13. const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
  14.  
  15. for (const file of commandFiles) {
  16.     const filePath = path.join(commandsPath, file);
  17.     const command = require(filePath);
  18.     client.commands.set(command.data.name, command); // Add the chat command to the commands collection
  19. }
  20.  
  21. const eventsPath = path.join(__dirname, 'events');
  22. const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
  23.  
  24. for (const file of eventFiles) {
  25.     const filePath = path.join(eventsPath, file);
  26.     const event = require(filePath);
  27.     if (event.once) {
  28.         client.once(event.name, (...args) => event.execute(...args, client)); // This is where we will need to pass 'client'
  29.     } else {
  30.         client.on(event.name, (...args) => event.execute(...args, client)); // Same here. ...args will handle any arguments that come with the event.
  31.     }
  32. }
  33.  
  34. client.once('ready', c => {
  35.     console.log(`Ready! Logged in as ${c.user.tag}`);
  36. });
  37.  
  38. client.on('message', async msg => {
  39.     if(!msg.content.startsWith(config.prefix)) return;
  40.  
  41.     var command = msg.content.substring(1);
  42.  
  43.     if(!client.commands.has(command)) return;
  44.  
  45.     try{
  46.         await client.commands.get(command).execute(msg);
  47.     } catch(error) {
  48.         console.error(error);
  49.         await msg.reply({content: "there was an error", epthemeral: true})
  50.     }
  51. });
  52.  
  53. client.login(token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement