C
C#β€’3mo ago
Rhys

Discord.net help - Trying to access a users roles

Does anybody know a method I can use to access a users discord roles whilst I am logging into my application so I can either allow them into the app or disallow them? If you have any guides on how I could do this or some examples that would give some ideas on how I can do this as currently my method is not working. Here is how it looks.
c#
private async Task<bool> IsUserInServerWithRole(string discordUserId)
{
ulong requiredGuildId = 123456790;
ulong requiredRoleId = 123456790;

// Attempt to get the guild
var guild = _botService.Client.GetGuild(requiredGuildId);
if (guild == null)
{
_logger.LogWarning("Guild not found.");
return false;
}

var user = guild.GetUser(ulong.Parse(discordUserId));
if (user != null)
{
_logger.LogInformation($"User {user.Username} has {user.Roles.Count} roles.");
foreach (var role in user.Roles)
{
_logger.LogInformation($"Role: {role.Name}, ID: {role.Id}");
if (role.Id == requiredRoleId)
{
_logger.LogInformation($"Match found for required role ID: {role.Id}");
}
}
}
else
{
_logger.LogWarning("User not found in guild.");
}

bool hasRole = user.Roles.Any(role => role.Id == requiredRoleId);
return hasRole;
}
c#
private async Task<bool> IsUserInServerWithRole(string discordUserId)
{
ulong requiredGuildId = 123456790;
ulong requiredRoleId = 123456790;

// Attempt to get the guild
var guild = _botService.Client.GetGuild(requiredGuildId);
if (guild == null)
{
_logger.LogWarning("Guild not found.");
return false;
}

var user = guild.GetUser(ulong.Parse(discordUserId));
if (user != null)
{
_logger.LogInformation($"User {user.Username} has {user.Roles.Count} roles.");
foreach (var role in user.Roles)
{
_logger.LogInformation($"Role: {role.Name}, ID: {role.Id}");
if (role.Id == requiredRoleId)
{
_logger.LogInformation($"Match found for required role ID: {role.Id}");
}
}
}
else
{
_logger.LogWarning("User not found in guild.");
}

bool hasRole = user.Roles.Any(role => role.Id == requiredRoleId);
return hasRole;
}
Just looking for more ideas about this approach. My brain is starting to run out of them. Tia
58 Replies
Anu6is
Anu6isβ€’3mo ago
what exactly doesn't work? The code you show is a valid option for getting user roles
Rhys
Rhysβ€’3mo ago
Let me run it and grab the error. I'll just be a second
Rhys
Rhysβ€’3mo ago
Empty = "Enumeration yielded no results"
Empty = "Enumeration yielded no results"
This appears when I hit the break point here however it shows there is a count of 10 roles. This makes it return as false Its like it can't see the roles?
No description
Rhys
Rhysβ€’3mo ago
Thats the count. Its just not showing me what 10 roles there are.
No description
Anu6is
Anu6isβ€’3mo ago
πŸ˜• so what does your logging output? seeing as you loop through the roles collection before that
Rhys
Rhysβ€’3mo ago
No description
Rhys
Rhysβ€’3mo ago
Doesn't seem to get the role and Id
Anu6is
Anu6isβ€’3mo ago
uh...which part of those logs is related to this section of code?
Rhys
Rhysβ€’3mo ago
The 10 roles at the top it seems
Anu6is
Anu6isβ€’3mo ago
I don't see log in ur code snippet oh sorry, i see so if you debug (use the debugger) does it just skip the foreach
Rhys
Rhysβ€’3mo ago
Yeah its skipping this whole section here
No description
Anu6is
Anu6isβ€’3mo ago
πŸ˜•
Rhys
Rhysβ€’3mo ago
Yeah I am totally lost. I wonder if you can't do Oauth but I would be suprised if thats the case
Anu6is
Anu6isβ€’3mo ago
nothing in this snippet is related to OAuth though... you have a DiscordSocketClient assuming that's what _botService.Client returns... and that fact that you successfully get a guild and a user should mean the client is already logged in and ready
Rhys
Rhysβ€’3mo ago
Yeah its logged in and ready but if you don't have a specified role I want it to (at some point) send you to another page so it just sends the user to that But currently its not even capable of extracting the data from whatever the role info is stored in
Rhys
Rhysβ€’3mo ago
Got the socket setup here
No description
Anu6is
Anu6isβ€’3mo ago
what async work does this method actually do?
Rhys
Rhysβ€’3mo ago
Literally this
No description
Anu6is
Anu6isβ€’3mo ago
no, i mean the IsUserInServerWithRole why is it returning a Task<bool> is there other code that you left out? cause these no awaits in your snippit
Rhys
Rhysβ€’3mo ago
Just this
No description
Anu6is
Anu6isβ€’3mo ago
πŸ€” inside of the method, now how u call the method
private async Task<bool> IsUserInServerWithRole(string discordUserId)
{
ulong requiredGuildId = 123456790;
ulong requiredRoleId = 123456790;

// Attempt to get the guild
var guild = _botService.Client.GetGuild(requiredGuildId);
if (guild == null)
{
_logger.LogWarning("Guild not found.");
return false;
}

var user = guild.GetUser(ulong.Parse(discordUserId));
if (user != null)
{
_logger.LogInformation($"User {user.Username} has {user.Roles.Count} roles.");
foreach (var role in user.Roles)
{
_logger.LogInformation($"Role: {role.Name}, ID: {role.Id}");
if (role.Id == requiredRoleId)
{
_logger.LogInformation($"Match found for required role ID: {role.Id}");
}
}
}
else
{
_logger.LogWarning("User not found in guild.");
}

bool hasRole = user.Roles.Any(role => role.Id == requiredRoleId);
return hasRole;
}
private async Task<bool> IsUserInServerWithRole(string discordUserId)
{
ulong requiredGuildId = 123456790;
ulong requiredRoleId = 123456790;

// Attempt to get the guild
var guild = _botService.Client.GetGuild(requiredGuildId);
if (guild == null)
{
_logger.LogWarning("Guild not found.");
return false;
}

var user = guild.GetUser(ulong.Parse(discordUserId));
if (user != null)
{
_logger.LogInformation($"User {user.Username} has {user.Roles.Count} roles.");
foreach (var role in user.Roles)
{
_logger.LogInformation($"Role: {role.Name}, ID: {role.Id}");
if (role.Id == requiredRoleId)
{
_logger.LogInformation($"Match found for required role ID: {role.Id}");
}
}
}
else
{
_logger.LogWarning("User not found in guild.");
}

bool hasRole = user.Roles.Any(role => role.Id == requiredRoleId);
return hasRole;
}
^ in this
Rhys
Rhysβ€’3mo ago
its called in my HTTP Post So when I make a post to do the exchange its called within that
Anu6is
Anu6isβ€’3mo ago
I don't think u get what i'm asking
Rhys
Rhysβ€’3mo ago
I think I might be confused
Anu6is
Anu6isβ€’3mo ago
this method private async Task<bool> IsUserInServerWithRole has async Task<bool> yet it does not do any async work why is it not just private bool IsUserInServerWithRole
Rhys
Rhysβ€’3mo ago
Let me try it
Severity Code Description Project File Line Suppression State
Error CS1061 'bool' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?) 72 Active
Severity Code Description Project File Line Suppression State
Error CS1061 'bool' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?) 72 Active
Got an error about "Get Awaiter"?
Anu6is
Anu6isβ€’3mo ago
where?
Rhys
Rhysβ€’3mo ago
No description
Anu6is
Anu6isβ€’3mo ago
remove await
Rhys
Rhysβ€’3mo ago
Empty = "Enumeration yielded no results"
Empty = "Enumeration yielded no results"
still this issue :cry_cat: inside of my Roles Also skips the actual check for the role :/
Anu6is
Anu6isβ€’3mo ago
which check?
Rhys
Rhysβ€’3mo ago
No description
Rhys
Rhysβ€’3mo ago
This one^
Anu6is
Anu6isβ€’3mo ago
does it skip the foreach itself or just the if?
Rhys
Rhysβ€’3mo ago
Just the if. It goes to the user.Roles then the in then back out
Anu6is
Anu6isβ€’3mo ago
also are you actually using the debugger or just relying on the logging?
Rhys
Rhysβ€’3mo ago
I am using the debugger It shoes the count of roles so in this case = 21. But then it wont match the id with the ids so it knows ids are there just not able to access
Anu6is
Anu6isβ€’3mo ago
that doesn't make sense though... if you have a collection with items, it should simply iterate through that collection
Rhys
Rhysβ€’3mo ago
Thats what i thought but when I try to access the collection I get Empty = "Enumeration yielded no results"
Rettoph
Rettophβ€’3mo ago
maybe unnecessary, im unsure what intents are privileged and what arent, but have you verified whatever youre doing doesnt require opting in within the Discord developer dashboard? I dont think it makes sense since you are allegedly getting the 10 roles and can see them in the debugger, but might be worth playing around with? πŸ€·β€β™‚οΈ
No description
Rhys
Rhysβ€’3mo ago
I have all of them checked 😦
Rhys
Rhysβ€’3mo ago
Actually, not this one. Unsure fully what it does
No description
Anu6is
Anu6isβ€’3mo ago
if (user != null)
{
var roles = user.Roles.ToArray();
....
....
}
if (user != null)
{
var roles = user.Roles.ToArray();
....
....
}
if you add that before you logger line and put a break point on the logger. Does the array have anything?
Rhys
Rhysβ€’3mo ago
Empty = "Enumeration yielded no results" Still has the count thoug
Anu6is
Anu6isβ€’3mo ago
screenshot
Rhys
Rhysβ€’3mo ago
its not letting me, just comes off when I go to screenshot
Rhys
Rhysβ€’3mo ago
In visualiser I get this
No description
Anu6is
Anu6isβ€’3mo ago
yeah, idk... this is weird. As far as I can see the code is correct
Rhys
Rhysβ€’3mo ago
I've been stuck on and off for about 2 months I am about to give up and go to js at this point :CryingCool:
Rhys
Rhysβ€’3mo ago
No description
Rhys
Rhysβ€’3mo ago
There you go thats whats popping up
Anu6is
Anu6isβ€’3mo ago
not the user.Roles.... the array var roles = user.Roles.ToArray(); what does roles show
Rhys
Rhysβ€’3mo ago
No description
Anu6is
Anu6isβ€’3mo ago
πŸšΆβ€β™‚οΈ gg
Rhys
Rhysβ€’3mo ago
:cowboy_cool_cry_mild_panic: Yep, its really not looking good
Rettoph
Rettophβ€’3mo ago
do you have the code sitting anywhere public? I can try taking a quick stab at it locally
Rhys
Rhysβ€’3mo ago
I don't no currently its filled with personal info and stuff of that matter 😦 I would be happy to go into a call and go through what I have so you can see where I am and what its doing?
Rettoph
Rettophβ€’3mo ago
No promises, but sure.