你如何获得 discord.py 命令中使用的参数?

时间:2021-04-27 00:38:55

标签: python discord.py

我正在 discord.py 中为我的机器人制作自定义帮助命令,并且我希望在命令示例中列出参数,就像默认示例一样,如下所示:c!ping [datetime=True]。有没有办法做到这一点?这是我当前的命令:

from typing import Optional

from discord.ext import commands
import discord

from bot.utilities import get_yaml_val

colors = get_yaml_val("config.yml", "colors")["colors"]

class Help(commands.Cog):
    """Cog for the help command."""
    def __init__(self, bot: commands.Bot):
        self.bot = bot

    @commands.command()
    async def help(self, ctx: commands.Context, command: Optional[str] = None):
        """This help command."""
        if not command:
            embed = discord.Embed(
                title="Catbot Help",
                description="A detailed list of all commands",
                color=colors["light_blue"]
            )
            for key, value in self.bot.docs.items():
                embed.add_field(
                    name=key, 
                    value=value
                )
            await ctx.send(embed=embed)
        else:
            command = command.lower()
            command_data = self.bot.docs.get(command)
            if command_data is None:
                await ctx.send(f"`{command}` is an invalid command!")
                return
            embed = discord.Embed(
                title="Catbot help",
                description="Test",
                color=colors["light_blue"]
            )
            embed.add_field(name=command, value=command_data)
            await ctx.send(embed=embed)



def setup(bot: commands.Bot):
    """Loads cog."""
    bot.add_cog(Help(bot))
    docs = {"help": Help.help.help}
    bot.docs.update(docs)
    print(Help.help.args())

我为每个齿轮更新文档。

1 个答案:

答案 0 :(得分:0)

discord.py 具有出色的帮助命令支持。我建议您在创建自己的帮助命令之前先查看 this

# you are using self.bot.docs to get command, I assume thats your own implementation.
command = self.bot.get_command(command)
if command is None:
   print('invalid command')
else:
  command_string = f" {command.qualified_name} {command.signature}"

command.qualified_name 返回整个命令(如果有父命令) command.signature 返回参数、其类型和默认值(如果有)。

参考:

注意: 我认为 HelpCommand 上没有很多有用的教程,如果你想实现它,你可以看看这些公共机器人如何实现它。

RoboDanny--> 非常复杂的帮助命令

TechWithTim