Search code examples
url-rewritingintdiscord.py

How do you check if a string is convertible to an int, and if so, convert it? (Python, involves discord.py rewrite)


I'm currently making a discord bot in Python. I want to make a command where you have two options for an argument:

  • An integer to delete a certain amount of messages
  • And the word 'all' (a string) to delete all the messages in the channel.

I've tried functions like this to determine if it is a string or not...

def isint(s):
try:
    int(s)
    isint = True
except ValueError:
    isint = False
return isint

... but it returns this error:

TypeError: '<=' not supported between instances of 'str' and 'int'

This is the current, most recent code for the command that I have tried:

    @commands.command()
@commands.has_role("Clear Chat Key")
async def clear(self, ctx, amount: typing.Union[str, int] = 10):
    number = isint(amount)
    if number == False:
        if amount == 'all':
            await ctx.channel.purge(limit = inf)
        else:
            await ctx.send('Invalid amount.')
    if number == True:
        await ctx.channel.purge(limit = amount)
    else:
        await ctx.send('Invalid amount.')

The full traceback for the error, in case it is relevant, is the following:

Traceback (most recent call last):
  File "C:\Users\44794\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\44794\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\44794\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: '<=' not supported between instances of 'str' and 'int'

Please bear with me here, I am very new to Python.

EDIT: Thanks for helping me with this! Here is the code I used in case anyone else needs it:

def is_int(x):
if x.isdigit():
    return True
return False

@commands.command()
@commands.has_role("Clear Chat Key")
async def clear(self, ctx, amount: typing.Union[str, int] = 10):
    number = is_int(amount)
    if number == False:
        if amount == 'all':
            await ctx.channel.purge(limit = inf)
        else:
            await ctx.send('Invalid amount.')
    if number == True:
        amount = int(amount)
        await ctx.channel.purge(limit = amount)
    else:
        await ctx.send('Invalid amount.')

Again, thanks for helping me out!


Solution

  • You're able to use the string's .isdigit() method.

    Bear in mind that this will return False for negatives:

    >>> my_str = "123"
    >>> my_str.isdigit()
    True
    >>> other_str = "-123"
    >>> my_str.isdigit()
    False
    

    Working example:

    def is_int(some_value):
        if some_input.isdigit():
            return True
        return False
    
    some_input = input("Enter some numbers or words!\n-> ")
    print(is_int(some_input))
    

    Reference: