discord.js运行help命令报Cannot read property 'MessageEmbed' of undefined错误
报错原因与修复方案
核心报错原因
TypeError: Cannot read property 'MessageEmbed' of undefined 报错的根本原因是你定义的execute函数接收的第三个参数Discord为undefined,调用该方法时没有传入Discord模块实例,因此访问Discord.MessageEmbed失败。
其他语法问题
你的代码还存在链式调用后的多余逗号问题,setDescription('here are the commands'),、addFields(...),末尾的逗号会触发语法报错,需要删除。
修复方案
方案一:调用execute时传入Discord实例
在你的bot主文件中调用命令execute方法的位置,补充传入Discord模块参数:
// 主文件示例代码 const Discord = require('discord.js'); // 命令触发逻辑 client.on('messageCreate', message => { // ... 前缀判断、命令匹配逻辑 const command = client.commands.get(cmdName); command.execute(message, args, Discord); // 确保第三个参数传入Discord })
方案二:命令文件直接引入Discord模块
你也可以直接在help命令文件顶部引入Discord,无需依赖传参:
const Discord = require('discord.js'); module.exports = { name: 'help', description: "this is the help command", execute(message, args){ const newEmbed = new Discord.MessageEmbed() .setColor('#304281') .setTitle('Commands') .setDescription('here are the commands') .addFields( {name: '-help', value: 'The help command'}, {name: '-ping', value: 'Ping pong**'} ) .setFooter('thanks for using the bot') message.channel.send(newEmbed); } }
v14版本适配说明
如果你使用的是discord.js v14及以上版本,MessageEmbed已经被EmbedBuilder替代,调整写法即可:
const { EmbedBuilder } = require('discord.js'); // 实例化代码改为 const newEmbed = new EmbedBuilder()
内容的提问来源于stack exchange,提问作者A code
相关产品推荐
相关产品推荐

