![](/img/trans.png)
[英]How to create a loop to send a message if the channel last message was sent X minutes ago? [discord.py]
[英]How to check if no message has been sent for X minutes? (discord.py)
我正在嘗試執行一個命令,當在用戶指定的時間內沒有發送任何消息時,機器人會發送一個隨機主題,但是,我似乎無法獲得正確的時間部分。
我的問題是:如何檢查頻道中是否有 X 分鍾未發送消息? 這是我想出的代碼:
@bot.command()
async def timedtopic(ctx, time : int):
global keepLooping
timer = dt.datetime.utcnow() + dt.timedelta(seconds=time)
keepLooping = True
embed = discord.Embed(title="Reviver's topic is", description=(random.choice(List)))
await asyncio.sleep(time)
while keepLooping:
if timer > ctx.channel.last_message.created_at and ctx.channel.last_message.author != bot.user:
await ctx.send(embed=embed)
試試這個。
channel = ctx.channel
async for message in channel.history(limit = 1):
past_message = message.created_at
await asyncio.sleep(time)
async for message in channel.history(limit = 1):
new_message = message.created_at
if past_message == new_message:
print(f"No message has been sent in {time} seconds!")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.