Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

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. All other commands are working just fine but I am getting this error:

TypeError: Cannot read property 'execute' of undefined

What should I do?

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

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?

question from:https://stackoverflow.com/questions/65646704/javascript-discord-js-node-js-typeerror-cannot-read-property-execute-of-unde

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...