1

I am trying to iterate through a folder of command files to make a help command for a discord bot. This is my code so far.

module.exports = {
  name: 'help',
  description: 'Lists current commands.',
  execute(message) {
    //was the first time i made something interesting out of a for loop
    if (message.content.toLowerCase() === '$help') {
      const commands = 'C:/Bot/commands';
      const Discord = require('discord.js');
      const helpEmbed = new Discord.MessageEmbed()
        .setTitle("Commands")
        .setColor(0x6e7175)
        .setFooter('Provided by Echo', 'https://cdn.discordapp.com/avatars/748282903997186178/6288e1f487e111b211aa9966c583d948.png?size=128')
        .setTimestamp()

      for (i of commands) {
        let title = i.name
        let value = i.description
        helpEmbed.addField(title, value)
      }
      message.channel.send(helpEmbed)
    }
  }
}

C:/Bot/commands is the folder in which all the commands are stored, at the moment i.name and i.description are undefined. What's the issue here?

1
  • 1
    how does commands structure look like? Commented Oct 30, 2020 at 15:14

2 Answers 2

1

C:/Bot/commands is the folder with stored comands, but string 'C:/Bot/commands' is not a folder - it is a string.

For iterate through folder you need to read it by fs.readdir or sync version

Sign up to request clarification or add additional context in comments.

Comments

1

commands is currently just a string. If you want to get an array of all files in a folder, use fs.

const fs = require('fs'); // node.js built in module
const files = fs.readdierSync('../commands'); // get the names of all files in the foler

for (file of files) {
 // now you can require the file
 const { name, description } = require(`../commands/${file}`);
 embed.addField(name, description);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.