Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
TypeError: bot.commands.get(...).run is not a function

I've just been trying to make a command handler on discord.js and whenever I run the bot, it throws

TypeError: bot.commands.get(...).run is not a function

Here's the bottom of the code on where the error is happening.

bot.on("message", async message => {

    if(message.author.bot) return;   
    if(message.channel.type === 'dm') return;

    if(message.content.startsWith(prefix)) {
        const args = message.content.slice(prefix.length).trim().split(/ +/);

        const command = args.shift().toLowerCase();

        if(!bot.commands.has(command)) return;


        try {
            bot.commands.get(command).run(bot, message, args);
        } catch (error){
            console.error(error);
        }
    }
})

bot.login(token);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It seems like you command handler is a little bit incomplete. Here is how I have mine setup, you can try it and tell me if it works (I explain everything with the comments :

// your bot.js or main.js file
const bot = new Discord.Client(); // I setup the bot client
bot.commands = new Discord.Collection(); // I create a collection with all the commands

const commandsFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); 
// In my project tree, I define all the files with the .js extension in this folder as command files. I will add a file (.js) to this folder for each command I want to create. For example, there will be './commands/ping.js' inside.

for(const file of commandsFiles){
  const command = require(`./commands/${file}`);
  client.commands.set(command.name, command);
}
// For each command file detected, I add it's name and it's path to my client.commands collection


client.on('message', message => {

  if(message.author.bot) return;   
  if(message.channel.type === 'dm') return;

  if(message.content.startsWith(prefix)) {

    const args = message.content.slice(prefix.length).trim().split(/ +/); 
    const command = args.shift().toLowerCase();

    if(!bot.commands.has(command)) return;
    // if the collection doesn't have a command with this name, return

    try {
      bot.commands.get(command).run(bot, message, args);
      // I get the command object defined previously in the 'for' loop, and I execute the run function with bot, message and args as arguments
    } catch (error){
      console.error(error);
    }
  }

}

Now that you have your command handle set up, for each command that you want to create, add a file inside the ./commands/ folder (for example, ./commands/ping.js, don't forget the .js part). In this newly created file, write :

module.exports = {
  name : 'ping',
  description : 'Ping command',
  run(bot, message, args) {

  // the code to be executed

}

Here you can see that we define an object, that will be used by the command handler.

When we do client.commands.set(command.name, command) (in bot.js or main.js), we use the name defined here.

When we do client.commands.get(command).run(bot, message, args), we run the run function defined inside of the module.export object of the targetted command file.

Don't hesitate to tell me if there's anything you don't understand.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!