JavaScript、Discord.js、Node.js TypeError:无法读取未定义的属性“执行”

时间:2021-01-09 19:21:43

标签: javascript node.js discord.js

我正在构建一个不和谐的机器人,我想在 bdays.json 中保存信息,但会弹出这个错误。所有其他命令都运行良好,但出现此错误:

TypeError: 无法读取未定义的属性 'execute'

我该怎么办?

main.js

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('Bot is online!');
});

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 === 'jsidement') {
        client.commands.get('ping').execute(message, args);
    } else if (command === 'help') {
        client.commands.get('help').execute(message, args, Discord);
    } else if (command === 'mute') {
        client.commands.get('mute').execute(message, args);
    } else if (command === 'unmute') {
        client.commands.get('unmute').execute(message, args);
    } else if (command === 'remember') {
        client.commands.get('remember').execute(message, args);
    }
})

client.login('Token');

和remeber.js

module.exports = {
    name: 'remeber',
    description: 'this is a remember command!',
    execute(message, args){
        const fs = require('fs');
        client.bdays = require ('./bdays.json');
        
        client.bdays [message.author.username] = {
            message: message.content
        }
        fs.writeFile('./bdays.json', JSON.stringify (client.bdays, null, 4), err => {
            if(err) throw err;
            message.channel.send('Saved!');
        });
    }
}

我该怎么办?

1 个答案:

答案 0 :(得分:0)

你的代码有错别字...

remeber.js 中,您将命令的名称命名为 remeber,但随后在您的 main.js 文件中,您使用了 client.commands.get('remember').execute(message, args);

要修复它,请使用:

// remember.js
module.exports = {
    name: 'remember',
    description: 'this is a remember command!',
    execute(message, args){
        const fs = require('fs');
        client.bdays = require ('./bdays.json');
        
        client.bdays [message.author.username] = {
            message: message.content
        }
        fs.writeFile('./bdays.json', JSON.stringify (client.bdays, null, 4), err => {
            if(err) throw err;
            message.channel.send('Saved!');
        });
    }
}

或者用这个替换有错字的行:

client.commands.get('remeber').execute(message, args);