【问题标题】:I want to delete every message until 10 seconds ago我想删除直到 10 秒前的每条消息
【发布时间】:2023-03-16 09:00:01
【问题描述】:

标题主要是这样说的,我尝试了各种方法,我尝试使变量 = 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 秒内删除每条消息

【问题讨论】:

    标签: discord discord.py purge


    【解决方案1】:

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

    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 的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 2021-02-18
      • 1970-01-01
      • 2011-06-09
      • 2018-06-14
      相关资源
      最近更新 更多