【问题标题】:Use a local file as the set_image file discord.py使用本地文件作为 set_image 文件 discord.py
【发布时间】:2020-08-18 02:15:36
【问题描述】:

我知道在 discord.py 中,您可以将嵌入的 set_image 设置为图像的 url。但是,我想在我的计算机上使用本地文件作为 set_image 而不是图像的 url。

embed = discord.Embed(title="Title", description="Desc", color=0x00ff00)
embed.set_image(url = "https://example.com/image.png") #this is for set_image using url

我怎样才能做到这一点?有没有别的功能什么的?

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    好的,我刚刚知道了。它的代码如下:

    embed = discord.Embed(title="Title", description="Desc", color=0x00ff00) #creates embed
    file = discord.File("path/to/image/file.png", filename="image.png")
    embed.set_image(url="attachment://image.png")
    await ctx.send(file=file, embed=embed)
    

    唯一你应该改变的是第 2 行,上面写着"path/to/image/file.png"

    注意:在第 2 行和第 3 行,有一个 image.png。不要担心,因为这就是 Discord 所称的上传文件(例如:我有一个名为 duck.png 的文件,Discord 将其作为 image.png 上传到他们的服务器)。因此,您无需更改 image.png 部分。但是,如果您使用的是特定扩展名很重要的文件,请记住将 image.png 更改为所需的扩展名。需要特定扩展名的文件示例是 GIF,因此如果您使用的是 GIF,请记住将 image.png 更改为 image.gif

    您可以在 discord.py 的官方文档中阅读更多内容:https://discordpy.readthedocs.io/en/latest/faq.html#how-do-i-use-a-local-image-file-for-an-embed-image

    【讨论】:

    • @AnchithAcharya 糟糕的是,我的回答没有那么详细。我只是继续编辑它。将image.png 更改为image.gif。该文件名是 Discord 服务器上文件的名称。在您的场景中,您正在调用本地 gif 文件,但将其上传为 .png
    • 不,我可以从语法中推断出来。我确实尝试过使用 .gif,但程序只是在 channel/ctx.send 处停止,甚至没有返回任何错误消息。我尝试将 gif 作为文件上传,但结果相似。它只是进入了 send 方法,再也没有返回。或者我是这么想的。 分钟 后,gif 弹出。我认为流程在发送方法中突然停止,而事实上,它根本没有停止并且一直在上传。猜猜使用 url 上传比使用本地文件更好,对于 gif。
    • 您的代码 sn-p 重新定义了第 3 行的嵌入,它丢失了您在第 1 行设置的颜色、标题和描述。
    • @SamRockett 我的错。我已更改答案以反映更改。
    【解决方案2】:

    Peppa 的回答是正确的,但我必须补充一点,如果您是为 gif 执行此操作,最好将实际 url 传递给 set_image,而不是 attachments://image.gif url。因为每次您执行后者并调用 ctx.send 或 channel.send 时,必须先上传 gif 才能发送。如果您的 gif 未压缩,这可能需要很长时间(对我来说,5MB gif 大约需要 2-3 分钟)。而通过传递 url,您可以在几秒钟内获得 gif。

    【讨论】:

      猜你喜欢
      • 2021-06-17
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      • 2020-11-04
      相关资源
      最近更新 更多