简体   繁体   English

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

[英]JavaScript,Discord.js, Node.js TypeError: Cannot read property 'execute' of undefined

I'm building a discord bot and I want to save information in bdays.json but this error pops up.我正在构建一个 discord 机器人,我想将信息保存在 bdays.json 但弹出此错误。 All other commands are working just fine but I am getting this error:所有其他命令都可以正常工作,但我收到此错误:

TypeError: Cannot read property 'execute' of undefined TypeError:无法读取未定义的属性“执行”

What should I do?我应该怎么办?

main.js 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');

and remeber.js并记住.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!');
        });
    }
}

What should I do?我应该怎么办?

You have a typo in your code...您的代码中有错字...

In remeber.js you give the command the name of remeber but then in your main.js file you use client.commands.get('remember').execute(message, args);remeber.js ,您将命令命名为remeber ,然后在main.js文件中使用client.commands.get('remember').execute(message, args);

To fix it, use either:要修复它,请使用:

// 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!');
        });
    }
}

Or replace the line with the typo with this instead:或者用这个代替错字的行:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法读取 discord.js 中未定义的属性“执行” - TypeError: Cannot read property 'execute' of undefined in discord.js Node.js Discord.js UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“calculatedPosition” - Node.js Discord.js UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'calculatedPosition' of undefined 类型错误:无法读取未定义 Discord.js node.js v12.16.3 的属性“第一个” - TypeError: Cannot read property 'first' of undefined Discord.js node.js v12.16.3 Discord.js 错误 - 类型错误:无法读取未定义的属性“执行” - Discord.js Error - TypeError: Cannot read property 'execute' of undefined TypeError:无法读取未定义的属性“执行”(Discord.js) - TypeError: Cannot read property 'execute' of undefined (Discord.js) JavaScript(discord.js)TypeError:无法读取未定义的属性“startsWith” - JavaScript(discord.js) TypeError: Cannot read property 'startsWith' of undefined 类型错误:无法读取未定义 Discord.js javascript 的属性“添加” - TypeError: Cannot read property 'add' of undefined Discord.js javascript 使用 Node.js、Mongoose 和 Discord.js 的未定义错误 [无法读取未定义的属性] - Undefined errors using Node.js, Mongoose, and Discord.js [Cannot read property of undefined] node.js/discord.js:类型错误:无法读取 null 的属性“setPresence” - node.js/discord.js: TypeError: Cannot read property 'setPresence' of null node.js discord.js 无法读取未定义的属性“发送” - node.js discord.js cannot read property 'send' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM