我有一个连接到
MySQL database
的 Discord.py 机器人。
我想根据用户的 URL
从数据库中检索用户的横幅 Discord user ID
。
检索 URL 后,我使用 urllib.request.urlretrieve
下载图像并将其保存到 local file
。
尝试下载图像时,我收到 urllib.error.HTTPError 或 urllib.error.URLError 以及 403(禁止)错误。
@commands.slash_command(name="profile", description="Fetch user data")
async def profile(self, interaction: discord.Interaction, user: discord.User):
cursor.execute("SELECT banner_url FROM user_ui WHERE user_id = %s", (user.id,))
result = cursor.fetchone()
now = datetime.datetime.now()
if result:
print(result)
url = result[0]
full_path = "assets/banner.jpg"
urllib.request.urlretrieve(url, full_path)
banner = "assets/banner.jpg"
0.09 13:49:59 [Bot] discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: HTTPError: HTTP Error 403: Forbidden
我想了解为什么在尝试下载图像时收到 403 错误以及如何解决该错误。如果我的代码片段需要任何改进或更正,请提供指导。
我玩了一下,找到了解决方案,先保存它,然后再存储到数据库中,然后在再次使用 ut 时匹配名称