首页 > 解决方案 > 为什么我不能将嵌入的图像设置为用户发送的附件?

问题描述

制作一个允许用户报告错误的命令。我想要它,这样你就可以在错误中包含一个图像,这样你就可以展示一个视觉示例,除了我没有的东西不起作用,并且在查看文档时,这应该可以工作。

我遇到的问题是嵌入没有发送。当我删除bugEmbed.set_image()它时,它再次起作用。

@commands.command()
async def bug(self, ctx, *, bugReport=None):
    """Command that allows users to report bugs about the bug"""

    channel = self.client.get_channel(864211572218265610)

    bugEmbed = discord.Embed(
        title=f"Bug Report",
        description=bugReport,
        color= 0xFFFF00
    )

    bugEmbed.add_field(
        name="Reported by",
        value=f"<@{ctx.author.id}>"
    )

    bugEmbed.set_footer(
        text="",
        icon_url=ctx.author.avatar_url
    )
    bugEmbed.set_image(
        url=bugReport.attachments.url
    )

    if bugReport is None:
        await ctx.send("You didn't include a bug with the report! Try again.")

    await channel.send(embed=bugEmbed)

标签: pythonpython-3.xdiscorddiscord.py

解决方案


问题可能与attachments列表有关,因此您无法访问名为 url 的属性,这意味着您必须执行以下操作:

if len(bugReport.attachments):
    bugEmbed.set_image(
        url=bugReport.attachments[0].url
    )

编辑:好的,所以问题是您试图从中获取附件,bugReport这只是您传递的文本,您真正想要做的是从上下文(ctx)中获取该数据

if len(ctx.message.attachments):
    bugEmbed.set_image(
        url=ctx.message.attachments[0].url
    )

推荐阅读