如何让机器人在一段时间后删除其嵌入的消息?

时间:2021-03-13 13:11:38

标签: python discord.py bots

下午好,我想让机器人在一分钟内删除它的嵌入消息,我该如何实现,这是代码

    @client.command(pass_context = True)
@commands.has_any_role(819292703589269514,817408828500213860,817408830240456754,817643991331766283)
async def clear (ctx, amount : int):
    await ctx.channel.purge(limit = amount + 1)
    emb = discord.Embed (title = 'Удалено {} сообщений!'.format(amount), colour = discord.Color.gold())
    await ctx.send(embed = emb)
    await asyncio.sleep(15)
    await emb.delete()

2 个答案:

答案 0 :(得分:0)

如果其他人遇到问题,这里是一个答案:

请注意缩进正确,有些地方空格太多。

您也可以使用 delete_after“事件”代替您的 emb.delete()

看起来像这样:

@client.command(pass_context=True)
@commands.has_any_role(819292703589269514, 817408828500213860, 817408830240456754, 817643991331766283)
async def clear(ctx, amount: int):
    await ctx.channel.purge(limit=amount + 1)
    emb = discord.Embed(title='Удалено {} сообщений!'.format(amount), color=discord.Color.gold())
    await ctx.send(embed=emb, delete_after=60) # delet_after added

如果你想使用你的方法,你必须定义你发送的嵌入:

@client.command(pass_context=True)
@commands.has_any_role(819292703589269514, 817408828500213860, 817408830240456754, 817643991331766283)
async def clear(ctx, amount: int):
    await ctx.channel.purge(limit=amount + 1)
    emb = discord.Embed(title='Удалено {} сообщений!'.format(amount), colour=discord.Color.gold())
    test1 = await ctx.send(embed=emb) # We define the embed as test1
    await asyncio.sleep(15)
    await test1.delete() # test1 (Embed) gets deleted

答案 1 :(得分:0)

这样做非常简单。您只需要定义一个发送嵌入的变量。这是代码 -

@client.command(pass_context = True)
@commands.has_any_role(819292703589269514,817408828500213860,817408830240456754,817643991331766283)
async def clear (ctx, amount : int):
    await ctx.channel.purge(limit = amount + 1)
    emb = discord.Embed (title = 'Удалено {} сообщений!'.format(amount), colour = discord.Color.gold())
    sendemb = await ctx.send(embed = emb)
    await asyncio.sleep(15)
    await sendemb.delete()

所以这里我所做的就是将嵌入发送命名为 (sendemb),然后在休眠 15 秒后将其删除!

希望能帮到你。