I am trying to check for embeds in a discord channel and delete them. However, messages with links that have a preview are also getting deleted. Here is my code.
@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")
Discord converts uses embeds to show previews from links, hence it is also shown as an embed in message.embeds
. If we take a look at the api docs , we can see that there are several types of embeds, almost all are some form of preview embeds.
here is the json of an image preview.
{
"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"
}
The type of image previews are image. If the embed was manually sent by a bot or a webhook, it would have the type rich
, we can use this to ignore previews for your case.
@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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.