使用类创建特定的嵌入不和谐
Posted
技术标签:
【中文标题】使用类创建特定的嵌入不和谐【英文标题】:Create specific embed discord with a class 【发布时间】:2022-01-14 05:39:12 【问题描述】:我想使用名为 embed 的类创建我的不和谐嵌入,因为嵌入行在我的代码中占用了大量空间,我想对其进行优化。谢谢你读我。 我的不和谐文件
@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)
我的 embed.py 文件
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)
错误信息:
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'
【问题讨论】:
【参考方案1】:在内部 discord.py
将嵌入作为 JSON 数据发送(在 python 中表示为 dict
)。您可以创建自己的to_dict
方法来解决此问题
class EMBED:
...
def to_dict(self):
return self.embed.to_dict()
【讨论】:
以上是关于使用类创建特定的嵌入不和谐的主要内容,如果未能解决你的问题,请参考以下文章