【问题标题】:Discord.py Rewrite gathering list of all commandsDiscord.py 重写所有命令的收集列表
【发布时间】:2019-05-23 14:19:52
【问题描述】:

我正在尝试获取我的 Discord 机器人中所有命令的列表以进行重写。我正在使用 Python 3.6 编写此代码

我尝试通过执行打印命令列表 print(bot.commands) 这只为我提供了以下回报:

{<discord.ext.commands.core.Command object at 0x00000209EE6AD4E0>, <discord.ext.commands.core.Command object at 0x00000209EE6AD470>}

我希望通常的输出是clear(),因为这是迄今为止我在机器人中编写的唯一命令,该命令按预期工作。但它只打印上面的内容

【问题讨论】:

    标签: python discord.py discord.py-rewrite


    【解决方案1】:

    我想你正在寻找这样的东西。

    @bot.command(name="help", description="Returns all commands available")
    async def help(ctx):
        helptext = "```"
        for command in self.bot.commands:
            helptext+=f"{command}\n"
        helptext+="```"
        await ctx.send(helptext)
    

    【讨论】:

    • 为什么@bot.commandname="help", description="Returns all commands available"。这对代码是否重要,是它如何获取所有命令的列表吗?
    • 名称 perimeter 是用户将输入的内容,例如 !help,如果您选择这样做,可以使用命令返回描述,这样更容易。
    • 如何从特定的 cog 获取命令?
    • 名称是help,但命令名称是help?你为什么要发name="help"
    【解决方案2】:
    @commands.command(help = "Blah")
    async def l1(self,ctx):
        commands = [c.name for c in self.client.commands]
        print(commands)
    

    你得到的是命令对象,所以如果你想要命令的名称,你可以得到名称为Command.name

    参考:https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Command.name

    【讨论】:

      【解决方案3】:

      这些是您的机器人拥有的 Command 对象。之所以有两个,是因为您的机器人有一个内置的 help 命令,可以完全满足您的需求。如果您的 clear 命令是

      @bot.command()
      async def clear(ctx):
          "A command for clearing stuff"
          pass
      

      然后运行 ​​!help 会给你输出

      没有分类: help 显示此消息。 clear 清除东西的命令 键入 !help 命令以获取有关命令的更多信息。 您还可以键入 !help category 以获取有关类别的更多信息。

      【讨论】:

        【解决方案4】:

        我真的很晚了,但我想分享这个。

        添加到 akiva 和 damaredayo 的响应中,这是一个基本命令,它将获取 cogs 主文件中的所有命令。

        @bot.command()
        async def commandcount(ctx):
          counter = 0
          for command in bot.commands:
            counter += 1
          await ctx.send(f"There are `{counter}` commands!")
        

        【讨论】:

        • 虽然有效,但不幸的是,它不能回答问题。
        猜你喜欢
        • 1970-01-01
        • 2020-10-19
        • 2021-07-15
        • 2020-05-03
        • 2020-11-06
        • 2021-03-30
        • 2020-09-11
        • 1970-01-01
        • 2019-07-10
        相关资源
        最近更新 更多