【问题标题】:Discord.py check if input is intDiscord.py 检查输入是否为 int
【发布时间】:2018-04-14 05:53:18
【问题描述】:

我正在尝试使用 discord.py 为我的机器人编写一个抽奖命令,并希望用户可以执行以下命令来开始抽奖:

!raffle time 获胜者标题 EG:!raffle 60 1 Pie

我遇到的问题是创建验证以检查前两个输入是否为数字并且标题是否为空白。目前这是我的命令代码:

@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

但是我没有运气并且收到以下错误:

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.

任何帮助都会很棒,因为我确信这是某个地方的简单错误!

提前致谢

【问题讨论】:

  • time != int 等没有意义。将其包装在 try/except..
  • 你能详细说明一下吗?我用谷歌搜索了 try/except,但对它如何与代码匹配感到困惑。
  • 我在用手机,所以我不能写答案。但是您当前的方法是检查某些内容是否等于内置方法。相反,请使用 try/except 尝试将您的输入转换为 int

标签: python discord.py


【解决方案1】:

您定义的实际上是两个检查。第一个是你要确保你的命令有 3 个参数,第二个是确保前两个是整数。

第一个实际上由 ext.commands 内部处理。要捕获它,您需要定义一个on_command_error 事件方法。

@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

第二个是检查整数,正如@Luke McPuke 所说的那样简单

if not isinstance(time, int)

【讨论】:

  • 谢谢你的回复我想我明白你的意思了。我现在有以下代码以及您的更改,但仍然遇到同样的问题。如果我只放 !raffle ,我会收到控制台错误,如果我放 !raffle 6 ,我会被发送到语句的 else 部分。 CODE
【解决方案2】:

尝试改用isinstance

if not isinstance(time, int)

【讨论】:

  • 我想我不明白 OP 实际上在问什么。似乎他们需要使用 try/except 或断言作为输入,但他们的错误消息使他们看起来除了这个 raffle() 之外没有在函数中包含必要的参数。
  • 我正在寻找的是一种检查 !raffle 后面的三个输入是否有效的方法。例如时间和获胜者是整数,标题不是空白。
  • @KaiJones 那么是的,assert type(time) == int, 'time argument is not an integer' 或使用isinstance 来达到同样的效果。您的标题代码应该可以正常工作。
猜你喜欢
  • 2017-07-19
  • 1970-01-01
  • 2018-05-08
  • 2012-08-17
  • 2021-09-29
  • 2018-05-26
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
相关资源
最近更新 更多