Discord.py中的随机数生成器

时间:2020-06-08 13:38:42

标签: python random discord.py

@client.command()
async def givenum(ctx):
    await ctx.send("Type a number")
    num1 = ctx.wait_for_message(author=ctx.author)
    await ctx.send("Type a second, larger number")
    num2 = ctx.wait_for_message(author=ctx.author)
    try:
        numb1 = int(num1)
        numb2 = int(num2)
        if numb1 < numb2:
            value = random.randint(num1,num2)
            await ctx.send(f"You got {value}.")
        else:
            await ctx.send(":warning: Please ensure the first number is smaller than the second number.")
    except:
        await ctx.send(":warning: Please use positive integers")

这是我们discord.py机器人的随机数生成器。当我们运行此代码时,会说“输入数字”,但是当我们输入数字时,它不会发送任何内容。它甚至没有给出错误。我们试图使其在非前缀时响应

2 个答案:

答案 0 :(得分:1)

您似乎正在使用异步(v0.16.x)中的旧文档,请从重写文档中尝试以下操作:

@client.command()
async def givenum(ctx):

    # checks the author is responding in the same channel
    # and the message is able to be converted to a positive int
    def check(msg):
        return msg.author == ctx.author and msg.content.isdigit() and \
               msg.channel == ctx.channel

    await ctx.send("Type a number")
    msg1 = await client.wait_for("message", check=check)
    await ctx.send("Type a second, larger number")
    msg2 = await client.wait_for("message", check=check)
    x = int(msg1.content)
    y = int(msg2.content)
    if x < y:
        value = random.randint(x,y)
        await ctx.send(f"You got {value}.")
    else:
        await ctx.send(":warning: Please ensure the first number is smaller than the second number.")

参考:

答案 1 :(得分:0)

您必须在awaitctx.wait_for_message()。我还自由地进行了修复,因此该机器人可以对这两个数字进行排序,而用户不必弄清楚哪个较小。

@client.command()
async def givenum(ctx):
    await ctx.send("Type a number")
    num1 = await ctx.wait_for_message(author=ctx.author)
    await ctx.send("Type a second number")
    num2 = await ctx.wait_for_message(author=ctx.author)
    try:
        numb = int(num1)
        numb = int(num2)
        value = random.randint(min(num1, num2), max(num1, num2))
        await ctx.send(f"You got {value}.")
    except:
        await ctx.send(":warning: Please use positive integers")