discord.js自定义动态嵌入帮助命令 查询单个命令无法嵌入如何解决
问题原因
你当前的代码仅在无参数查询全量命令列表时构造了MessageEmbed嵌入实例,查询单个命令的分支逻辑中,仅将命令属性拼接到普通字符串数组后直接发送,没有走嵌入消息构造流程,因此返回纯文本内容。
修复步骤
- 删除无参数分支中冗余的
const description =赋值逻辑,data.push()返回值为数组长度,该变量实际未被使用 - 在单个命令查询的分支末尾,新增
MessageEmbed实例构造逻辑,将拼接好的命令属性填充到嵌入中后再发送
修复后完整代码
module.exports = { name: 'help', aliases: ['h'], description: '帮助命令', cooldown: 5, execute(message, args, cmd, client, Discord) { const data = []; const { commands } = message.client; const prefix = process.env.PREFIX; if (!args.length) { const title = '我的全部命令列表如下:'; // 移除冗余的变量赋值 data.push(commands.map(command => command.name).join(', ')); const footer = `你可以发送 ${prefix}help [命令名] 查看指定命令的详细信息!`; const helpEmbed = new Discord.MessageEmbed() .setColor('RANDOM') .setAuthor(message.author.tag, message.author.displayAvatarURL({ dynamic: true })) .setTitle(title) .setDescription(data) .setTimestamp() .setFooter(footer); return message.author.send(helpEmbed) .then(() => { if (message.channel.type === 'dm') return; message.reply('我已经把全部命令列表发送到你的私信了!'); }) .catch(error => { console.error(`无法给 ${message.author.tag} 发送私信.\n`, error); message.reply(`我好像没法给你发私信! 你是不是关闭了私信权限?`); }); } const name = args[0].toLowerCase(); const command = commands.get(name) || commands.find(c => c.aliases && c.aliases.includes(name)); if (!command) { return message.reply('这不是一个有效的命令!'); } data.push(`**名称:** ${command.name}`); if (command.aliases) data.push(`**别名:** ${command.aliases.join(', ')}`); if (command.description) data.push(`**描述:** ${command.description}`); if (command.usage) data.push(`**用法:** ${prefix}${command.name} ${command.usage}`); if (command.cooldown) data.push(`**冷却时间:** ${command.cooldown} 秒`); // 新增单个命令嵌入构造逻辑 const singleHelpEmbed = new Discord.MessageEmbed() .setColor('RANDOM') .setAuthor(message.author.tag, message.author.displayAvatarURL({ dynamic: true })) .setTitle(`命令 \`${command.name}\` 详情`) .setDescription(data.join('\n')) .setTimestamp() message.channel.send(singleHelpEmbed); } }
如果你使用的是discord.js v13及以上版本,将最后发送嵌入的代码修改为如下格式即可兼容:
message.channel.send({ embeds: [singleHelpEmbed] });
内容的提问来源于stack exchange,提问作者Strqtz
相关产品推荐
相关产品推荐

