【问题标题】:How do I make the bot delete its embed message after a while?如何让机器人在一段时间后删除其嵌入消息?
【发布时间】:2021-06-11 07:09:45
【问题描述】:

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

    @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()

【问题讨论】:

  • 您有任何错误吗?您的代码中有解决方案,只需将 15 更改为 60?
  • 是的,我把它改成60,我只是测试了一下,这样可以更快地删除它,但它没有被删除,我没有收到任何错误
  • 你试过没有asyncio事件的await ctx.send(embed=emb, delete_after=60)吗?
  • 不,我现在就试试

标签: python discord.py bots


【解决方案1】:

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

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

您也可以使用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

【讨论】:

    【解决方案2】:

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

    @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 秒后将其删除!

    希望我能帮到你。

    【讨论】:

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