【问题标题】:Pycord - Roll a Dice Slash CommandPycord - 掷骰子斜线命令
【发布时间】:2022-10-17 23:43:35
【问题描述】:

我正在尝试创建一个滚动命令,您可以在其中选择上限或将其保留为默认值 (6) 这是我尝试过的:

@bot.slash_command(name="roll", description="Rolls a dice", guild_ids=[824342611774144543])
async def roll(ctx, upper_limit: Option(int, required=False)):

    d = random.randint(1, upper_limit)
    dd = random.randint(1, 6)

    if upper_limit == None:
        await ctx.respond(dd)
    else:
        await ctx.respond(d)

当我选择上限时,该命令运行良好,但是当我将其保留为默认值时,它会引发错误

Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py", line 768, in process_application_commands
    await ctx.command.invoke(ctx)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 306, in invoke
    await injected(ctx)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 116, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.commands.errors.ApplicationCommandInvokeError: Application Command raised an exception: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

【问题讨论】:

    标签: python discord bots pycord


    【解决方案1】:

    当您不输入upper_limit 时,它被设置为None,因此您在定义d 变量时尝试获取从1 到None 的随机整数,这会产生您遇到的此错误。

    为避免这种情况,您可以在命令正文中执行以下操作:

    if upper_limit is None:
            dd = random.randint(1, 6)
            await ctx.respond(dd)
    else:
            d = random.randint(1, upper_limit)
            await ctx.respond(d)
    

    这样,它只会在upper_limit 不是None 时执行d = random.randint(1, upper_limit)

    【讨论】:

      猜你喜欢
      • 2022-06-20
      • 2022-06-24
      • 1970-01-01
      • 2022-10-05
      • 2018-09-08
      • 2012-02-29
      • 2020-12-16
      • 2015-06-26
      • 1970-01-01
      相关资源
      最近更新 更多