TypeError: Cannot read property 'has' of undefined

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

const client = new Discord.Client();
const {token, prefix} = require(./config.json);

client.once(‘ready’, () => {
console.log(‘Ready!’);
});

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

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

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

try {
client.commands.get(command).execute(message, args);
} catch (error) {
console.error(error);
message.reply(‘there was an error trying to execute that command!’);
}
});

client.login(token);

I have used this exact code before on another bot but now for whatever reason it will not work and I am not sure why.

client.commands is undefined. You need to define it.

3 Likes

Seems like your code was from the discord.js guide. Like @Ghostoblivion said you will need to add in the part where the bot will look for commands

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.