如何在不触发discord.ext.commands.errors.MissingRequiredArgument的情况下制作子命令?

时间:2021-07-23 08:40:21

标签: python discord discord.py

(顺便说一下,这是我的第一篇文章<3) 我正在制作一个 discord.py 机器人,我想要一个命令,它可以为用户列出警告(警告列表),为用户清除警告(警告清除)或为命令提供帮助消息(任何不符合其他两个命令)! 代码如下:

    @commands.command(name="warnings")
    async def warnings(self, ctx, usage, user):
        if usage == "clear":
            f = open(user + ".txt", "w")
            f.write("")
            f.close()
            embed = discord.Embed(title="Warnings removed", description=f"I have removed all warnings for <@!{user}>", colour=discord.Colour.green())
            await ctx.channel.send(embed=embed)
        elif usage == "list":
            try:
                f = open(user + ".txt", "r")
                readResult = f.read()
                if readResult.startswith("Reason: "):
                    embed = discord.Embed(title="Warnings for: <@!" + user + ">", description=readResult, colour=discord.Colour.red())
                    await ctx.channel.send(embed=embed)
                else:
                    embed = discord.Embed(title="This user has no warnings", colour=discord.Colour.green())
                    await ctx.channel.send(embed=embed)
            except:
                embed = discord.Embed(title="This user has no warnings", colour=discord.Colour.green())
                await ctx.channel.send(embed=embed)

我尝试了 if 语句并使用 @warnings.error 但它没有用,我也用同样的错误消息搜索了前面的问题,但它们并不完全相同! :( 顺便说一下,代码本身不是问题:)

我的期望:

!警告

>[Something help info.]

我得到了什么:

raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: usage is a required argument that is missing.

1 个答案:

答案 0 :(得分:0)

使用param=None做一个子命令,也许你可以搜索一下如何自定义help,对你有帮助。

@commands.command(name="warnings")
    async def warnings(self, ctx, usage=None, user=None):
        
    if usage is None:
        pass   # do something here
    elif usage == "clear":
            f = open(user + ".txt", "w")
    ...