From f47d366c8df7a8359c60de1e100521404f23f467 Mon Sep 17 00:00:00 2001 From: Ske Date: Sat, 18 Jul 2020 13:26:36 +0200 Subject: [PATCH] Show member groups on member card --- PluralKit.Bot/Services/EmbedService.cs | 11 +++++++++-- PluralKit.Core/Models/ModelQueryExt.cs | 7 ++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index b027cbfd..50de24e2 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -102,12 +102,16 @@ namespace PluralKit.Bot { // for now we just default to a blank color, yolo color = DiscordUtils.Gray; } + + await using var conn = await _db.Obtain(); - var guildSettings = guild != null ? await _db.Execute(c => c.QueryOrInsertMemberGuildConfig(guild.Id, member.Id)) : null; + var guildSettings = guild != null ? await conn.QueryOrInsertMemberGuildConfig(guild.Id, member.Id) : null; var guildDisplayName = guildSettings?.DisplayName; var avatar = guildSettings?.AvatarUrl ?? member.AvatarFor(ctx); - var proxyTagsStr = string.Join('\n', member.ProxyTags.Select(t => $"`` {t.ProxyString}``")); + var groups = (await conn.QueryMemberGroups(member.Id)).ToList(); + + var proxyTagsStr = string.Join('\n', member.ProxyTags.Select(t => $"`` {t.ProxyString} ``")); var eb = new DiscordEmbedBuilder() // TODO: add URL of website when that's up @@ -125,6 +129,9 @@ namespace PluralKit.Bot { description += "*(this member has a server-specific avatar set)*\n"; if (description != "") eb.WithDescription(description); + if (groups.Count > 0) + eb.AddField($"Groups ({groups.Count})", string.Join("\n", groups.Select(g => $"[`{g.Hid}`] **{g.Name}**")).Truncate(1000)); + if (avatar != null) eb.WithThumbnail(avatar); if (!member.DisplayName.EmptyOrNull() && member.NamePrivacy.CanAccess(ctx)) eb.AddField("Display Name", member.DisplayName.Truncate(1024), true); diff --git a/PluralKit.Core/Models/ModelQueryExt.cs b/PluralKit.Core/Models/ModelQueryExt.cs index 9b751759..8eaa3da2 100644 --- a/PluralKit.Core/Models/ModelQueryExt.cs +++ b/PluralKit.Core/Models/ModelQueryExt.cs @@ -43,13 +43,18 @@ namespace PluralKit.Core { var query = new StringBuilder("select count(*) from group_members"); if (privacyFilter != null) - query.Append(" left join members on group_members.member_id = members.id"); + query.Append(" inner join members on group_members.member_id = members.id"); query.Append(" where group_members.group_id = @Id"); if (privacyFilter != null) query.Append(" and members.member_visibility = @PrivacyFilter"); return conn.QuerySingleOrDefaultAsync(query.ToString(), new {Id = id, PrivacyFilter = privacyFilter}); } + public static Task> QueryMemberGroups(this IPKConnection conn, MemberId id) => + conn.QueryAsync( + "select groups.* from group_members inner join groups on group_members.group_id = groups.id where group_members.member_id = @Id", + new {Id = id}); + public static Task QueryOrInsertGuildConfig(this IPKConnection conn, ulong guild) => conn.QueryFirstAsync("insert into servers (id) values (@guild) on conflict (id) do update set id = @guild returning *", new {guild});