HTTP 错误 403:当我尝试使用我的 discord 机器人获取新成员的个人资料图片时,在 urllib 上被禁止
HTTP Error 403: Forbidden on urllib when I'm trying to get profile pic of a new member with my discord bot
我正在编写 discord.py discord BOT。现在我正在写“on_member_join”,我正在尝试获取新会员个人资料图片以制作自定义欢迎图片。这就是问题所在。当我启动程序,并且有成员加入服务器时,请求已正确发送,但我收到 HTTP Error 403: Forbidden
错误。
我的代码:
import discord
from PIL import Image,ImageDraw,ImageFont
import urllib.request
@client.event
async def on_member_join(member):
url = f"{member.avatar_url}"
name = "profile.png"
urllib.request.urlretrieve(url, name)
请记住,我需要下载图像,因为我必须使用“枕头库”对其进行自定义
discord.Member.avatar_url
(I am linking to the User
equivalent as the docs redirect there anyway) returns an Asset that you can call await read()
上。这将为您提供头像的字节表示,您可以直接将其读入 Pillow Image
,如下所示:
from io import BytesIO
from PIL import Image
avatar_bytes = await member.avatar_url.read()
im = Image.open(BytesIO(avatar_bytes))
如果需要将原头像存盘,可以用save()
代替read()
我正在编写 discord.py discord BOT。现在我正在写“on_member_join”,我正在尝试获取新会员个人资料图片以制作自定义欢迎图片。这就是问题所在。当我启动程序,并且有成员加入服务器时,请求已正确发送,但我收到 HTTP Error 403: Forbidden
错误。
我的代码:
import discord
from PIL import Image,ImageDraw,ImageFont
import urllib.request
@client.event
async def on_member_join(member):
url = f"{member.avatar_url}"
name = "profile.png"
urllib.request.urlretrieve(url, name)
请记住,我需要下载图像,因为我必须使用“枕头库”对其进行自定义
discord.Member.avatar_url
(I am linking to the User
equivalent as the docs redirect there anyway) returns an Asset that you can call await read()
上。这将为您提供头像的字节表示,您可以直接将其读入 Pillow Image
,如下所示:
from io import BytesIO
from PIL import Image
avatar_bytes = await member.avatar_url.read()
im = Image.open(BytesIO(avatar_bytes))
如果需要将原头像存盘,可以用save()
代替read()