'Discord OAuth2 getting user roles from exact guild
I am using discord OAuth2 for chrome extension authentication. I get user access_token and would like to use it to get roles of actual user on my server. I tried to use https://discordapp.com/api/v6/users/@me/ or https://discordapp.com/api/v6/users/@me/guilds but it only gives me information about user servers. Can I get information about roles of this user also? How can I do it?
Solution 1:[1]
You can access the role ids through the /users/@me/guilds/{guild.id}/member API with the guilds.members.read scope and OAuth2 but don't think you can get the actual role object. I thought I could call /guilds/{guild.id}/roles to get a list of the roles and resolve the id but this thread suggests that its only available to bots and not OAuth2.
EDIT:
In my scrambling to figure out how to resolve role_ids I found that you can use the shortcut <@&[role_id]> in a Discord Message entry form to figure out the name of the role the role_id is actually referring to.
Solution 2:[2]
To get information about guilds that the user is on and the roles that user is on, you might need to look into Discord's documentation. I'm not 100% sure if you can get information about user's roles through OAuth2 but this is what I found.
guilds scope returns partial guild info
allows /users/@me/guilds to return basic information about all of a user's guilds
Like said on the document (quote on above), it returns basic information, this might not include user's roles, but only all guild roles, and not the roles user has.
{
"id": "80351110224678912",
"name": "1337 Krew",
"icon": "8342729096ea3675442027381ff50dfe",
"owner": true,
"permissions": "36953089",
"features": ["COMMUNITY", "NEWS"]
}
Not sure if the guilds scope return more info than that like full guild structure
I would suggest echoing/logging down information that you get from that scope.
Solution 3:[3]
Not sure if this will help at all but this link has the official discord documentation on how to get a list of users in a guild, search for a user, or get more info on a specific user, via oath2 and requesting info from the endpoints. Official Discord (get-guild-member)
Of note personally I am using a GET request to /guilds/{guild.id}/members to get a list of users within a guild the account is a part of.
I believe that this then either returns the role within the server along with the list or if this is not included, you are able to take the id and either search for the user with that id, or request more information from /guilds/{guild.id}/members/{user.id} to get as much information as discord has available on a certain user within the guild.
Also of note, I had the same issue with the 401: unauthorized error. The solution to this is making sure you have the correct members, and guilds intents listed in your discord application.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | NotTrixxie |
| Solution 3 |
