Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get roles of current user in Discord server

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.

like image 610
HaveAGitGat Avatar asked Jan 26 '26 23:01

HaveAGitGat


1 Answers

There is

GET /users/@me/guilds/{guild.id}/member

Which will get you a JSON object containing

{
  "avatar": null,
  "communication_disabled_until": null,
  "flags": 0,
  "is_pending": null,
  "joined_at": "2023-01-11T23:12:34.423000+00:00",
  "nick": null,
  "pending": null,
  "premium_since": null,
  "roles": [
    2888141278217623600,
    5904405471246944000
  ],
  "user": {
    "id": 8285334657500223000,
    "username": "your-name",
    "display_name": null,
    "avatar": null,
    "avatar_decoration": null,
    "discriminator": 4041,
    "public_flags": 0
  },
  "mute": null,
  "deaf": null
}

To use this with OAuth2, you must request the guilds.members.read OAuth2 scope, which is the one that Discord prompts users for their permissions with

Read your member info (nickname, avatar, roles, etc...) for servers you belong to

This was added in sometime in late 2021, judging from this PR.

like image 91
Nick Guenther Avatar answered Jan 28 '26 13:01

Nick Guenther



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!