如何获取使用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
相关产品推荐
相关产品推荐

