You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  1. Typos & Undefined Variables in Command Files

    • Both your ban.js and kick.js have a typo in the execute function parameter: messsage (extra 's') instead of message. This breaks access to the message object entirely.
    • The if (command === "ban")/if (command === "kick") checks inside your execute functions are unnecessary and broken—command isn't defined in those files, which would throw another error.
    • In kick.js, you used userBan (undefined) instead of userKick when checking for the mentioned user.
  2. Incorrect Event Nesting

    • You placed the guildMemberAdd event listener inside the message event 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.
  3. Missing Core Imports & Login

    • Your main code is missing the discord.js import (const Discord = require('discord.js');) and the critical client.login() call to start the bot.

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 message object is accessible, and undefined variables are removed.
  • Moving guildMemberAdd to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 18:23:15