You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取使用discord.File发送的图片URL?

获取Discord发送图片的URL(使用discord.File)

当你用ctx.channel.send()发送discord.File后,返回的Message对象里包含了发送的附件信息,直接从附件中提取URL即可:

修正后的代码

import aiohttp
import io
import discord
from discord.ext import commands

@bot.command()
async def testing(ctx, user: discord.Member):
    async with aiohttp.ClientSession() as session:
        endpoint = str(user.avatar_url)
        async with session.get(endpoint) as end:
            pfp = await end.read()
            with io.BytesIO(pfp) as file:
                # 发送文件并保存返回的消息对象
                msg = await ctx.channel.send(file=discord.File(file, filename=f"pfp.png")) 
                # 获取第一个附件的URL
                image_url = msg.attachments[0].url
                print(image_url)  # 可打印或发送给用户

关键说明

  • msg.attachments是对应消息所有附件的Attachment对象列表
  • 由于仅发送了一张图片,直接取列表第一个元素msg.attachments[0]
  • 附件对象的url属性就是该图片的永久可访问链接

内容的提问来源于stack exchange,提问作者YazoR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 07:05:12