如何修复此错误:“未捕获的类型错误:无法读取未定义的属性‘执行’”?

时间:2021-04-23 07:26:48

标签: javascript discord.js

我在 discord.js 中编写 discord bot 但不知何故它不想响应命令,我输入任何命令,它总是关闭并写入错误:“未捕获的类型错误:无法读取未定义的属性‘执行’”,我已经搜索了堆栈溢出,但我什么也没找到,或者我什么都没找到。如果你知道如何解决这个问题,我很乐意回答。谢谢 这是我的 index.js:

    const Discord = require('discord.js');
const client = new Discord.Client();
const {TOKEN} = require('./config.json');
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', () => {
    language = 0;
    console.log('Im online!');                                  // ↓ WATCHING PLAYING STREAMING COMPETETIVE
    client.user.setPresence({ activity: { name: 'Your mom', type: "WATCHING" } });
});
 
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'){
    if(language == 1) {
        client.commands.get('ping-en').execute(message, args);}
    if(language == 0) {
        client.commands.get('ping-cz').execute(message, args);}
    } else if(command === 'help'){
      if(language == 1) {
          client.commands.get('help-en').execute(message, args);}
      if(language == 0) {
        client.commands.get('help-cz').execute(message, args);}
    } else if(command === 'clear'){
      if(language == 1) {
        client.commands.get('clear-en').execute(message, args);}
      if(language == 0)
        client.commands.get('clear-cz').execute(message, args);
    } else if(command === 'kick'){
      if(language == 1){
        client.commands.get('kick-en').execute(message, args);}
      if(language == 0){
        client.commands.get('kick-cz').execute(message, args);}
    } else if(command === 'ban'){
      if(language == 1){
        client.commands.get('ban-en').execute(message, args);}
      if(language == 0){
        client.commands.get('ban-cz').execute(message, args);}
    } else if(command === 'poll'){
      if(language == 1){
        client.commands.get('poll-en').execute(message, args);}
      if(language == 0){
        client.commands.get('poll-cz').execute(message, args);}
    } else if(command === 'broadcast'){
        if(language == 1){
            client.commands.get('broadcast-en').execute(message, args);}
        if(language == 0){
            client.commands.get('broadcast-cz').execute(message, args);}
    } else if(command === 'czech'){
        message.reply('Jazyk byl přepnut na český!')
        language = 0;
        console.log(language);
    } else if(command === 'english'){
        message.reply('Language has been set to english!')
        language = 1;
        console.log(language);
    }

});
    client.login(TOKEN);

这是我的 help.js:

const Discord = require('discord.js')
module.exports.execute = {
    name: 'help',
    description: "Help!",
    execute(message, args){
        let embed = new Discord.MessageEmbed()
        .setTitle('        ↓ Commands ↓')
        .setDescription('\n\n***- Default:***\n`!ping`\n\n***- Moderation:***\n`!clear <number max is 100!>`\n`!kick <member ping>`\n`!ban <member ping>`\n`!poll <ping channel> <message>`\n`!broadcast <ping channel> <message>`')
        .setFooter('By ItsMeDarkness#9538 and L0wiee#9552')
        .setColor('RED')
        message.channel.send(embed);
    }
}

Bot 正常打开,但当我输入任何命令时,它关闭并显示此错误:“未捕获的类型错误:无法读取未定义的属性‘执行’ ” 谢谢回复

1 个答案:

答案 0 :(得分:1)

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

command 具有形式

{
    execute: {
        name: ...,
        description: ...,
        execute: ...
    }
}

command.nameundefinedcommand.execute.name"help"

module.exports.execute = { 和其他中的 module.exports = { 修正为 help-en.js