【问题标题】:(discord.py) Any way to automatically save an embed image?(discord.py) 有什么方法可以自动保存嵌入图像?
【发布时间】:2021-05-19 03:54:20
【问题描述】:

我正在尝试这样做,以便当有人在嵌入消息中发送图像时,机器人将保存图像。当有人将图像作为附件发送时,我已经想出了如何做到这一点,但是当附件在嵌入中时,我无法弄清楚如何做到这一点。有人能帮我吗?我对 discord.py 或一般的编码很陌生,所以我试图弄乱它。

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    Message object 有一个embeds attribute,它是一个List of Embed objects。嵌入对象有一个 image attribute,其中包含图像的 URL。

    有了这些知识,我们可以执行以下操作(简化的逻辑优先代码):

    import discord
    import urllib
    
    # purposely left out command boilerplate...
    
    embeds = message.embeds
    for embed in embeds:
        image_url = embed.image.url
        # check whether there is an image url
        if image_url != discord.Embed.Empty:
            # as seen on https://stackoverflow.com/a/8286449/13042738
            # save image to `filename.jpg`
            urllib.request.urlretrieve(image_url, "filename.jpg")
    

    【讨论】:

      猜你喜欢
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      相关资源
      最近更新 更多