【问题标题】:How to check if no message has been sent for X minutes? (discord.py)如何检查 X 分钟内是否没有消息发送? (discord.py)
【发布时间】:2020-12-08 18:37:32
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    试试这个。

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

    【讨论】:

      猜你喜欢
      • 2020-10-15
      • 2021-09-18
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多