fix: correctly handle missing role cache in message embed

This commit is contained in:
spiral 2021-11-02 22:36:14 -04:00
parent 08c5b78cc2
commit 21e3e61db0
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
2 changed files with 10 additions and 3 deletions

View File

@ -40,7 +40,7 @@ namespace Myriad.Extensions
public static Role GetRole(this IDiscordCache cache, ulong roleId) public static Role GetRole(this IDiscordCache cache, ulong roleId)
{ {
if (!cache.TryGetRole(roleId, out var role)) if (!cache.TryGetRole(roleId, out var role))
throw new KeyNotFoundException($"User {roleId} not found in cache"); throw new KeyNotFoundException($"Role {roleId} not found in cache");
return role; return role;
} }

View File

@ -331,9 +331,16 @@ namespace PluralKit.Bot
var roles = memberInfo?.Roles?.ToList(); var roles = memberInfo?.Roles?.ToList();
if (roles != null && roles.Count > 0) if (roles != null && roles.Count > 0)
{ {
// TODO: what if role isn't in cache? figure out a fallback
var rolesString = string.Join(", ", roles var rolesString = string.Join(", ", roles
.Select(id => _cache.GetRole(id)) .Select(id => {
_cache.TryGetRole(id, out var role);
if (role != null)
return role;
return new Role() {
Name = "*(unknown role)*",
Position = 0,
};
})
.OrderByDescending(role => role.Position) .OrderByDescending(role => role.Position)
.Select(role => role.Name)); .Select(role => role.Name));
eb.Field(new($"Account roles ({roles.Count})", rolesString.Truncate(1024))); eb.Field(new($"Account roles ({roles.Count})", rolesString.Truncate(1024)));