camerond33

index.js

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