生成一个随机数

时间:2017-11-08 16:13:46

标签: python random discord.py

我正在尝试通过pip discord.py建立discord bot。在命令!random之后我想问用户任何号码,然后机器人将生成0和输入数字之间的随机数。它对我来说并不合适,因为我对discord.py一无所知,我问那里:)

代码:

import random
elif message.content.startswith("!random"):
    await client.send_message(message.channel, "Enter a number: ")
    num = await client.wait_for_message(int)
    numm = int(num)
    randomnum = random.randint(0, numm)
    await client.send_message(message.channel, randomnum)

1 个答案:

答案 0 :(得分:0)

试试这个:

import random
elif message.content.startswith("!random"):
    await client.send_message(message.channel, "Enter a number: ")
    def check(msg):
        return msg.content
    reaction.content = await client.wait_for_message(author=message.author, check=check)
    numm = int(reaction)
    randomnum = random.randint(0, numm)
    await client.send_message(message.channel, randomnum)

您还应该检查反应是否为int

(添加了squaswin的反应。请注意,代码未经过测试)