[英]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.