MysteriousK
MysteriousK

Reputation: 99

How to handle `MissingRequiredArgument` errors

I was making a bot in the rewrite branch of discord.py and lets say this is my code:

@bot.command()
async def ok(ctx,con):
    try:
        await ctx.send(con)
    except commands.MissingRequiredArgument:
        await ctx.send('You did not give me anything to repeat!')

What I am trying to do is handle the MissingRequiredArgument error but the code i wrote still gives the error instead of returning You did not give me anything to repeat! I would appreciate it if someone would tell me how to handle it. exact error:

Ignoring exception in command translate:
Traceback (most recent call last):
  File "C:\Users\jatinder\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\jatinder\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 790, in invoke
    await self.prepare(ctx)
  File "C:\Users\jatinder\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 751, in prepare
    await self._parse_arguments(ctx)
  File "C:\Users\jatinder\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 679, in _parse_arguments
    kwargs[name] = await self.transform(ctx, param)
  File "C:\Users\jatinder\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 516, in transform
    raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: con is a required argument that is missing.

Upvotes: 0

Views: 2503

Answers (4)

Cootshk
Cootshk

Reputation: 25

use an error catcher (checks for missing required argument error)

@bot.command()
async def ok(ctx,con):
   await ctx.send(con)
@ok.error
async def okerror(ctx,error):
   if isinstance(error, commands.MissingRequiredArgument):
       await ctx.send("i need something to say")
   else:
      await ctx.send("The error that occured was"+str(error))

Upvotes: 0

StarbuckBarista
StarbuckBarista

Reputation: 1318

The best way to do this would be to use the error event.

@ok.error
async def on_command_error(error, ctx):
    await ctx.send(“You did not give me anything to repeat!”)

Please note the @ok.error is intentional as it allows it to only function for that command as to not interfere with ay other commands you have.

Upvotes: 0

Ecks Dee
Ecks Dee

Reputation: 471

This error cannot be handled by a try/except, I'm not sure why but I have two ways you can handle this.

First method:


@bot.command()
async def ok(ctx, con=None):
    if con == None: return await ctx.send('You did not give me anything to repeat!')
    # Do whatever here, if you come here it means the user gave something to repeat

'con' is set to None by default (con=None). If a user doesn't give anything it'll stay None. But if a user gives something it'll be what (s)he gave. The if-statement will detect if 'con' equals to None, if it does then it means the user returned nothing.

Second Method


@bot.command()
async def ok(ctx, con):
    # Do whatever here, if you come here it means the user gave something to repeat

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        print('You did not give me anything to repeat!')

I used @bot.event here meaning the error will be handled for all commands that don't have an argument (In other words commands that get the MissingRequiredArgument error) If you want to work for only the 'ok' command, used @ok.error instead of @bot.event.

Upvotes: 1

user12309578
user12309578

Reputation:

I'm not sure that the other answer will work but this will

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, MissingRequiredArgument):
        await ctx.send("A parameter is missing") 

Upvotes: 0

Related Questions