gpt4 book ai didi

python - 编辑一个 discord.py 嵌入,就好像有人在上面有 "commented"

转载 作者:行者123 更新时间:2023-12-04 15:06:52 25 4
gpt4 key购买 nike

我有一个命令可以根据用户的消息发送嵌入...我希望能够使用命令将内容添加到嵌入消息中,就好像我在评论一样。这是 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)

请不要回答告诉我为什么这段代码不起作用我知道为什么这只是一个例子,这样更容易理解我的思维过程。

最佳答案

这非常简单,您可以使用 Embed.to_dict 将嵌入转换为字典,然后像使用普通 python 字典一样使用它。然后将其转换回 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)

引用:

关于python - 编辑一个 discord.py 嵌入,就好像有人在上面有 "commented",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65975406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com