Discord.py: MissingRequiredArgument

Created on 15 Feb 2017  路  6Comments  路  Source: Rapptz/discord.py

When you're missing an argument for a command, instead of calling 'except Exception:', you drop the entire code and display an error that does not return a message into chat.

Example

@bot.command()
async def test(myvar : str):
    'Test command for exceptions'
    try:
        await bot.say(myvar)
    except Exception:
        await bot.say('Proper usage: test string')
        return

Instead, console outputs:

Ignoring exception in command test
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/discord/ext/commands/bot.py", line 848, in process_commands
yield from command.invoke(ctx)
File "/usr/local/lib/python3.5/dist-packages/discord/ext/commands/core.py", line 362, in invoke
yield from self.prepare(ctx)
File "/usr/local/lib/python3.5/dist-packages/discord/ext/commands/core.py", line 340, in prepare
yield from self._parse_arguments(ctx)
File "/usr/local/lib/python3.5/dist-packages/discord/ext/commands/core.py", line 299, in _parse_arguments
transformed = yield from self.transform(ctx, param)
File "/usr/local/lib/python3.5/dist-packages/discord/ext/commands/core.py", line 207, in transform
raise MissingRequiredArgument('{0.name} is a required argument that is missing.'.format(param))
discord.ext.commands.errors.MissingRequiredArgument: myvar is a required argument that is missing.

Most helpful comment

How do you expect lib to pass a variable into your function if there is no argument provided? Use an error handler and check the error type instead.

A "generic" error handler would be like

@bot.event
async def on_command_error(error, ctx):
    # you can compare error here

A "per-command" error handler would be like

@test.error
async def test_error(error, ctx): 
    ...

Here's the code for error dispatching. And here's the code for "per-command" error handler decorator. And you can find types of errors here.

All 6 comments

The error is correct, an argument is missing, you should catch the error using the event on_command_error to return a message

How do you expect lib to pass a variable into your function if there is no argument provided? Use an error handler and check the error type instead.

A "generic" error handler would be like

@bot.event
async def on_command_error(error, ctx):
    # you can compare error here

A "per-command" error handler would be like

@test.error
async def test_error(error, ctx): 
    ...

Here's the code for error dispatching. And here's the code for "per-command" error handler decorator. And you can find types of errors here.

My understanding is that if it couldn't fulfill try then it would process except, but I've been coding in Python for a whole day - so forgive my lack of understanding.
I did search and find the bot.py, but really couldn't understand it very well - I even went as far as adding 'except on_command_error', but apparently I did not make it far enough.

Thanks for the code snippit, Helehelehele.

I apologize for opening an issue.

Could someone provide an example of the original code of using the on_command_error function?

Can you join the discord.py server to talk about this issue more? You seem to be talking on an issue more than 6 months old.

Can You Guys Explain More Further I mean The Code Discplay

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marcoshuck picture marcoshuck  路  16Comments

Chicchi73930 picture Chicchi73930  路  17Comments

haaddaa1 picture haaddaa1  路  14Comments

ams2990 picture ams2990  路  14Comments

imayhaveborkedit picture imayhaveborkedit  路  58Comments