Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda