B
B
Bubble_in_space2020-08-11 11:31:29
Python
Bubble_in_space, 2020-08-11 11:31:29

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)

spoiler
{ВЕСЬ ТЕКСТ И ССЫЛКИ УБРАЛ ДЛЯ ВОПРОСА}

Embed itself is assembled without errors and printed as an object:
discord.embeds.Embed object at 0x7f48da700280
When trying to send, an error occurs:
TypeError: post() got an unexpected keyword argument 'embed'
If you send via embeds, you need to convert to json, but this it didn't work out for me.
If you create pure json embed and send it, then the code works, but this option is not suitable because json must be generated depending on the request and the embed_obj.set method ... is best
===
Please tell me how send ready object embed or how to convert this object into normal json

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2020-08-11
@Bubble_in_space

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()]},
)

2. Use the webhook support from discord.py: https://discordpy.readthedocs.io/en/v1.4.1/api.htm... .
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 question

Ask a Question

731 491 924 answers to any question