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

914
Visualizações
Cannot read properties of undefined (reading 'get') discord.js

So I recently started developing a discord bot using discord.js, and when I'm trying to run this command, it is raising an error when I update the bot at the line where I execute the command (client.commands.get('kick').execute(message, args);).

The error is:

TypeError: Cannot read properties of undefined (reading 'get'
    at Client.<anonymous> (C:\Users\Shushan\Desktop\discordbot\main.js:18:25)
    at Client.emit (node:events:527:28) )

And here are my code files:

main.js

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = '?';

client.once('ready', () => {
    console.log("Shushbot is online!");
})

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 === 'kick') {
        client.commands.get('kick').execute(message, args);
    }
})

client.login('SECURITY TOKEN');

kick.js

module.exports = {
    name: 'kick',
    description: 'This Command Kicks People!',
    execute(message, args) {
        const member = message.mentions.users.first();
        if(member) {
            const memberTarget = message.guild.members.cache.get(member.id);
            memberTarget.kick();
            message.channel.send("This User Has Been Kicked From The Server.");
        } else {
            message.channel.send("This User Couldn't Be Kicked.");
        }
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you should check that : https://discordjs.guide/creating-your-bot/command-handling.html#reading-command-files

So far, your bot (client) don't know the commands method, and even if he knows, get dosent exists at all.

You have to write some code to create and set these commands, exemple from doc :


client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);
    // Set a new item in the Collection
    // With the key as the command name and the value as the exported module
    client.commands.set(command.data.name, command);
}

You can do it that way or an other, but at this time your object is empty.

Even your client.commands is undefined !

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