首页 > 解决方案 > 一旦我的不和谐机器人到达“等待 ctx.wait_for_message(author=author, timeout=20)”,我的 python 代码就会冻结

问题描述

我已经尝试调试这个很久了,它可能只是我没有看到的非常明显的东西,但有人知道这里发生了什么。我正在尝试创建一个可以玩二十一点的不和谐机器人,并且我试图在服务器中向用户询问一个问题,阅读它并做出回应。其他一切在机器人上都可以正常工作,但这只是这一部分,这是这个特定部分的代码,我没有收到任何错误,它只是到达那一行,然后什么也不做,但我仍然可以调用其他命令. 提前致谢:)

@bot.command(pass_context=True)
async def blackjack(ctx, bet):
    sender = (str(ctx.message.author.name))
    author = ctx.message.author
    rwallet = open(sender + ".txt", "r")
    wallet = rwallet.read()
    global dealer_cards
    dealer_cards = []
    global player_cards
    player_cards = []
    
    # Deal the cards
    # Display the cards
    # Dealer Cards
    # Player Cards
    while len(dealer_cards) != 2:
        dealer_cards.append(random.randint(1, 11))
        player_cards.append(random.randint(1, 11))
        if len(dealer_cards) == 2:
            if len(player_cards) == 2:
                print("test")
                await ctx.send("Dealer has ? & " +  str(dealer_cards[1]) + " You have " + str(player_cards))
                # Sum of the Dealer cards
                if sum(dealer_cards) == 21:
                        await ctx.send("Dealer has 21 and wins!")
                elif sum(dealer_cards) > 21:
                        await ctx.send("Dealer has busted!")
                    # Sum of the Player cards
                while sum(player_cards) < 21:
                    print("test")
                    await ctx.send(str("Do you want to stay or hit? "))
                    print("test")
                    response = await ctx.wait_for_message(author=author, timeout=20,)
                    if response.content.lower() == "hit":
                        player_cards.append(random.randint(1, 11))
                        print("test")
                        await ctx.send("You now have a total of " + str(sum(player_cards)) + " from these cards ", player_cards)```

标签: pythondiscord.py

解决方案


上下文对象中没有 wait_for_message 函数。

https://discordpy.readthedocs.io/en/latest/migrating.html#waiting-for-events

在 v1.0 之前,等待事件本身之外的事件的机制是通过两个不同的函数完成的,Client.wait_for_message 和 Client.wait_for_reaction。一种这样的方法的一个问题是它不允许您等待库提供的事件之外的事件。

在 v1.0 中,等待另一个事件的概念已被概括为与任何事件一起工作,如 Client.wait_for()。

您可以通过使用调试器并运行来相当容易地解决此问题

dir(ctx)

我还建议使用他们的不和谐服务器,他们能够使用机器人搜索文档。


推荐阅读