【发布时间】:2021-06-11 16:13:24
【问题描述】:
所以我在 python 中的不和谐机器人有任务在用户发送消息“$start”后立即发送嵌入。在此之后,代码将启动一个 while 循环并检查用户是否做出了反应。同时我想每秒编辑一次机器人的消息,这样我就可以显示某种计时器来向用户展示他们还有多少时间做出反应,但我不知道如何实现一个同时运行的计时器。使用multiprocessing 这个有用吗?
如果有人需要它来回答我的问题,这是我非常具体的代码:)
@client.command()
async def start(ctx):
timer = 120
remTime = timer
seedCapital = 10000
sessionEmpty = True
players[ctx.author] = seedCapital
em = discord.Embed(title = "New Poker Session", description = "Waiting for players to join ...\nreact with ???? to join or ▶️ to start (only host)", color = discord.Color.green())
#em.add_field(name = "2 minutes remaining from now on!", value = "")
botMsg = await ctx.send(embed = em)
await botMsg.add_reaction("????")
await botMsg.add_reaction("▶️")
while True:
mom0 = time.perf_counter()
try:
reaction, user = await client.wait_for('reaction_add', timeout = remTime, check = lambda reaction, user: reaction.emoji in ["????", "▶️"])
#msg = await client.wait_for('message', timeout = remTime, check = lambda m: m.channel == ctx.channel)
#print(f"receiving message: {msg.content}")
except asyncio.TimeoutError:
break
if reaction.emoji == "????":
sessionEmpty = False
await ctx.send(f"{user.mention} joined the session!")
players[user] = seedCapital
elif user == ctx.author and reaction.emoji == "▶️":
break
mom1 = time.perf_counter()
neededTime = mom1 - mom0
remTime -= neededTime
print(remTime)
if not sessionEmpty:
await ctx.send("starting session ...")
#start session
else:
await ctx.send("Noone joined your session :(")
【问题讨论】:
标签: python discord.py bots python-asyncio