如何检查嵌入是否为图像预览?

时间:2021-05-14 04:56:55

标签: python discord.py

我正在尝试检查不和谐频道中的嵌入内容并删除它们。但是,带有预览链接的邮件也会被删除。 这是我的代码。

@client.event
async def on_message(message):
    if len(message.embeds) > 0 and message.channel.id in chat_channels:
         await message.delete()
         await message.channel.send("No embeds in this channel")

1 个答案:

答案 0 :(得分:0)

Discord converts 使用嵌入来显示链接的预览,因此它也显示为 message.embeds 中的嵌入。如果我们看一下 api docs,我们可以看到有几种类型的嵌入,几乎都是某种形式的预览嵌入。

这里是图片预览的json。

{
    "thumbnail":{
        "url":"https:\/\/cdn.discordapp.com\/avatars\/488278979900342282\/a738115e01fe31d415af7b7d4b862acb.png?size=1024",
        "proxy_url":"https:\/\/images-ext-2.discordapp.net\/external\/QODrzk5Or_q-xaVaACaXt_GVP8m7ZQ34ezGFwJgrAK4\/%3Fsize%3D1024\/https\/cdn.discordapp.com\/avatars\/488278979900342282\/a738115e01fe31d415af7b7d4b862acb.png",
        "width":410,
        "height":410
    },
    "type":"image",
    "url":"https:\/\/cdn.discordapp.com\/avatars\/488278979900342282\/a738115e01fe31d415af7b7d4b862acb.png?size=1024"
}

图像预览的类型是图像。如果嵌入是由机器人或网络钩子手动发送的,则它的类型为 rich,我们可以使用它来忽略您案例的预览。

@client.event
async def on_message(message):
    if len(message.embeds) > 0 and message.channel.id in chat_channels:
       if any('rich' == embed.type for embed in message.embeds):
            #remove message, as it has a rich embed
相关问题