我的discord bot在asyncio.sleep之后发送多条消息。如何解决这个问题?

2024-06-01 08:26:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个代码,在其中我在一定时间后删除afk。用户可以在这段时间内发送任意数量的消息,但afk不会被删除,它只会在一定时间后被删除,但我的代码只发送用户发送的消息数量与删除的afk嵌入的数量相同

这是我的密码

if afkData.val() is not None:
    await asyncio.sleep(10)
    db.child("AFK").child(str(message.guild.id)).child(str(message.author.id)).remove()
    em = discord.Embed(title="AFK removed",description=f"Your AFK was removed {message.author.mention}",color=discord.Color.from_rgb(255,20,147))
    await message.channel.send(embed=em)
elif message.mentions:
    for i in message.raw_mentions:
        AFK = db.child("AFK").child(str(message.guild.id)).child(str(i)).get()
    if AFK is not None:
        reason = AFK.val()["reason"]
        em = discord.Embed(title=f"User AFK",description=f"The Mentioned user is AFK....... **Reason: {reason}**",color=discord.Color.from_rgb(255,20,147))
        await message.channel.send(embed=em)

这里if语句运行多次。请帮我解决这个问题


Tags: 代码idchildmessage数量ifis时间
1条回答
网友
1楼 · 发布于 2024-06-01 08:26:15

你的密码错了

await asyncio.sleep(10)

在一定时间后不会删除AFK,它只会等待10秒,然后再执行其他代码。 因此,我不知道如何向您推荐一种简单的方法来设置您想要的计时器,但我可以告诉您,您编写的代码不会限制用户命令和/或在一段时间后删除AFK。用户输入后,它将仅等待10秒

(prefix)(command)

。我希望我能帮助你,对于任何其他信息,我都会非常乐意帮助你

相关问题 更多 >