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

153
Visualizações
TypeError: Cannot read properties of undefined (reading 'execute') command handler
    const discord = require('discord.js')

const client = new discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })

const prefix = '&';

const fs = require('fs');

client.commands = new discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
}

client.on('ready', () => {
    console.log("================");
    console.log("|Bot is ready|");
    console.log("================");
});

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

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

    if(command === 'ip'){
        client.commands.get('ip').execute(message, args);
    } else if (command == 'creator'){
        client.commands.get('creator').execute(message, args);
    } else if (command == 'rulespost'){
        client.commands.get('rulespost').execute(message, args, discord);
    } else if(command == 'test'){
        client.commands.get('test').execute(message, args);
    }
    
})

And with this I get an error with 'rulespost' saying TypeError: Cannot read properties of undefined (reading 'execute') and within rulespost.js is

    module.exports = {
    name: 'RulesPost',
    description: "Posts the rules of the server",
    execute(message, args, discord) {
        const newEmbed = Discord.MessageEmbed()
        .setColor('#000000')
        .setTitle('Official Rules for ALL Platforms')
        .setDescription('...')
        .addFields(
            {value: 'TEXT'}
        )
        message.channel.send(newEmbed)


    }
}

And when using the &rulespost command the bot dies and nothing else happens. all other commands work fine with no problems but trying to use the embed command it kills the bot completely.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Note this is WRONG, however it can not be taken down as it is marked for some reason.

the syntax for the execute function is incorrect. The interpreter thinks that you are calling a function and then passing further items to the object. this can be fixed by adding the function keyword or by assigning execute to an anonymous function like so:

module.exports = {
    name: 'RulesPost',
    description: "Posts the rules of the server",
    // Execute is the key for the anon. function 
    execute: function(message, args, discord) {
        const newEmbed = Discord.MessageEmbed()
        .setColor('#000000')
        .setTitle('Official Rules for ALL Platforms')
        .setDescription('...')
        .addFields(
            {value: 'TEXT'}
        )
        message.channel.send(newEmbed)
    }
}

This creates the execute function that you are exporting.

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