首页 > 解决方案 > Discordpy 将 add_field 添加到现有的嵌入命令

问题描述

我正在尝试将单个嵌入用作某种列表。我希望在将命令推送到机器人时添加一个 add_field。例如,我想在当前嵌入中添加一个带有 embed.add_field(name, value) 的新行,该行由用户在激活时给出。我该怎么办?我尝试过使用消息 ID 和消息内容,但我认为这不是最好的方法。提前致谢。

@bot.command()
async def mangaEmbed(ctx):
    embed = discord.Embed(
        title = "Completed Self Prints",
        #description ="test",
        color = discord.Color.blue()
        )
    #embed.set_footer(text="Footer")
    #embed.set_image(url="https://media.discordapp.net/attachments/138231921925816320/902586759773306900/unknown.png?width=810&height=375")
    #embed.set_thumbnail(url="https://media.discordapp.net/attachments/138231921925816320/902586759773306900/unknown.png?width=810&height=375")
    #embed.set_author(name= "Kyle G", icon_url="https://media.discordapp.net/attachments/138231921925816320/902586759773306900/unknown.png?width=810&height=375")
    embed.add_field(name="Kokou no Hito by NggKGG", value="Volumes 1-17", inline=False)
    embed.add_field(name="DrangonBall Z\nby NggKGG", value="Kokoue no 222o", inline=False)
    embed.add_field(name="Homoculus", value="Volumes 1-24\nManmangaboy", inline=False)
    await ctx.send(embed=embed)

新问题是无法使用消息 id 传递 discord.Message

@bot.command()
async def editembed(ctx, vols, *, title):
    #just assuming you only want to get the first embed in the message
    message = discord.Message(903060490035556383)
    print(message)
    embed = message.embeds[0]
    embed.add_field(name=title, value=vols, inline=False)
    await message.edit(embed=embed)

标签: pythondiscorddiscord.py

解决方案


如果我正确理解了您的问题,您想编辑已从其他命令发送的嵌入,对吗?

message具有属性embeds,它将返回消息具有的嵌入列表。您可以使用它来获取所需的嵌入并在单独的命令中对其进行编辑。
像这样的东西应该工作:

@bot.command()
async def editembed(ctx, message: discord.Message):
    #just assuming you only want to get the first embed in the message
    embed = message.embeds[0]
    embed.add_field(name="test", value="test")
    await message.edit(embed=embed)

https://discordpy.readthedocs.io/en/master/api.html?highlight=message#discord.Message.embeds

编辑:

关于您的第二个问题:
您必须从某个地方获取您的消息,如果它始终是您想要编辑的同一个嵌入,您可以使用在您的命令中定义它fetch_message。有两种方法可以做到这一点,要么您还必须获取您要编辑的消息所在的频道,或者只是使用ctx但您只能在与您要编辑的嵌入相同的频道中使用此命令。

选项 1(推荐):

@bot.command()
async def editembed(ctx):
    channel = bot.get_channel(1234567890)
    message = await channel.fetch_message(1234567890)

选项 2:

@bot.command()
async def editembed(ctx):
    message = await ctx.fetch_message(1234567890)

请记住,嵌入中只能有 25 个字段。


推荐阅读