我想删除每条消息直到 10 秒前

问题描述

标题主要是这样说的,我尝试了各种方法,我尝试将变量设置为 1 并将每条消息附加到列表中,然后在 10 秒后说列表 = [],我从 pyhton discord 服务器获得了帮助,但是它最后没有用,然后我在文档上看到有办法在某个日期后删除消息,但我不知道如何将其设置为 10 秒前,所以我问是否有人对此有任何想法怎么做,我的代码

@commands.cooldown(rate=1,per=25,type=commands.BucketType.user)
async def king_crimson(ctx):
    kingcrimson = discord.utils.get(ctx.guild.roles,id=817441099173199923)
    if kingcrimson in ctx.author.roles:
        kc_list = []
        kc_ability = []
        await ctx.channel.purge(limit=1)
        kc_embed = discord.Embed(title="King Crimson activated!",colour=discord.Colour(0xb90d0d))
        await ctx.author.send(embed=kc_embed)
        kc_ability = 1
        await asyncio.sleep(10)
        kc_ability = 0
        kc_embed2 = discord.Embed(title="Time has skipped!",colour=discord.Colour(0xb90d0d))
        await ctx.author.send(embed=kc_embed2)
        await ctx.channel.purge(after=10)

基本上当命令被触发时,它会等待 10 秒,然后在这 10 秒内删除每条消息

解决方法

当命令被调用时,将当前时间存储为一个日期时间对象:

import datetime

start_time = datetime.datetime.now()

await asyncio.sleep(10)

now = datetime.datetime.now()
await ctx.channel.purge(before=now,after=start_time,bulk=True)

您还应该指定 bulk=True,以便一次性删除所有消息。您可能还想设置一个检查,以便不会删除机器人消息。您可以找到有关清除命令 here 的更多信息。