超时不适用于Wait_For_Message Discord PY

时间:2019-01-25 09:07:02

标签: python python-3.x discord discord.py

wait_for_message的超时不起作用。我什至尝试使用timeout = time.time() + 60*15 elif time.time() > timeout:代替elif guess is None:,但15分钟后它仍然不会超时。我通过放置winner = guess.author来判断while循环是否被打破,它将超时并发送消息并向该通道发送另一个safe_loop。有人有建议吗?

async def safe_loop():
    await client.wait_until_ready()
    channel = client.get_channel("538211206066995230")
    while not client.is_closed:
        x = client.get_all_members()
        for member in x:
            reset_guessesTaken(member, 0)
        answer1 = random.randint(10, 99)
        answer2 = random.randint(10, 99)
        answer3 = random.randint(10, 99)
        answer4 = random.randint(10, 99)
        answer5 = random.randint(10, 99)
        safedollars = random.randint(150, 300)
        safetokens = random.randint(50, 100)
        winner = ''
        await client.send_message(channel, " CRACK THE SAFE \n {}, {}, {}, {}, ?\nWhat is the last number?".format(answer1,  answer2, answer3, answer4))
        print(answer5) 
        def guess_check(m):
            return m.content.isdigit()
        while winner == '':
            guess = await client.wait_for_message(timeout=900, channel=channel, check=guess_check)
            if get_guessesTaken(guess.author) == 5:
                await client.send_message(channel, '{} you have already guessed 5 times! Try again in 15 minutes '.format(guess.author.mention))
            elif int(guess.content) == answer5:
                await client.send_message(channel, '{} cracks the safe and finds ${} and {} tokens inside! \nCheck back in 15 minutes to crack a new safe'.format(guess.author.mention, safedollars, safetokens))
                add_dollars(guess.author, safedollars)
                add_tokens(guess.author, safetokens)
                for member in x:
                    reset_guessesTaken(member, 0)
                winner = guess.author
            elif get_guessesTaken(guess.author) < 5 and int(guess.content) > answer5:
                add_guessesTaken(guess.author, 1)
                await client.send_message(channel, 'That number is too high {}, try again '.format(guess.author.mention))
            elif get_guessesTaken(guess.author) < 5 and int(guess.content) < answer5:
                add_guessesTaken(guess.author, 1)
                await client.send_message(channel, 'That guess is incorrect {}, try again '.format(guess.author.mention))
            else:
                if guess is None:
                    winner = guess.author
                    await client.send_message(channel, 'No one cracked the safe, the number was {}\nCheck back in 15 minutes for a new safe'.format(answer5))
                    for member in x:
                        reset_guessesTaken(member, 0)
        await asyncio.sleep(450)
        await client.send_message(channel, "7 more minutes until a new safe to crack ⏰")
        await asyncio.sleep(450)

client.loop.create_task(safe_loop())

1 个答案:

答案 0 :(得分:2)

如果elif int(guess.content) == answer5:winner = guess.author(如果消息超时,则Client.wait_for_message会返回guess或None >

在检查其他所有内容之前,您必须检查消息是否超时而没有人猜到

    while winner == '':
        guess = await client.wait_for_message(timeout=900, channel=channel, check=guess_check)
        if guess is None:
            #winner = guess.author #guess = None so you're trying to do None.author which doesn't work
            await client.send_message(channel,
                                      'No one cracked the safe, the number was {}\nCheck back in 15 minutes for a new safe'.format(
                                          answer5))
            for member in x:
                reset_guessesTaken(member, 0)

        elif get_guessesTaken(guess.author) == 5:
            await client.send_message(channel, '{} you have already guessed 5 times! Try again in 15 minutes '.format(guess.author.mention))
#etc...