简体   繁体   English

超时不适用于 Wait_For_Message Discord PY

[英]Timeout not working for Wait_For_Message Discord PY

The timeout for wait_for_message is not working. wait_for_message 的超时不起作用。 I even tried to use timeout = time.time() + 60*15 elif time.time() > timeout: instead of elif guess is None: and it still won't time out after 15 minutes.我什至尝试使用timeout = time.time() + 60*15 elif time.time() > timeout:而不是elif guess is None:并且它仍然不会在 15 分钟后超时。 I figured if the while loop is broken by putting winner = guess.author that it would timeout and send the message and send another safe_loop to the channel.我想如果 while 循环被设置winner = guess.author将超时并发送消息并将另一个safe_loop发送到通道。 Does anyone have a suggestion?有人有建议吗?

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())

You can't really do elif int(guess.content) == answer5: or winner = guess.author if guess is None (which is what Client.wait_for_message returns if the message times out你真的不能做elif int(guess.content) == answer5:或者winner = guess.author如果guessNone (如果消息超时,这是Client.wait_for_message返回的内容

You have to check if the message timed out without anyone guessing before you check everything else在检查其他所有内容之前,您必须检查消息是否超时而没有任何人猜测

    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...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM