1

I've been trying to code a discord bot that checks for use inputs and commands, with arguments, but I would like to check whether it's a string or a number. Let me explain clearly :

In my code :

@client.command()
async def create(ctx, *args): # create a lobby
    firstarg = (int)(args[0])
    if (firstarg >= 4 and firstarg <= 10):
        await ctx.send("Max players : " + args[0])
        return (0)
    else:
        await ctx.send("Error. Please enter a maximum number of players between 4 and 10.")
        return (84)

The arguments are always strings, so I thought I could just cast them as integers and it works. But, there is one catch.

When I'm using anything else than numbers in my command, not any error message will show up, because it couldn't cast it into an integer.

So, I would like to know if there is a way to check for this specific error and if it's the same for other kinds of errors that I would encounter in the future.

I'm not sure about how to "check whether it's a string, and if it's not cast it as an int and use it in the rest of the code".

It might not be 100% clear, so if something is not clear please tell me and I'll try to clarify as good as I can.

Thanks.

pironc
  • 51
  • 1
  • 9
  • Does this answer your question? [Can't catch ValueError in Python](https://stackoverflow.com/questions/58354826/cant-catch-valueerror-in-python) – buran Sep 23 '20 at 13:57

1 Answers1

1

I think you need to look at how exceptions works in Python and how to handle them

In your case, you need to catch the ValueError, which would give something like that :

@client.command()
async def create(ctx, *args): # create a lobby
    try:
      firstarg = (int)(args[0])
    except ValueError:
      # the string was not a number
      await ctx.send("Error. Please enter a number.")
      return (84)
    else:
      # the string was a number
      if (firstarg >= 4 and firstarg <= 10):
        await ctx.send("Max players : " + args[0])
        return (0)
      else:
        await ctx.send("Error. Please enter a maximum number of players between 4 and 10.")
        return (84)
Viper
  • 405
  • 3
  • 11
  • Thanks for your answer. Actually I fixed it really easily by using the "isdigit()" function to check whether my first argument string had numbers or not, but your answer is still really useful as I didn't really know how to manage exceptions like that. I was also wondering, if the try works, does it still go for the else statement? As I see it it looks like it will try the first statement, and if it fails it would do something "else". – pironc Sep 23 '20 at 14:08
  • oh alright my bad, I think the else is just a condition if the ValueError was not caught. Thanks a lot! – pironc Sep 23 '20 at 14:10
  • yes, the else is what happened if the try statement did not raise any exception. isdigit() is a good idea but to be perfectly accurate you should use isdecimal() because, according to documentation, isdigit "covers digits which cannot be used to form numbers in base 10" – Viper Sep 23 '20 at 14:14
  • Wouldn't isdecimal be worse than digit? As I only need integers and not floats (4 to 10, not anything else). So whatever the string, if it's not from 4 to 10, only integers, it would just skip it. – pironc Sep 23 '20 at 14:22
  • It's a bit confusing but actually no. If you read [isdigit documentation](https://docs.python.org/3/library/stdtypes.html#str.isdigit) it states : "Digits include decimal characters and digits that need special handling". and neither of those two functions works with float – Viper Sep 23 '20 at 14:37