本文介紹了如何檢查 X 分鐘內(nèi)是否沒有消息發(fā)送?(discord.py)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在嘗試執(zhí)行一個(gè)命令,當(dāng)在用戶指定的時(shí)間內(nèi)沒有發(fā)送任何消息時(shí),機(jī)器人會(huì)發(fā)送一個(gè)隨機(jī)主題,但是,我似乎無法正確設(shè)置計(jì)時(shí)部分.
I am trying to do a command where the bot sends a random topic when no message has been sent for the time the user specifies, however, I can't seem to get the timing part right.
我的問題是:如何檢查頻道中 X 分鐘內(nèi)是否沒有消息發(fā)送?這是我想出的代碼:
My question is: How do I check if no message has been sent in a channel for X minutes? Here is the code I came up with:
@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)
推薦答案
試試這個(gè).
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!")
這篇關(guān)于如何檢查 X 分鐘內(nèi)是否沒有消息發(fā)送?(discord.py)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!