python - 一旦我的不和谐机器人到达“等待 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)```
解决方案
上下文对象中没有 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)
我还建议使用他们的不和谐服务器,他们能够使用机器人搜索文档。
推荐阅读
- linux-kernel - 无论引导参数 console= 的值如何,如何让我的模块中的 printk 在串行控制台上显示消息
- itfoxtec-identity-saml2 - SAML2 令牌重放验证
- mysql - 需要帮助将列添加到子查询表
- python - 使用 smtplib 和 email.message.EmailMessage 发送电子邮件时未显示徽标/图像
- spring-boot - 在 Intellij-idea 中使用 gradle“compileOnly”项目类型依赖运行/调试应用程序
- xml - XSLT for-each 在每次迭代之前插入文本
- c++ - C++ Array 类可以作为可变大小的参数传递吗?
- python - 我可以在不使用睡眠的情况下每秒感知.set_pixels 吗?
- c - 此解决方案不接受任何浮点数
- python - 在python中使代码打印在同一行而不重复