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

2024-09-29 23:16:17 发布

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

我正在尝试执行一个命令,当用户指定的时间内没有消息发送时,bot会发送一个随机主题,但是,我似乎无法正确把握时间

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

Tags: 用户命令消息messagetimebot时间dt
1条回答
网友
1楼 · 发布于 2024-09-29 23:16:17

试试这个

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

相关问题 更多 >

    热门问题