@saico

TypeError: Object of type Embed is not JSON serializable. Как исправить?

Что значит ошибка TypeError: Object of type Embed is not JSON serializable? Пытаюсь ее решить, ничего не помогает. Помогите, пожалуйста.

Код:
@commands.command()
@commands.guild_only()
async def tweet(self, ctx, *, message):
	api = nekobot.NekoBot()
	message = str(api.tweet(ctx.author.display_name, message))[18:-59]
	async with aiohttp.ClientSession() as ClientSession:
		async with ClientSession.get(message) as reslink:
			res = io.BytesIO(await reslink.read())
			file = discord.File(res, filename = "tweet.png")
			embed = discord.Embed(color = 0x33FF57)
			embed.set_image(url = "attachment://tweet.png")
			await ctx.send(embed = embed, file = file)

Ошибка:
Ignoring exception in command tweet:
Traceback (most recent call last):
  File "D:\Python\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "D:\Bot\RUI\cogs\Fun.py", line 709, in tweet
    await ctx.send(embed = embed, file = file)
  File "D:\Python\lib\site-packages\discord_components\client.py", line 46, in send_component_msg_prop
    return await self.send_component_msg(ctxorchannel.channel, *args, **kwargs)
  File "D:\Python\lib\site-packages\discord_components\client.py", line 143, in send_component_msg
    "payload_json", dumps(data, separators=(",", ":"), ensure_ascii=True)
  File "D:\Python\lib\json\__init__.py", line 234, in dumps
    return cls(
  File "D:\Python\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "D:\Python\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "D:\Python\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Embed is not JSON serializable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\Python\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "D:\Python\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "D:\Python\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: Object of type Embed is not JSON serializable
  • Вопрос задан
  • 911 просмотров
Пригласить эксперта
Ответы на вопрос 1
Allan-BlackWell
@Allan-BlackWell
Ничтожество :D
Как знаю - File не может принимать значение байтов, по-этому сначала можно попробовать конвертировать его в изображение с помощью PIL (Pillow)
from PIL import Image

Сам код:
@commands.command()
@commands.guild_only()
async def tweet(self, ctx, *, message):
    api     = nekobot.NekoBot()
    message = str(api.tweet(ctx.author.display_name, message))[18:-59]
  
    async with aiohttp.ClientSession() as ClientSession:
        async with ClientSession.get(message) as reslink:
            res   = await reslink.read()
            data  = io.BytesIO(res)
            image = Image.open(data)

            file = io.BytesIO()

            image.save(file, format="PNG")
            file.seek(0)

            embed = discord.Embed(color = 0x33FF57).set_image(url = "attachment://tweet.png")
            
            await ctx.send(embed = embed, file = discord.File(image, filename="1.png"))


Если не работает можете комментарий написать, уже у себя потестирую. Сейчас просто не особо времени нет
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы