【问题标题】:Get roles of current user in Discord server获取 Discord 服务器中当前用户的角色
【发布时间】:2020-11-23 20:14:04
【问题描述】:

我正在尝试使用 Discord 的 API(使用 access_token)获取特定聊天室中当前用户的角色。我已经通过文档进行了公平的查看,但看不到如何做到这一点。例如,向https://discordapp.com/api/users/@me 发出获取请求会按预期提供基本用户信息。然后我尝试了以下内容,但没有任何运气:

https://discordapp.com/api/users/@me/guilds/${guild.id}/roles

这是我正在使用的代码的 sn-p:

               ....get access token then

                .then(info => {
                    console.log(info)
                    fetch(`https://discordapp.com/api/users/@me`, {
                        headers: {
                            authorization: `${info.token_type} ${info.access_token}`,
                        },
                    }).then(response => response.json())
                        .then(info => {
                            console.log(info)
                        })
                })

有什么想法吗?

为了澄清,用户使用 discord 登录,我的应用程序会收到一个用户访问令牌,然后我试图用它来获取用户在特定 discord 房间中的角色。

【问题讨论】:

    标签: javascript api discord


    【解决方案1】:

    从documentation,您可以使用端点执行此操作:

    GET /guilds/{guild.id}/members/{user.id}

    这将返回一个包含该用户角色的公会成员对象。

    公会成员对象示例:

    {
      "user": {},
      "nick": "",
      "roles": [],
      "joined_at": "",
      "deaf": false,
      "mute": false
    }
    

    【讨论】:

    • 嗨,谢谢,我试过了,但得到了{ message: '401: Unauthorized', code: 0 }。当用户使用 Discord 登录时,我启用了 guild 范围
    • 文档说明 here 要访问某些东西,包括公会成员,“您必须首先转到开发人员门户中的应用程序并启用您希望使用的特权意图的切换。”也许这是你的问题。
    • 不像 OP 要求的那样与 access_token 一起工作。 GET /guilds/{guild.id}/members/{user.id} 不是 oauth2 的有效范围。
    猜你喜欢
    • 2020-08-09
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 2020-11-16
    • 2021-06-23
    • 2016-04-20
    • 2018-06-16
    相关资源
    最近更新 更多