如何使用 Discord,py 的用户 ID 获取特定用户的头像

dun*_*ter 2 python discord discord.py

我曾经discord.py制作过一个机器人,将用户 ID 存储在数据库中以识别他们,但我不知道如何仅通过使用特定用户的id. 我四处搜索并发现了类似的东西 -Client.get_user()但它对我不起作用,因为我无法从文档中理解它的工作原理。有什么方法可以从数据库中获取用户的 id 字符串并将其传递给函数以获取该用户的头像并在嵌入中使用它吗?

我在 StackOverflow 上发现了另一个像我一样的问题,但该问题的解决方案也不适合我。

Gio*_*dze 7

根据文章,您获取用户id并请求用户的头像数据。看起来像string。以下是请求的 JSON 响应示例:

{
  "id": "80351110224678912",
  "username": "Nelly",
  "discriminator": "1337",
  "avatar": "8342729096ea3675442027381ff50dfe",
  "verified": true,
  "email": "nelly@discord.com",
  "flags": 64,
  "premium_type": 1,
  "public_flags": 64
}

Run Code Online (Sandbox Code Playgroud)

现在当你得到这个: 时"avatar": "8342729096ea3675442027381ff50dfe",你就知道头像数据是8342729096ea3675442027381ff50dfe。之后,您将使用图像的 Image Base Url:https://cdn.discordapp.com/
要请求图像,您必须选择格式(jpg、gif、png 等)。所以你的最终请求应该是:

https://cdn.discordapp.com/avatars/{user_id}/{user_avatar}.png
Run Code Online (Sandbox Code Playgroud)

其中user_iduser_avatar是要更改的变量。例如:

https://cdn.discordapp.com/avatars/80351110224678912/8342729096ea3675442027381ff50dfe.png
Run Code Online (Sandbox Code Playgroud)