【问题标题】:discord.py embed with locally saved imagesdiscord.py 嵌入本地保存的图像
【发布时间】:2018-07-09 20:52:45
【问题描述】:

所以要在 discord.py 中嵌入图片,语法是

embed.set_image(url='http://url_to_image')

但我的问题是我不知道如何将其设置为本地保存的图像,因此我不必继续与这些 URL 建立连接。

谢谢。

(文档链接:http://discordpy.readthedocs.io/en/latest/api.html#discord.Embed.set_image

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您需要将机器人根目录中的本地图像设置为附件,并且您还需要在ctx.send 中提及该文件!我尝试只做附件,但它显示一个空白嵌入。因此,请提及file 并将相同的文件作为附件放入set_image 并尝试运行代码。它有效?

        file = discord.File("output.png")
        e = discord.Embed()
        e.set_image(url="attachment://output.png")
        await ctx.send(file = file, embed=e)
    

    如果您仍然遇到问题,请告诉我!

    【讨论】:

      【解决方案2】:

      我的计算机上有一个包含图像的文件夹,我的不和谐机器人使用它来随机挑选和发送图像。

      这是我的代码简化并重新用于仅发送一张随机图像:

      import os    
      
      @client.event
      async def on_message(message):
          if message.content == ".img": # Replace .img with whatever you want the command to be
      
              imgList = os.listdir("./All_Images") # Creates a list of filenames from your folder
      
              imgString = random.choice(imgList) # Selects a random element from the list
      
              path = "./All_Images/" + imgString # Creates a string for the path to the file
      
              await client.send_file(message.channel, path) # Sends the image in the channel the command was used
      

      同样在@client.event 中,您需要记住将客户端替换为您在代码中进一步命名的客户端。

      当然,还要将文件夹名称更改为与您的文件实际位于同一文件夹中的名称。

      我希望这是您正在寻找的答案,祝您好运!

      (http://discordpy.readthedocs.io/en/latest/api.html#discord.Client.send_file)

      【讨论】:

      • 这并不能解决使用set_image方法设置Embed的本地保存图像的问题。
      • 我仍在寻找解决方案。我一直在尝试嵌入本地保存的图像,然后编辑嵌入以更改图像。
      • 在嵌入中使用本地文件作为 set_image 的答案可在 stackoverflow.com/questions/61578927/… 获得
      猜你喜欢
      • 2021-10-26
      • 2021-12-07
      • 2021-05-19
      • 2021-09-06
      • 2023-03-09
      • 2016-10-30
      • 2014-03-30
      • 1970-01-01
      • 2021-06-17
      相关资源
      最近更新 更多