簡體   English   中英

建議命令不起作用,因為“TypeError:無法讀取屬性‘執行’未定義”

[英]Suggest command not working because "TypeError: can't read property 'execute' undefined"

您好,我一直在嘗試制作 Discord 機器人來創建提交命令,由於某種原因,它總是在控制台中顯示“TypeError: can't read property '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:無法讀取屬性‘執行’未定義”

我一直無法找到這個錯誤的原因,我仍在繼續尋找。

-JFS

這意味着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',

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM