建议命令不起作用,因为“TypeError:无法读取未定义的属性‘execute’”
2021-06-14
41
您好,我一直在尝试制作一个 Discord 机器人来创建提交命令,但由于某种原因,它在控制台中总是显示“TypeError:无法读取未定义的属性‘execute’”,我已经检查过它由于某种原因无法正常工作。
//submit.js
module.exports = {
name: 'sumbit',
aliases: ['submission', 'suggest'],
description: 'Submits your article for The Monthly Grind',
execute(message, args, cmd, client, Discord){
const channel = message.guild.channels.cache.find(c => c.name === 'submitarticles');
if(!channel) return message.channel.send('suggestions channel does not exist!');
let messageArgs = args.join(' ');
const embed = new Discord.MessageEmbed()
.setColor('FADF2E')
.setAuthor(message.author.tag, message.author.displayAvatarURL({ dynamic: true }))
.setDescription(messageArgs);
channel.send(embed).then((msg) =>{
msg.react('👍');
msg.react('👎');
message.delete();
}).catch((err)=>{
throw err;
});
}
}
// command_handler.js
const fs = require('fs');
module.exports = (client, Discord) =>{
const command_files = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of command_files){
const command = require(`../commands/${file}`);
if(command.name){
client.commands.set(command.name, command);
} else {
continue
}
}
}
// message.js
const command_handler = require('../../handlers/command_handler');
require('dotenv').config();
module.exports = (Discord, client, message) =>{
const prefix = process.env.PREFIX;
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();
const command = client.commands.get(cmd) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));
try {
command.execute(message, args, cmd, client, Discord);
} catch (err) {
message.reply("Sorry there was an error running this command!")
console.log(err);
}
}
控制台错误:“TypeError:无法读取未定义的属性‘execute’”
我一直找不到这个错误的原因,我仍在继续寻找。
-JFS
1个回答
这意味着
command
未定义。请先尝试检查,如果未找到
command
,则忽略它:
const command = client.commands.get(cmd)
|| client.commands.find(a => a.aliases && a.aliases.includes(cmd));
if (!command) return;
try {
command.execute(message, args, cmd, client, Discord);
} catch (err) {
message.reply("Sorry there was an error running this command!")
console.log(err);
}
此外,请确保您的命令名称正确。当前命令名称拼写错误为
sumbit
,因此可以将
name
更改为“submit”:
module.exports = {
name: 'submit',
Freya
2021-06-14