【问题标题】:Error in Discord bot: Bot has no attribute '_default_help_command'Discord bot 中的错误:Bot 没有属性“_default_help_command”
【发布时间】:2020-12-23 17:47:35
【问题描述】:

我有一个 Discord 机器人,我试图将默认帮助命令的帮助与其他命令的帮助归入同一类别。当我什么都不做时,默认的帮助命令会显示:

ChatNote:
    note Adds a note to a notebook ​    
No Category:
    help Shows this message

然后我根据另一个 SO answer 尝试这段代码:

bot = commands.Bot(command_prefix="##", case_insensitive=True, name="ChatNote")

class ChatNoteCommands(Cog, name="ChatNote"):
    @commands.command(
        help="Adds a new note to your current notebook, or the specified notebook",
        brief="Adds a note to a notebook",
        pass_context=True
    )
    async def note(self, ctx, *, text):
        note_text = text.strip()
        await ctx.channel.send("Noted: " + note_text)

    @commands.command(pass_context=True)
    async def help(self, ctx, *args):
        return await commands.bot._default_help_command(ctx, *args)

bot.remove_command("help")
bot.add_cog(ChatNoteCommands())

bot.run(DISCORD_TOKEN)

但是这段代码给了我错误:

Command raised an exception: AttributeError: module 'discord.ext.commands.bot' has no attribute '_default_help_command'

如果我将 _default_help_command 换成自动完成提供给我的方法 DefaultHelpCommand,我会收到一个新错误:

Command raised an exception: TypeError: can't pickle _asyncio.Future objects

错误的含义很明显,但我应该使用什么方法名,或者,一般来说,我的代码有什么问题?

【问题讨论】:

  • 您是否正在尝试获取命令帮助,例如命令 ##help note 以使机器人回复“note”命令描述?还是您想返回显示所有命令的默认帮助菜单?
  • @LoremI。我正在尝试显示所有命令的默认帮助菜单,但 helpnote 在同一类别中,而不是在它自己的 No Category

标签: python python-3.x discord bots discord.py


【解决方案1】:

您可以使用bot.help_command.cog 设置help_command 的cog

from discord.ext import commands

bot = commands.Bot(command_prefix="##", case_insensitive=True, name="ChatNote")

class ChatNoteCommands(commands.Cog, name="ChatNote"):
    @commands.command(
        help="Adds a new note to your current notebook, or the specified notebook",
        brief="Adds a note to a notebook",
        pass_context=True
    )
    async def note(self, ctx, *, text):
        note_text = text.strip()
        await ctx.channel.send("Noted: " + note_text)

bot.add_cog(ChatNoteCommands())
bot.help_command.cog = bot.cogs["ChatNote"]

bot.run(TOKEN)

【讨论】:

  • 谢谢,非常接近,但我现在得到##help 的以下输出:ChatNote note Adds a note to a notebook ​ChatNote: help Shows this message 也就是说,我有两个相同的类别,no_category 似乎有自己的, 重复类别。
  • 我更新了代码,将帮助命令的 cog 设置为创建的那个,这应该会产生所需的结果。
猜你喜欢
  • 2020-10-22
  • 1970-01-01
  • 2022-12-09
  • 2020-11-11
  • 2018-06-15
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多