【问题标题】:Sending a File in an Embed Discord.py在嵌入 Discord.py 中发送文件
【发布时间】:2020-08-05 03:01:05
【问题描述】:

我正在尝试让我的机器人下载图像,将其更改为 url,然后将其嵌入 Discord py 中。但是在将文件更改为url的过程中,永远找不到文件的目录。这是我用来将文件更改为 url 的代码:

f = discord.File(f"/Desktop/Bot/Dino_Battles/DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}.jpg", filename = f'DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}.jpg')

但是,我总是收到一个错误,即即使图像存在也无法找到该目录。 这是错误:

FileNotFoundError: [Errno 2] No such file or directory: '/Desktop/Bot/Dino_Battles/DinoBattle_628693454754676768_VS_725411116272058369.jpg

这是文件目录:

C:\Users\lbart\Desktop\Bot\Dino_Battles\DinoBattle_628693454754676768_VS_725411116272058369.jpg

discord 怎么找不到文件? 很抱歉,如果这令人困惑。

【问题讨论】:

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


    【解决方案1】:

    我建议您将文件上传到https://imgur.com/,然后复制链接并使用链接而不是文件。这要容易得多:>

    P.S 记得使用 embed.set_image(url=imgurlink) 或 embed.set_thumbnail,随你喜欢

    【讨论】:

    • 哦,这是个好主意。有办法通过python上传对吗?
    • 通过python是什么意思?如果你的意思是喜欢通过文件,是的,但我不建议你这样做,除非你的老师特别告诉你去做你现在正在做的事情:>
    • 如果它适合你,请接受我的回答:> 非常感谢
    【解决方案2】:

    正如 Daniel Tam 所说,最好将其上传到网站。 imgur api docs。这也是一篇关于如何通过步骤使用它的好文章link

    这里是一个使用pyimgur上传和获取URL的例子

    import pyimgur
    
    CLIENT_ID = "Your_applications_client_id"
    PATH = "A Filepath to an image on your computer"
    
    im = pyimgur.Imgur(CLIENT_ID)
    uploaded_image = im.upload_image(PATH, title= f'DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}')
    print(uploaded_image.title)
    print(uploaded_image.link)
    print(uploaded_image.size)
    print(uploaded_image.type)
    

    上传后,您可以使用uploaded_image.link作为嵌入中的照片。

    【讨论】:

      【解决方案3】:

      尝试包含完整路径,来自

      f = discord.File(f"/Desktop/Bot/Dino_Battles/DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}.jpg", filename = f'DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}.jpg')
      

      f = discord.File(f"c:/Users/lbart/Desktop/Bot/Dino_Battles/DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}.jpg", filename = f'DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}.jpg')
      

      【讨论】:

        猜你喜欢
        • 2021-02-05
        • 1970-01-01
        • 2023-03-09
        • 2020-05-31
        • 2021-02-13
        • 2020-12-04
        • 2021-10-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多