【问题标题】:How to delete an embed message Discord.py?如何删除嵌入消息 Discord.py?
【发布时间】:2020-10-29 03:31:20
【问题描述】:

您好,我在自动删除消息时遇到问题,这是我正在使用的嵌入内容

@commands.command(name='hug', pass_context=True, aliases=['hugs'])
    @commands.cooldown(5, 60, commands.BucketType.user)
    async def hug(self, context, member: discord.Member):
        """Hug your friend"""
        author = context.message.author.mention
        mention = member.mention

        hug = "{0} hugs {1}"

        choices = ['example.gif']

        image = random.choice(choices)

        embed = discord.Embed(description=hug.format(author, mention), colour=discord.Color(random.randint(0x000000, 0xFFFFFF)))
        embed.set_image(url=image)

        await self.bot.say(embed=embed)
        await asyncio.sleep(3) 
        await self.bot.delete_message(embed)

好像我在这里做错了什么,因为它会抛出一个错误

File "C:\Users\Tom\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\client.py", line 1261, in delete_message
    channel = message.channel
AttributeError: 'Embed' object has no attribute 'channel'

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    您删除了Embed 所在的消息,而不是Embed 本身。

    sent = await self.bot.say(embed=embed)
    await asyncio.sleep(3) 
    await self.bot.delete_message(sent)
    

    【讨论】:

    • 我已经尝试过了,我收到了这个错误NameError: name 'sent' is not defined@Patrick Haugh(更正我一直忘记sent = await。抱歉,谢谢
    • 你是否也加入了sent = await self.bot.say(embed=embed)
    【解决方案2】:

    现在改写

    # For on_message
    await message.delete()
    
    # For commands
    await ctx.message.delete()
    

    编辑时删除嵌入

    # msg would be when you send the message Ex. msg = await ctx.send(embed=embed)
    # Make sure to specify what you want to edit with 'content' and 'embed'
    await msg.edit(content='Content Here', embed=None)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      • 2018-04-29
      • 2020-03-17
      • 2021-09-08
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多