【问题标题】:How to assign a sub-help for a command discord.py?如何为命令 discord.py 分配子帮助?
【发布时间】:2021-12-10 07:17:48
【问题描述】:

如何为 discord.py 中的命令分配子帮助?

class xkcd(commands.Cog):
  def __init__(self, bot):
    self.bot = bot

  @commands.command()
  async def hi(self, ctx):
    await ctx.send('hey')

当我键入 时,它应该会显示有关该命令的更多信息。 我该怎么做?

【问题讨论】:

  • 检查this。答案中有一条评论应该可以帮助您。
  • 我无法理解您所说的“子帮助”是什么意思。您的意思是要使用默认的 discord.py help 命令实现为 hi 命令添加帮助消息?
  • @Taku by sub-help,我的意思是如果你在 help 前加上前缀,它应该会显示有关该命令的更多信息。

标签: python discord discord.py bots


【解决方案1】:

有几个选项取决于您要显示的内容和显示位置,但我会给出重要的选项。 briefdescription 允许您指定命令的简短描述和完整描述。它们是@client.command() 的参数,将被放置在@client.command(HERE)。在你的情况下,那将是@commands.command(HERE)

补充:正如Taku 提到的,help 参数也将 让你做你想做的事。 @commands.command(help="") 让你 定义帮助消息的长文本,这实际上可以 briefdescription 一次。如果您不需要,请使用它 区分这两种描述。

brief 将在不提供参数的情况下运行!help 命令时为您提供简要说明。此处显示的机器人的输出:

​No Category:
  help  Shows this message
  hi    This is where the BRIEF would be found.

Type !help command for more info on a command.
You can also type !help category for more info on a category.

description 将在命令作为参数传递给默认帮助命令时设置消息,在您的情况下为:!help hi。机器人的输出将显示为:

This is where the DESCRIPTION is found.

!hi

因此,总而言之,您的新代码将如下所示:

class xkcd(commands.Cog):
def __init__(self, bot):
  self.bot = bot

@commands.command(brief="This is where the BRIEF would be found.", description="This is where the DESCRIPTION is found.")
async def hi(self, ctx):
  await ctx.send('hey')

只需将字符串更改为每个命令所需的字符串。

我还建议阅读有关此问题的文档,找到 here。这将遍历 command() 函数的所有参数,并且很高兴知道您拥有的所有选项。

编辑:看到评论澄清了子帮助的意思

【讨论】:

  • 帮助消息的主要“正文”带有help 参数或作为函数的文档字符串提供。 description 只是帮助信息的第一行。也许你应该提到最简单的 doc-string 方法和 help 参数。
  • @tkomasa 但这是针对不和谐的默认帮助命令。我已经制作了自定义帮助命令并禁用了默认帮助命令。是否有另一种方法可以通过前缀帮助 来为命令提供描述?
  • @MockingJay 不幸的是,您应该提供一个不同的问题和重要信息。我建议提出一个新问题,其中包含更多示例和更清晰的说明您需要什么。在那里也提供自定义帮助命令的当前代码。
【解决方案2】:

尝试创建另一个名为 help_hi 的命令,然后在别名中将其称为 help hi,这样它就会在没有 _ 的情况下调用它

例子:

  @commands.command()
  async def hi(self, ctx):
    await ctx.send('hey')

  @commands.command(aliases = ["help hi"])
  async def help_hi(ctx):
    await ctx.send("Using `!hi` will make the bot respond with `hey`")

或者你可以像这样暗示帮助部分:

  @commands.command(help = "Want <bot_name> to say hi? Use `!hi`")
  async def hi(self, ctx):
    await ctx.send('hey')

所以现在当你运行命令 !help 时,默认的帮助嵌入不和谐应用会显示出来!

如果这不能回答您的问题,请告诉我!

【讨论】:

    【解决方案3】:

    试试这个:

    class xkcd(commands.Cog):
      def __init__(self, bot):
        self.bot = bot
    
      @commands.command()
      async def hi(self, ctx):
        """
        This is the description for my very cool command.
        Have a very good day!
        """
        await ctx.send('hey')
    

    Docstrings 用于向函数添加文档,它们在 discord.py 中用于!help &lt;command&gt;

    文档字符串的第一行将用作!help 中的1liner,因此在这种情况下它将被列为:

    hi    This is the description for my very cool command.
    

    !help hi 将显示:

    !hi 
    
    This is the description for my very cool command.
    Have a very good day!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-06
      • 2021-09-16
      • 2021-04-21
      • 2021-06-13
      • 2021-01-01
      • 2019-03-17
      • 2020-10-19
      • 2019-07-16
      相关资源
      最近更新 更多