编辑 discord.py 嵌入,就像有人对它“发表评论”一样

时间:2021-01-31 03:12:06

标签: python discord discord.py discord.py-rewrite

我有一个命令,它根据用户的消息发送嵌入...我希望能够使用命令将内容添加到嵌入消息中,就像我在评论一样。以下是 Discord 制作的机器人的示例:

embed "can reproduce" field has comments

正如您在图片中看到的,人们对嵌入内容发表了评论。我希望能够做到这一点:保留原始内容,使用命令添加更多内容。
我知道如何编辑消息,但找不到有关在特定嵌入字段中添加内容的任何信息。

一些示例代码,以便您了解我的思考过程:

@client.command()
async def comment(ctx, message, *, args):
    messagec = await ctx.fetch_message(message)
    embed = discord.Embed(title=messagec.content['title'], description=messagec.content['description'])
    value = f'\n`{ctx.message.author.name}: {args}`'
    embed.add_value(name='comments', value=value)
    await message.edit(embed=embed)

请不要回复告诉我为什么此代码不起作用我知道为什么这只是一个示例,以便更容易理解我的思维过程。

2 个答案:

答案 0 :(得分:1)

这非常简单,您可以使用 Embed.to_dict 将嵌入转换为字典,然后像使用普通的 Python dict 一样使用它。然后将其转换回 discord.Embed 实例,使用 Embed.from_dict 类方法

embed_dict = embed.to_dict()

for field in embed_dict["fields"]:
    if field["name"] == "your field name here":
        field["value"] += "new comment!"

embed = discord.Embed.from_dict(embed_dict)
await message.edit(embed=embed)

参考:

答案 1 :(得分:0)

谢谢。我设法使用了 message.embeds 并且效果很好。

embed = message.embeds[0]
embed_dict = embed.to_dict()

for field in embed_dict["fields"]:
    if field["name"] == "your field name here":
        field["value"] += "new comment!"

embed = discord.Embed.from_dict(embed_dict)
await message.edit(embed=embed)
相关问题