【问题标题】:Discord PYthon bot deleting own message after some timeDiscord PYthon 机器人在一段时间后删除自己的消息
【发布时间】:2021-05-17 23:34:40
【问题描述】:

我的机器人有一个清除命令。但是如果你清除,机器人会发送一条消息,说你清除了这个聊天......那么我怎么能在 10 秒后使用 maby asyncio 删除它...... 这是我的代码:

import discord
from discord.ext import commands
import asyncio

bot = commands.Bot(command_prefix='>', description="This is a bot")

@bot.command(aliases=['c'])
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount: int = None):
    if amount == 0:
        await ctx.channel.purge(limit=10000000000000000000000)
        await ctx.send("Cleared the entire chat!")
        print("Cleared the chat!")
    else:
        if amount >= 1:
            await ctx.channel.purge(limit=amount)
            await ctx.send("Done!")
            print(f"Cleared {amount} messages!")

感谢您的帮助...

【问题讨论】:

    标签: python discord bots


    【解决方案1】:
    await ctx.send("...", delete_after=10.0)
    

    参数delete_after会在10s后删除消息。

    或者,您也可以将消息分配给一个变量,等待,然后删除。

    msg = await ctx.send("...")
    await asyncio.sleep(10.0)
    await msg.delete()
    

    【讨论】:

      猜你喜欢
      • 2021-04-01
      • 2022-01-06
      • 2018-12-29
      • 2020-08-24
      • 2019-02-20
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      相关资源
      最近更新 更多