Discord机器人Help嵌入无法显示/fun子命令问题求助
问题解决方案:Discord机器人/help命令无法显示/fun子命令
核心问题分析
你的help.js没能正确遍历/fun命令下的子命令结构——即便检测了option.type=1(子命令类型),但可能存在以下疏漏:
- 未处理子命令组(若/fun使用了分组结构)的递归遍历
- 未正确提取子命令的名称与描述字段
- 命令注册后的结构未被
help.js正确读取
具体修复步骤
1. 确认fun.js的命令注册结构
确保/fun的子命令是按Discord.js规范注册的,示例结构如下:
// fun.js 正确的子命令注册示例 const { SlashCommandBuilder } = require('discord.js'); module.exports = { data: new SlashCommandBuilder() .setName('fun') .setDescription('Fun commands to play with!') .addSubcommand(subcommand => subcommand .setName('8ball') .setDescription('Ask the magic 8ball a question')) .addSubcommand(subcommand => subcommand .setName('av') .setDescription('Get a user\'s avatar')) .addSubcommand(subcommand => subcommand .setName('coinflip') .setDescription('Flip a coin')), async execute(interaction) { // 命令执行逻辑 }, };
2. 修改help.js的子命令遍历逻辑
替换原有的子命令检测代码,改为递归遍历命令的options,同时兼容子命令组和直接子命令:
// help.js 中新增命令信息构建函数 function buildCommandDetails(command) { let details = `\`/${command.data.name}\`: ${command.data.description}\n`; if (command.data.options) { for (const option of command.data.options) { // 处理子命令组(type=2),遍历组内子命令 if (option.type === 2) { details += ` **分组:${option.name}**\n`; for (const subCmd of option.options) { if (subCmd.type === 1) { details += ` \`/${command.data.name} ${option.name} ${subCmd.name}\`: ${subCmd.description}\n`; } } } // 处理直接子命令(type=1) else if (option.type === 1) { details += ` \`/${command.data.name} ${option.name}\`: ${option.description}\n`; } } } return details; }
3. 确保help.js正确加载所有命令
确认命令加载逻辑能读取到所有命令文件的data属性:
// help.js 命令加载示例 const fs = require('fs'); const path = require('path'); const { EmbedBuilder } = require('discord.js'); module.exports = { data: new SlashCommandBuilder().setName('help').setDescription('Show all bot commands'), async execute(interaction) { const commands = []; const commandsDir = path.join(__dirname, 'commands'); const commandFiles = fs.readdirSync(commandsDir).filter(file => file.endsWith('.js')); for (const file of commandFiles) { const cmd = require(path.join(commandsDir, file)); commands.push(cmd); } // 构建帮助Embed const helpEmbed = new EmbedBuilder() .setTitle('机器人命令列表') .setColor('#0099ff'); commands.forEach(cmd => { helpEmbed.addFields({ name: '命令', value: buildCommandDetails(cmd), inline: false }); }); await interaction.reply({ embeds: [helpEmbed] }); }, };
验证修复
重启机器人后执行/help,Embed应显示:
/fun: Fun commands to play with! /fun 8ball: Ask the magic 8ball a question /fun av: Get a user's avatar /fun coinflip: Flip a coin
内容的提问来源于stack exchange,提问作者Siddharth Sinha
相关产品推荐
相关产品推荐

