【问题标题】:Timeout not working for Wait_For_Message Discord PY超时不适用于 Wait_For_Message Discord PY
【发布时间】:2019-06-19 02:16:43
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    如果guessNone(如果消息超时,这就是Client.wait_for_message 返回的内容),你不能真正做到elif int(guess.content) == answer5:winner = guess.author

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

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

    【讨论】:

    • 感谢 Tristo 的建议。我也尝试过这种方式,但它似乎仍然没有“超时”并发送消息。超时基于秒对吗?我是否需要中断该 while 循环以使其超时?
    • Yes timeout 以秒为单位,在 900 秒未收到消息后,它将返回 Noneguess。可能发生的事情是因为任务可能会被取消需要很长时间。由于它基于asyncio.wait_for,他们指出为避免取消,您可以尝试将其包装在shield()
    • 它会超时但不会吐出另一个循环,所以我在if guess is None: 下添加了winner= None,它现在可以完美运行。再次感谢您的建议
    • 您是否尝试过将 except 与 ayncio.TimeoutError 一起使用?
    猜你喜欢
    • 2019-02-20
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 2020-12-01
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多