0

I am new to JS and programming in general, so please explain with that in mind. I know that this question is not formulated very well, but I couldn't think of how to formulate it better so feel free to edit it. I am making a discord bot with discord.js. I have made a command called 'test'. My code is as follows (in a file named test.js):

module.exports = {
    name: 'test',
    description: 'Test command. Sends back "Test successful."',
    prefixType: 'devPrefix',
    execute(msg, args) {
        if (!args.length){
            msg.channel.send('Test successful.');
        } else {
            msg.channel.send('Test successful. You entered ' + args.length + ' argument(s).')
        }   
    },
};

How do i get the value prefixType and use it in my main index.js file? However the solution needs to work for any file in a folder (called 'commands'), not just with test.js.

If this helps, I am including the code in my main index.js file that deals with handling and executing commands:

const Discord = require('discord.js');
const fs = require('fs');   //For commands (fs = file system)
const botClient = new Discord.Client;
const CONFIG = require('./config.json');

//Get the commands
botClient.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    botClient.commands.set(command.name, command);
}

botClient.once('ready', () => {
    console.log('Bot online and ready.');
});

botClient.on('message', msg => {
    if ((!msg.content.startsWith(CONFIG.prefix) && !msg.content.startsWith(CONFIG.devPrefix)) 
        || msg.author.bot) return;

    const args = msg.content.slice(CONFIG.prefix.length).split(' ');
    const commandName = args.shift().toLowerCase();

    //If the command doesn't exist
    if (!botClient.commands.has(commandName)){
        msg.reply("That command does not exist. Do a.commands for a list of all commands");
        return;
    }

    const command = botClient.commands.get(commandName);

    try {
        command.execute(msg, args);
    } catch (error) {
        console.error(error);
        console.log('Error when trying to get and execute a command.');
        msg.reply('There was an error trying to execute that command.');     
    }
});

Thanks

5
  • require('test.js').prefixType? Commented Apr 6, 2020 at 15:52
  • Or const { prefixType } = require('test') Commented Apr 6, 2020 at 15:58
  • @terrymorse Thanks, but this needs to work with any file, not just test.js (like i said in the question). Commented Apr 6, 2020 at 16:17
  • @terrymorse are you sure? Can you not use something similar to require(`./commands/${file}`); Commented Apr 6, 2020 at 16:33
  • @cs09 You're right, you can specify the require path with a string variable. So const { prefixType } = require(filePath) will work. Commented Apr 6, 2020 at 16:40

1 Answer 1

1

Since you export that in command file, which you later require and put into botClient.commands Collection, you can access prefixType anywhere else you have access to your client. botClient.commands.get('test').prefixType will give you that property.

Additionally, you can access client property from most of Discord.js structures, like Message or User by doing message.client or user.client. And since your Collection of commands is attached to your client object, you can access it like in example above.

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

4 Comments

Thanks for answering, but I stated in the question this needs to work dynamically with any .js file, not just test.js.
So it's botClient.commands.get(commandName).prefixType or where do you want to use it?
@Sven31415 Yes, that works, thank you. Can i also use this to get any const or let inside the execute{} block?
Not really, as let and const are block scoped, so they are limited to that execute only. You would have to either return them from there, or have other exported object to export to outside of the file, and use it from it inside the file.

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.