Home > Back-end >  discord.py accept - if clause: .MissingRequiredArgument: command is a required argument that is miss
discord.py accept - if clause: .MissingRequiredArgument: command is a required argument that is miss

Time:05-02

I am currently coding a discord bot, which has a command called !tikhelp that displays all tiktok commands, and I tried to include a function where there can get specific info on that command by typing: !tikhelp commandname , but, this blocks the normal !tikhelp, command because it raises an error if the user doesn't include the second argument which should be a command name but not required.

This is my attempt to check for the argument presence but the error gets raised before it can:

@bot.command()
    async def tikhelp(self, ctx, command):
        
        if command:
            if command == "tikinfo"
                await ctx.send("To us this command, type !tikinfo accountname")
                return

        message = """```html
╔══════════════════════════════════════════════════════════════════════╗
    [TikTok Panel]   ║ Prefix: [!] ║  Commands: [0]  ║ version: [1.0]      
║══════════════════════════════════════════════════════════════════════║
║   [!]tikinfo  »» Get someones account information                    ║
║   [!]settik   »» set you TikTok Account Name                         ║
║   [!]mytik    »» Get you TikTok Account information                  ║
║   [!]tikuser  »» Display current Account Name                        ║
║══════════════════════════════════════════════════════════════════════║

╚══════════════════════════════════════════════════════════════════════╝
        ```"""
        
        await ctx.send(message)

Even with the if check, it raises an error, would there be a solution ?

Error:

Traceback (most recent call last):
  File "C:\Users\fkahd\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\fkahd\Downloads\Selfbot\Selfbot\selfbot.py", line 56, in on_command_error
    raise error
  File "C:\Users\fkahd\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\fkahd\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
    await self.prepare(ctx)
  File "C:\Users\fkahd\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 789, in prepare
    await self._parse_arguments(ctx)
  File "C:\Users\fkahd\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 697, in _parse_arguments
    transformed = await self.transform(ctx, param)
  File "C:\Users\fkahd\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 542, in transform
    raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: command is a required argument that is missing.

Thanks for your help

CodePudding user response:

Discord requires the parameter to be set unless you give it a default. You can define the command to default to None, and then check if it is None.

    @bot.command()  # WARNING: It looks like this command is in a cog. If it is, you should be using `@commands.command` and then adding the command by adding the cog.
    async def tikhelp(self, ctx, command: str = None):
        if command is None:
            await ctx.send('Sorry, please specify command')
            return

        # do stuff

CodePudding user response:

Use @bot.group() instead of @bot.command()

@bot.group()
async def tikhelp(self, ctx):
    ...

@tikhelp.command()
async def something(self, ctx):
    ...

to call something: !tikhelp something

to call tikhelp: !tikhelp

  • Related