Get roles of current user in Discord server

Viewed 4187

I'm trying to get the roles of the current user in a specific chat room using Discord's API (using an access_token). I've had a fair look through the docs and can't see how to do this. For example, doing a get request to https://discordapp.com/api/users/@me gives the basic user info as expected. I then tried something along the lines of the following without any luck:

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

Here's a snippet of the code I'm using:

               ....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)
                        })
                })

Any ideas?

To clarify, the user logs in with discord and my application receives a user access token which I'm then trying to use to get the roles of the user in a specific discord room.

1 Answers

From the documentation you can do this using the endpoint:

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

This will return a guild member object that contains the roles of this user.

Example guild member object:

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