Discord.Py上的高级帮助命令?

2024-10-16 22:35:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道我是新来的,但我有个问题要问你:

如果您想用Python编写一个带有特殊齿轮的Discord机器人,并用以下命令替换默认的help命令,该怎么办

@commands.command(pass_context=True)
async def help(self,ctx,*cog:str):
    """Gives you info on my cogs *and* their commands."""
    if not cog:
        halp=discord.Embed(title='Cog Listing and Uncatergorized Commands',
                           description='Use `!help *cog*` to find out more about them!')
        cogs_desc = ''
        for x in self.bot.cogs:
            cogs_desc += ('{} - {}'.format(x,self.bot.cogs[x].__doc__)+'\n')
        halp.add_field(name='Cogs',value=cogs_desc[0:len(cogs_desc)-1])
    else:
        halp=discord.Embed(title='{cog} Command Listing')
        for x in self.bot.cogs[cog]:
            halp.add_field(name=x,value=x.__doc__)

    await self.bot.whisper(embed=halp)

…命令会发送机器人cog的信息,但拒绝提供特定cog命令的信息,并提供以下信息:

    Ignoring exception in command help
Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.5/site-packages/discord/ext/commands/core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "/home/pi/Documents/Coding/Python/AwesomeDiscordBot/cogs/info.py", line 49, in help
    for x in self.bot.cogs[cog]:
KeyError: ('Info',)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.5/site-packages/discord/ext/commands/bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "/home/pi/.local/lib/python3.5/site-packages/discord/ext/commands/core.py", line 374, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "/home/pi/.local/lib/python3.5/site-packages/discord/ext/commands/core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: ('Info',)

您将如何修复它,以便可以使用您的Discord机器人!帮助命令获取有关cogs命令的信息

另外,我正在Raspberry Pi上使用Python3.5,我还希望在主帮助嵌入中包含未分类的命令


Tags: inpy命令selfhomebotpihelp