Answer the question
In order to leave comments, you need to log in
How to send embed object type to discord using webhook?
import discord
embed_obj = discord.Embed(title="<i>Заголовок</i>", colour=2216909, url= "<i>ссылка</i>")
embed_obj.set_image(url="<i>ссылка на фото</i>")
embed_obj.set_thumbnail(url="<i>ссылка на фото</i>")
embed_obj.set_footer(text="<i>Текст</i>", icon_url="<i>ссылка на фото</i>")
embed_obj.set_author(name= "<i>Текст</i>", url= "<i>ссылка</i>", icon_url= "<i>ссылка на фото</i>")
embed_obj.add_field(name="<i>Текст</i>", value="")
Disc.post(embed = embed_obj)
Answer the question
In order to leave comments, you need to log in
By the name of the function .post
, I would venture to suggest that Disc
- an instance of an object of type aiohttp.ClientSession
. He doesn't know what discord is.
Several options for the development of decision events:
1. You continue to use the session to connect and send requests manually. In this case, you need to pass the json endpoint to the webhook yourself. You can get a dict to pass to the json argument of the post function via Embed.to_dict().
import discord
import aiohttp
session = aiohttp.ClientSession()
# Discord поддерживает только markdown для форматирования. html-теги (<i> и прочие) будут отображаться напрямую
embed_obj = discord.Embed(
title="*Заголовок*", colour=0x21D3CD, url="https://example.com"
) # Цвет в HEX более читаемый
embed_obj.set_image(url="https://via.placeholder.com/140x100")
embed_obj.set_thumbnail(url="https://via.placeholder.com/140x100")
embed_obj.set_footer(text="*Текст*", icon_url="https://via.placeholder.com/140x100")
embed_obj.set_author(
name="*Текст*",
url="https://example.com",
icon_url="https://via.placeholder.com/140x100",
)
embed_obj.add_field(
name="*Текст*", value="\N{Zero Width Space}"
) # Значение поля не может быть пустым, такой embed дискорд отклонит. Используем визуально пустой символ для этого.
await session.post(
"https://discord.com/api/webhooks/%WEBHOOK_ID%/%WEBHOOK_TOKEN",
json={"embeds": [embed_obj.to_dict()]},
)
import aiohttp
import discord
session = aiohttp.ClientSession()
webhook = discord.Webhook.from_url(
"https://discord.com/api/webhooks/%WEBHOOK_ID%/%WEBHOOK_TOKEN",
adapter=discord.AsyncWebhookAdapter(session),
)
embed_obj = ...
await webhook.send(embed=embed_obj)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question