Skip to content
Advertisement

How to make a sub command without trigger discord.ext.commands.errors.MissingRequiredArgument?

(This is my first post by the way <3) I am making a discord.py bot and I want a command that will either list warnings (warnings list ) for a user, clear warnings for a user (warnings clear ) or give a help message for the command (anything that does not fit the other two commands)! Here is the code:

    @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)

I have tried an if statement and using @warnings.error but it has not worked, I have also searched the previous questions with this same error message but they are not quite the same! :( By the way the code itself isnt the problem :)

What I expect:

!warnings

>[Something help info.]

What I get:

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

Advertisement

Answer

use param=None to do a sub command, maybe you can search how to make a custom help, that’ll help you.

@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")
    ...
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement