0

Here you have the code I made for the command :

#?dodaj
@client.command()
async def dodaj(ctx, a: int, b: int):
    if (a == int and b == int):
        await ctx.send(a + b)
    else:
        await ctx.send("Błąd : Nie mogłem obliczyc tego działania!")

I want to know what's wrong with this, because it won't work properly.

Here's the error i'm getting by typing "?dodaj coś jeszcze" :

Ignoring exception in command dodaj:
Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 370, in _actual_conversion
    return converter(argument)
ValueError: invalid literal for int() with base 10: 'coś'

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

Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\bot.py", line 863, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 721, in invoke
    await self.prepare(ctx)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 685, in prepare
    await self._parse_arguments(ctx)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 599, in _parse_arguments
    transformed = await self.transform(ctx, param)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 455, in transform
    return await self.do_conversion(ctx, converter, argument, param)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 408, in do_conversion
    return await self._actual_conversion(ctx, converter, argument, param)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 379, in _actual_conversion
    raise BadArgument('Converting to "{}" failed for parameter "{}".'.format(name, param.name)) from exc
discord.ext.commands.errors.BadArgument: Converting to "int" failed for parameter "a".

Also when I type "?dodaj 20 20" it should add the 20's but it sends the error message that can be seen in the else parameter, in the code.

Pomi 3
  • 17
  • 3
  • Welcome to StackOverflow. When you say "it won't work properly," could you give us the exact error? And if you could provide a comment for the last await command as to what error is being handled. It would help the English-speaking members of this formum. – rajah9 Nov 04 '19 at 13:40

1 Answers1

0
a == int and b == int

Here lies the problem. a is not equal to int. int is a class. a is an instance of class int.

This means you need to check for the type of a, or better - if a is instance of int (it can be any class inheriting from int then):

isinstance(a, int) and isinstance(b, int)
h4z3
  • 5,265
  • 1
  • 15
  • 29
  • it helps with the error message when I type the "?dodaj 20 20" command but when I type "?dodaj coś jeszcze" I get the same error as in the post – Pomi 3 Nov 04 '19 at 13:48
  • @Pomi3 It breaks earlier, not in this code. I haven't used discord.py myself, but it looks like it tries to convert those values for you somewhere earlier. Try looking where you set your parameters to be ints. Maybe the lib is looking at the annotations in the parameter list? – h4z3 Nov 04 '19 at 13:57