如何让discord.py 从 url 发送图像?

Spa*_*.yg 1 python discord.py

我有一个来自互联网的图像,我想要将其发送到服务器上的频道,但我希望显示该图像不是作为文件发送。我也不想发送链接本身。

Rur*_*uri 5

您可以使用discord.py、io 和aiohttp 从url 上传文件。你可以这样做:

import aiohttp
import discord

channel = client.get_channel(123567890000000) # replace id
url = "your image url" # must be an image
async with aiohttp.ClientSession() as session: # creates session
    async with session.get(url) as resp: # gets image from url
        img = await resp.read() # reads image from response
        with io.BytesIO(img) as file: # converts to file-like object
            await channel.send(file=discord.File(file, "testimage.png"))
Run Code Online (Sandbox Code Playgroud)

这都是异步的。(如果您使用此命令,则可以将“channel”替换为 ctx)