Discord.py: Help about commands and Args

Created on 28 Oct 2017  Â·  10Comments  Â·  Source: Rapptz/discord.py

Hello i'm starting with multiple arguments commands. Here i wan't to check if arg1 and arg2 are not null but i don't know how to do ? Thanks in advance

@client.command(pass_context=True)
async def test(ctx, arg1, arg2):
    if arg1 or arg2 is None:
        client.send_message(ctx.message.channel, "=cw <Carte> <Heure>")
    else:
        desc = 'Bataille sur la carte {0} à {1}'.format(arg1,arg2)
        em = discord.Embed(title='Alerte CW !', description=desc, colour=0x57FE01)
        await client.send_message(client.get_channel("371613656313757699"), embed=em)
question

Most helpful comment

copy/pasting example code and expecting it to work with no changes will set
you up for disappointment :)

On Sat, Oct 28, 2017, 17:45 Nadir Chowdhury notifications@github.com
wrote:

Nevermind, found the issue, there's a missing parenthesis after "=cw
"

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/Rapptz/discord.py/issues/874#issuecomment-340221877,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABeHicFR__l1Aq1CJT5cu1p3SC_o4HFxks5sw6COgaJpZM4QKAkM
.

All 10 comments

Unless you specify default arguments, arg1 and arg2 will never be null - instead a CommandError will be fired before the function is invoked.

You can specify a custom error handler to handle this case. You can either use @test.error decorator, or the on_command_error event handler.

e.g.

@client.command(pass_context=True)
async def test(ctx, arg1, arg2):
    desc = 'Bataille sur la carte {0} à {1}'.format(arg1,arg2)
    em = discord.Embed(title='Alerte CW !', description=desc, colour=0x57FE01)
    await client.send_message(client.get_channel("371613656313757699"), embed=em)

@test.error
async def test_on_error(ctx, error):
    await client.send_message(ctx.message.channel, "=cw <Carte> <Heure>"

Also, for questions like this about how to use the library, asking in the discord api server chat is usually better suited than github issues - https://discord.gg/discord-api

I hate this :
https://i.gyazo.com/f7981ff94c5fe1a398196fab8487f6dd.gif

File "bot.py", line 31
async def on_message(message):

Could you post that as a code block or on pastebin?

Nevermind, found the issue, there's a missing parenthesis after "=cw <Carte> <Heure>"

copy/pasting example code and expecting it to work with no changes will set
you up for disappointment :)

On Sat, Oct 28, 2017, 17:45 Nadir Chowdhury notifications@github.com
wrote:

Nevermind, found the issue, there's a missing parenthesis after "=cw
"

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/Rapptz/discord.py/issues/874#issuecomment-340221877,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABeHicFR__l1Aq1CJT5cu1p3SC_o4HFxks5sw6COgaJpZM4QKAkM
.

Thanks but with his solution nothing is returned in chat when i don't specify arguments :


Task exception was never retrieved
future: <Task finished coro=<test_on_error() done, defined at /usr/local/lib/python3.5/dist-packages/discord/ext/commands/core.py:43> exception=CommandInvokeError("Command raised an exception: AttributeError: 'MissingRequiredArgument' object has no attribute 'message'",)>
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/discord/ext/commands/core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "bot.py", line 28, in test_on_error
    await client.send_message(ctx.message.channel, "=cw <Carte> <Heure>")
AttributeError: 'MissingRequiredArgument' object has no attribute 'message'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "/usr/local/lib/python3.5/dist-packages/discord/ext/commands/core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'MissingRequiredArgument' object has no attribute 'message'

Swap the error and ctx arguments around.

Now working, thanks a lot ! :)

For further issues, I suggest visiting the guild @khazhyk mentioned earlier.

I'm in, thanks :P

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rimmy50 picture Rimmy50  Â·  3Comments

Spyder-exe picture Spyder-exe  Â·  3Comments

superloach picture superloach  Â·  3Comments

j0hnmeow picture j0hnmeow  Â·  3Comments

ghost picture ghost  Â·  3Comments