繁体   English   中英

如何检查是否有 X 分钟未发送消息? (不和谐.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM