Discord机器人报错TypeError: Cannot read property 'execute' of undefined求助
Hey there, let's break down why you're getting that TypeError: Cannot read property 'execute' of undefined error and fix it step by step:
Key Issues Causing the Error
Typos & Undefined Variables in Command Files
- Both your
ban.jsandkick.jshave a typo in theexecutefunction parameter:messsage(extra 's') instead ofmessage. This breaks access to the message object entirely. - The
if (command === "ban")/if (command === "kick")checks inside your execute functions are unnecessary and broken—commandisn't defined in those files, which would throw another error. - In
kick.js, you useduserBan(undefined) instead ofuserKickwhen checking for the mentioned user.
- Both your
Incorrect Event Nesting
- You placed the
guildMemberAddevent listener inside themessageevent handler. This means every time a message is sent, you're re-registering the member join event, which causes unexpected behavior and can break other parts of your code.
- You placed the
Missing Core Imports & Login
- Your main code is missing the
discord.jsimport (const Discord = require('discord.js');) and the criticalclient.login()call to start the bot.
- Your main code is missing the
Fixed Code Snippets
Main File (index.js or similar)
const Discord = require('discord.js'); const client = new Discord.Client(); 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.once('ready', () => { console.log('yes its online'); }); // Move guildMemberAdd event outside the message handler client.on('guildMemberAdd', member =>{ const channel = member.guild.channels.cache.find(channel=> channel.name === "hlo"); if(!channel) return; channel.send(`Welcome to our server, ${member}, please read the rules!`); }); 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 === 'ping'){ message.channel.send('pong!'); } else if (command === 'kick'){ // Add a check to avoid the undefined error const kickCommand = client.commands.get('kick'); if(kickCommand){ kickCommand.execute(message, args); } else { message.reply('Kick command not found!'); } } else if (command === 'ban'){ const banCommand = client.commands.get('ban'); if(banCommand){ banCommand.execute(message, args); } else { message.reply('Ban command not found!'); } } }); // Replace with your bot token client.login('YOUR_DISCORD_BOT_TOKEN');
commands/ban.js
module.exports = { name: 'ban', description: "Uses ban hammer", execute(message, args){ const userBan = message.mentions.users.first(); if(userBan){ const member = message.guild.member(userBan); if(member) { member.ban({ reason: 'you broke rules buddy.' }) .then(() => { message.reply(`${userBan.tag} was banned from the server.`) }) .catch(err => { // Catch permission errors or other issues message.reply('I couldn\'t ban that user—make sure I have the Ban Members permission!'); console.error(err); }); } else{ message.reply('That user is not in the server.'); } }else { message.reply('You need to mention a user to ban!') } } }
commands/kick.js
module.exports = { name: 'kick', description: 'kick people', execute(message, args){ const userKick = message.mentions.users.first(); if(userKick){ const member = message.guild.member(userKick); if(member) { member.kick({ reason: 'you broke rules buddy.' }) .then(() => { message.reply(`${userKick.tag} was kicked from the server.`) }) .catch(err => { message.reply('I couldn\'t kick that user—make sure I have the Kick Members permission!'); console.error(err); }); } else{ message.reply('That user is not in the server.'); } }else { message.reply('You need to mention a user to kick!') } } }
Why This Works
- Fixed typos ensure the
messageobject is accessible, and undefined variables are removed. - Moving
guildMemberAddto the top level prevents duplicate event registrations. - Adding existence checks for commands stops the "execute of undefined" error if a command fails to load.
- Error catching in the ban/kick commands helps debug permission issues, which are common with moderation commands.
内容的提问来源于stack exchange,提问作者Patriot
相关产品推荐
相关产品推荐

