Guest User

Export

a guest
Nov 5th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. const fs = require('node:fs');
  2. const path = require('node:path');
  3. const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
  4. const { token, clientId } = require('./config.json');
  5.  
  6. const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]});
  7.  
  8. client.commands = new Collection();
  9. const foldersPath = path.join(__dirname, 'commands');
  10. const commandFolders = fs.readdirSync(foldersPath);
  11.  
  12. for (const folder of commandFolders) {
  13. const commandsPath = path.join(foldersPath, folder);
  14. const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
  15. for (const file of commandFiles) {
  16. const filePath = path.join(commandsPath, file);
  17. const command = require(filePath);
  18. if ('data' in command && 'execute' in command) {
  19. client.commands.set(command.data.name, command);
  20. } else {
  21. console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
  22. }
  23. }
  24. }
  25.  
  26.  
  27. client.once(Events.ClientReady, readyClient => {
  28. console.log(`Ready! Logged in as ${readyClient.user.tag}`);
  29. });
  30.  
  31. client.on(Events.InteractionCreate, async interaction => {
  32. if (!interaction.isChatInputCommand()) return;
  33. const command = interaction.client.commands.get(interaction.commandName);
  34.  
  35. if (!command) {
  36. console.error(`No command matching ${interaction.commandName} was found.`);
  37. return;
  38. }
  39.  
  40. try {
  41. await command.execute(interaction);
  42. } catch (error) {
  43. console.error(error);
  44. if (interaction.replied || interaction.deferred) {
  45. await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
  46. } else {
  47. await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
  48. }
  49. }
  50. });
  51.  
  52. client.login(token); //login into discord with the token
Advertisement
Add Comment
Please, Sign In to add comment