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

discord.py自定义HelpCommand报qualified_name属性错误

排查思路
  • 沿错误回溯栈逐层定位:错误触发于get_command_signature方法访问传入对象的qualified_name属性环节,报错信息明确显示当前传入的对象是discord.ext.commands模块,模块本身不存在qualified_name属性。
  • 反查get_command_signature的调用位置:send_command_help方法调用该方法时,传入的参数是全局导入的commands模块,而非方法形参接收的单命令实例command,这是触发本次报错的直接原因。
  • 全量检查代码逻辑,还存在3处隐藏问题:
    • send_cog_help中调用cog.get_commmands()时方法名拼写错误,多写了一个m,正确方法名为get_commands(),触发cog帮助逻辑时会抛出属性错误。
    • send_cog_help的Embed标题markdown标记错误,多写了一个*,会导致加粗格式渲染异常。
    • send_bot_help生成命令列表时遍历的是未经过滤的原始命令集合commands,而非经过权限校验的filtered集合,会向用户展示其无权限调用的命令。
修复后代码
class HelpCommand(commands.HelpCommand):
    color = 0x171616

    def footer(self):
        return f"{self.clean_prefix}{self.invoked_with} <command> for more details"

    def get_command_signature(self, command):
        return f"```yaml\n{self.clean_prefix}{command.qualified_name} {command.signature}```"

    async def send_cog_help(self, cog):
        em = discord.Embed(
            title=f"**{cog.qualified_name}** Commands", colour=self.color)
        if cog.description:
            em.description = cog.description

        filtered = await self.filter_commands(cog.get_commands(), sort=True)
        for command in filtered:
            em.add_field(name=command.qualified_name,
                         value=command.short_doc or "No Description Provided")

        em.set_footer(text=self.footer())
        await self.get_destination().send(embed=em)

    async def send_command_help(self, command):
        em = discord.Embed(title=command.qualified_name, color=self.color)
        if command.help:
            em.description = command.help

        em.add_field(name="Syntax", value=self.get_command_signature(command))
        em.set_footer(text=self.footer())
        await self.get_destination().send(embed=em)

    async def send_bot_help(self, mapping):
        em = discord.Embed(title="Loofah Help", color=self.color)
        for cog, commands in mapping.items():
            if not cog:
                continue

            filtered = await self.filter_commands(commands, sort=True)
            if filtered:
                value = "\t".join(f"`{i.name}`" for i in filtered)
                em.add_field(name=cog.qualified_name, value=value)
        em.set_footer(text=self.footer())
        await self.get_destination().send(embed=em)
修复点说明
  • 核心报错修复:将send_command_help中get_command_signature的入参从commands模块改为当前方法接收的command实例,匹配方法预期的参数类型。
  • 拼写错误修复:将错误的方法名get_commmands修正为discord.py标准APIget_commands。
  • 格式错误修复:调整cog帮助标题的markdown加粗标记,保证格式正常渲染。
  • 逻辑漏洞修复:全局帮助页命令列表遍历经过权限过滤的filtered集合,仅展示当前用户可调用的命令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.03 10:42:51