简体   繁体   中英

Discord.py check if input is int

I am trying to write a raffle command for my bot using discord.py and want it so the user can do the following command to start a raffle:

!raffle time winners title EG: !raffle 60 1 Pie

The issue I am having is creating validation to check that the first two inputs are numbers and that the title isn't blank. Currently this is the code I have for the command:

@bot.command(pass_context=True)
async def raffle(ctx, time, winners, title):    

    if time != int or winners != int or title != "":
        await bot.say("{} raffle has been started for {} seconds and there will be {} winner(s)!".format(title, time, winners))
    else:
        await bot.say("Seems you went wrong! Raffle format is: !raffle time winners title")
        return

However I am having no luck and am getting the following error:

Ignoring exception in command raffle
Traceback (most recent call last):
  File "C:\Users\kairj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "C:\Users\kairj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 367, in invoke
    yield from self.prepare(ctx)
  File "C:\Users\kairj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 345, in prepare
    yield from self._parse_arguments(ctx)
  File "C:\Users\kairj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 304, in _parse_arguments
    transformed = yield from self.transform(ctx, param)
  File "C:\Users\kairj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 212, in transform
    raise MissingRequiredArgument('{0.name} is a required argument that is missing.'.format(param))
discord.ext.commands.errors.MissingRequiredArgument: time is a required argument that is missing.

Any help would be great as I am sure its a simple mistake somewhere!

Thanks in advance

尝试使用isinstance代替:

if not isinstance(time, int)

What you are defining is actually two checks. The first is that you want to ensure there are 3 arguments to your command, and the second is to ensure that the first two are ints.

The first one is actually handled by ext.commands internally. To catch it, you will need to define an on_command_error event method.

@bot.event
def on_command_error(exc, ctx):
    if isinstance(exc, commands.errors.MissingRequiredArgument):
        # Send a message here
        return

    # If nothing is caught, reraise the error so that it goes to console.
    raise exc

The second is checking the ints, which as @Luke McPuke said is simply

if not isinstance(time, int)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM