建议命令不起作用,因为“类型错误:无法读取未定义的属性 'execute'”

时间:2021-06-14 16:42:14

标签: node.js discord discord.js

您好,我一直在尝试制作一个 Discord 机器人来创建一个提交命令,但出于某种原因,它总是在我检查过的控制台中显示为“TypeError:无法读取属性 'execute' undefined”由于某种原因无法工作。

//submit.js

module.exports = {
    name: 'sumbit',
    aliases: ['submission', 'suggest'],
    description: 'Submits your article for The Monthly Grind',
    execute(message, args, cmd, client, Discord){
        const channel = message.guild.channels.cache.find(c => c.name === 'submitarticles');
        if(!channel) return message.channel.send('suggestions channel does not exist!');

        let messageArgs = args.join(' ');
        const embed = new Discord.MessageEmbed()
        .setColor('FADF2E')
        .setAuthor(message.author.tag, message.author.displayAvatarURL({ dynamic: true }))
        .setDescription(messageArgs);

        channel.send(embed).then((msg) =>{
            msg.react('?');
            msg.react('?');
            message.delete();
        }).catch((err)=>{
            throw err;
        });
    }
}

// command_handler.js
const fs = require('fs');

module.exports = (client, Discord) =>{
    const command_files = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));

    for(const file of command_files){
        const command = require(`../commands/${file}`);
        if(command.name){
            client.commands.set(command.name, command);
        } else {
            continue
        }
    }
}

// message.js

const command_handler = require('../../handlers/command_handler');

require('dotenv').config();

module.exports = (Discord, client, message) =>{
    const prefix = process.env.PREFIX;

    if(!message.content.startsWith(prefix) || message.author.bot) return;

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

    const command = client.commands.get(cmd) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));

    try {
        command.execute(message, args, cmd, client, Discord);
    } catch (err) {
        message.reply("Sorry there was an error running this command!")
        console.log(err);
    }
}

控制台错误:“TypeError: can't read property 'execute' undefined”

我一直无法找到此错误的原因,我仍在继续查找。

-JFS

1 个答案:

答案 0 :(得分:0)

这意味着 command 未定义。尝试先检查它,如果没有找到 command,则忽略它:

const command = client.commands.get(cmd) 
    || client.commands.find(a => a.aliases && a.aliases.includes(cmd));

if (!command) return;

try {
    command.execute(message, args, cmd, client, Discord);
} catch (err) {
    message.reply("Sorry there was an error running this command!")
    console.log(err);
}

另外,请确保您的命令名称正确。当前的拼写错误 sumbit,因此可以将 name 更改为“提交”:

module.exports = {
    name: 'submit',