Skip to content

Commit

Permalink
feat: trygetmember (sync async)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Aug 12, 2024
1 parent 0836d3c commit 08184f7
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions DisCatSharp/Entities/Guild/DiscordGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,41 @@ public async Task<DiscordMember> GetMemberAsync(ulong userId, bool fetch = false
return mbr;
}

/// <summary>
/// Gets a member of this guild by their user ID.
/// </summary>
/// <param name="userId">ID of the member to get.</param>
/// <param name="member">The member object.</param>
/// <param name="fetch">Whether to fetch the member from the api prior to cache.</param>
/// <returns><see langword="true"/> if the member was found, <paramref name="member"/> will be a <see cref="DiscordMember"/> entity, otherwise <see langword="false"/> and <see langword="null"/>.</returns>
/// <exception cref="ServerErrorException">Thrown when Discord is unable to process the request.</exception>
public bool TryGetMemberAsync(ulong userId, out DiscordMember? member, bool fetch = false)
{
if (!fetch && this.MembersInternal != null && this.MembersInternal.TryGetValue(userId, out var mbr))
{
member = mbr;
return true;
}

try
{
member = this.Discord.ApiClient.GetGuildMemberAsync(this.Id, userId).Result;
}
catch (NotFoundException)
{
member = null;
return false;
}

var intents = this.Discord.Configuration.Intents;

if (intents.HasIntent(DiscordIntents.GuildMembers))
if (this.MembersInternal != null)
this.MembersInternal[userId] = member;

return true;
}

/// <summary>
/// Retrieves a full list of members from Discord. This method will bypass cache.
/// </summary>
Expand Down

0 comments on commit 08184f7

Please sign in to comment.