Home > OS >  Create specific embed discord with a class
Create specific embed discord with a class

Time:12-09

I want to create my discord embed with a class named embed because the embed lines takes a lot of space in my code and I want to optimize it. Thank you for reading me. My discord file

@bot.command()
async def test(ctx): # Commande de test pour vérifier que le bot est bien en Etat de répondre 
    '''Commande inutile pour le moment'''
    embed_test = embed.EMBED("title", "description", "!nom_fonction", 
    "https://www.supersoluce.com/sites/default/files/styles/picto_soluce/interrogation.png")
    embed_test.create()
    embed_test.add_field("Test1", "Value1")
    embed_test.add_field("Test2", "Value2")
    await ctx.send(embed=embed_test)

My embed.py file

from urllib.parse import urlsplit, parse_qs
import discord

class EMBED:
    def __init__(self, title, description, nom_fonction=None, logo=None, color=0x1f6e9e):
        self.embed_title = title
        self.embed_description = description
        self.embed_nom_fonction = nom_fonction
        self.embed_logo = logo
        self.embed_color = color
        self.embed = discord.Embed(title= self.embed_title, url= "https://myges.fr", description= self.embed_description, color= self.embed_color)

    def create(self):
        self.embed.set_author(name=f"ESGI | {self.embed_nom_fonction}", icon_url= self.embed_logo)
        self.embed.set_thumbnail(url="https://www.sciences-u-lyon.fr/images/2020/03/myges.png")
        self.embed.set_footer(text="Made by DAVE")
        
    def add_field(self, name, value, inline=True):
        self.embed.add_field(name=name, value=value, inline=inline)

Error message:

Ignoring exception in on_command_error
Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\Users\rmassiet\Desktop\ESGI bot\ESGI_BOT_DISCORD\main.py", line 70, in test
    await ctx.send(embed=embed_test)
  File "C:\Python310\lib\site-packages\discord\abc.py", line 1017, in send
    embed = embed.to_dict()
AttributeError: 'EMBED' object has no attribute 'to_dict'

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

Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\rmassiet\Desktop\ESGI bot\ESGI_BOT_DISCORD\main.py", line 113, in on_command_error
    raise error
  File "C:\Python310\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Python310\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Python310\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: AttributeError: 'EMBED' object has no attribute 'to_dict'

CodePudding user response:

Internally discord.py sends the embed as JSON data (represented as a dict in python). You can create your own to_dict method to fix the issue

class EMBED:
    ...

    def to_dict(self):
        return self.embed.to_dict()
  • Related