You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord.py自定义Help命令无法查询指定命令/分类帮助问题求解

问题根因
  • commands.MinimalHelpCommand 不存在 send_page_command 这个原生回调方法,你自定义的方法不会被框架自动触发,查询特定命令/分类时仍然会走默认的send_pages逻辑
  • 新增代码中 destination = self.get_destination 没有加括号执行方法调用,实际拿到的是函数对象而非目标频道,运行会直接报错
  • 没有针对全局帮助、单命令帮助、分类帮助三种场景分别做逻辑适配
修复方案

重写MinimalHelpCommand内置的三个对应场景的回调方法即可,完整可运行代码如下:

import discord
from discord.ext import commands
import datetime

class CustomHelpCommand(commands.MinimalHelpCommand):
    # 处理!help 不带参数的全局帮助
    async def send_bot_help(self, mapping):
        destination = self.get_destination()
        embed = discord.Embed(
            title="帮助中心",
            description="输入 `!help 命令名` 可查询指定命令的详细说明",
            colour=discord.Colour.blurple(),
            timestamp=datetime.datetime.utcnow()
        )
        # 遍历所有分类和命令添加到Embed,可按需调整样式
        for cog, commands in mapping.items():
            if not cog or not commands:
                continue
            valid_commands = [f"`{cmd.name}`" for cmd in commands if not cmd.hidden]
            if valid_commands:
                embed.add_field(name=cog.qualified_name, value="、".join(valid_commands), inline=False)
        await destination.send(embed=embed)

    # 处理!help 命令名 的单命令查询
    async def send_command_help(self, command):
        destination = self.get_destination()
        embed = discord.Embed(
            title=f"命令说明:{command.name}",
            # 直接读取你给命令设置的description参数
            description=command.description if command.description else "暂无说明",
            colour=discord.Colour.blurple(),
            timestamp=datetime.datetime.utcnow()
        )
        # 可扩展添加用法、别名、权限要求等信息
        if command.usage:
            embed.add_field(name="使用示例", value=f"`!{command.name} {command.usage}`", inline=False)
        await destination.send(embed=embed)

    # 处理!help 分类名 的分类查询
    async def send_cog_help(self, cog):
        destination = self.get_destination()
        embed = discord.Embed(
            title=f"分类说明:{cog.qualified_name}",
            description=cog.description if cog.description else "暂无分类说明",
            colour=discord.Colour.blurple(),
            timestamp=datetime.datetime.utcnow()
        )
        cog_commands = [f"`{cmd.name}`:{cmd.description}" for cmd in cog.get_commands() if not cmd.hidden]
        if cog_commands:
            embed.add_field(name="分类下命令", value="\n".join(cog_commands), inline=False)
        await destination.send(embed=embed)

# 替换机器人默认帮助命令
client.help_command = CustomHelpCommand()
补充说明
  • 代码中默认过滤了标记为hidden=True的隐藏命令,不需要可以删除对应判断
  • 框架会自动识别命令别名,不需要额外做兼容处理
  • 如果需要做权限控制,只需要在对应方法中加权限校验逻辑即可

内容的提问来源于stack exchange,提问作者dogluva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 09:15:01