如何检查X分钟内是否未发送消息? (discord.py)

时间:2020-08-19 13:18:35

标签: python discord discord.py discord.py-rewrite

我正在尝试执行一条命令,使机器人在用户指定的时间内没有发送消息时,机器人发送一个随机主题,但是,我似乎无法正确安排计时部分。

我的问题是:如何检查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)

1 个答案:

答案 0 :(得分:0)

尝试一下。

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!")