コード例 #1
0
    public static JObject ToJson(this PKMember member, LookupContext ctx, bool needsLegacyProxyTags = false,
                                 string systemStr = null)
    {
        var includePrivacy = ctx == LookupContext.ByOwner;

        var o = new JObject();

        o.Add("id", member.Hid);

        o.Add("uuid", member.Uuid.ToString());
        if (systemStr != null)
        {
            o.Add("system", systemStr);
        }

        o.Add("name", member.NameFor(ctx));

        o.Add("display_name", member.NamePrivacy.CanAccess(ctx) ? member.DisplayName : null);
        // o.Add("color", member.ColorPrivacy.CanAccess(ctx) ? member.Color : null);
        o.Add("color", member.Color);
        o.Add("birthday", member.BirthdayFor(ctx)?.FormatExport());
        o.Add("pronouns", member.PronounsFor(ctx));
        o.Add("avatar_url", member.AvatarFor(ctx).TryGetCleanCdnUrl());
        o.Add("banner", member.DescriptionPrivacy.Get(ctx, member.BannerImage).TryGetCleanCdnUrl());
        o.Add("description", member.DescriptionFor(ctx));
        o.Add("created", member.CreatedFor(ctx)?.FormatExport());
        o.Add("keep_proxy", member.KeepProxy);

        var tagArray = new JArray();

        foreach (var tag in member.ProxyTags)
        {
            tagArray.Add(new JObject {
                { "prefix", tag.Prefix }, { "suffix", tag.Suffix }
            });
        }
        o.Add("proxy_tags", tagArray);

        if (includePrivacy)
        {
            var p = new JObject();

            p.Add("visibility", member.MemberVisibility.ToJsonString());
            p.Add("name_privacy", member.NamePrivacy.ToJsonString());
            p.Add("description_privacy", member.DescriptionPrivacy.ToJsonString());
            p.Add("birthday_privacy", member.BirthdayPrivacy.ToJsonString());
            p.Add("pronoun_privacy", member.PronounPrivacy.ToJsonString());
            p.Add("avatar_privacy", member.AvatarPrivacy.ToJsonString());
            p.Add("metadata_privacy", member.MetadataPrivacy.ToJsonString());

            o.Add("privacy", p);
        }
        else
        {
            o.Add("privacy", null);
        }

        return(o);
    }
コード例 #2
0
        public static JObject ToJson(this PKMember member, LookupContext ctx)
        {
            var includePrivacy = ctx == LookupContext.ByOwner;

            var o = new JObject();

            o.Add("id", member.Hid);
            o.Add("name", member.NameFor(ctx));
            // o.Add("color", member.ColorPrivacy.CanAccess(ctx) ? member.Color : null);
            o.Add("color", member.Color);
            o.Add("display_name", member.NamePrivacy.CanAccess(ctx) ? member.DisplayName : null);
            o.Add("birthday", member.BirthdayFor(ctx)?.FormatExport());
            o.Add("pronouns", member.PronounsFor(ctx));
            o.Add("avatar_url", member.AvatarFor(ctx));
            o.Add("description", member.DescriptionFor(ctx));

            var tagArray = new JArray();

            foreach (var tag in member.ProxyTags)
            {
                tagArray.Add(new JObject {
                    { "prefix", tag.Prefix }, { "suffix", tag.Suffix }
                });
            }
            o.Add("proxy_tags", tagArray);

            o.Add("keep_proxy", member.KeepProxy);

            o.Add("privacy", includePrivacy ? (member.MemberVisibility.LevelName()) : null);

            o.Add("visibility", includePrivacy ? (member.MemberVisibility.LevelName()) : null);
            o.Add("name_privacy", includePrivacy ? (member.NamePrivacy.LevelName()) : null);
            o.Add("description_privacy", includePrivacy ? (member.DescriptionPrivacy.LevelName()) : null);
            o.Add("birthday_privacy", includePrivacy ? (member.BirthdayPrivacy.LevelName()) : null);
            o.Add("pronoun_privacy", includePrivacy ? (member.PronounPrivacy.LevelName()) : null);
            o.Add("avatar_privacy", includePrivacy ? (member.AvatarPrivacy.LevelName()) : null);
            // o.Add("color_privacy", ctx == LookupContext.ByOwner ? (member.ColorPrivacy.LevelName()) : null);
            o.Add("metadata_privacy", includePrivacy ? (member.MetadataPrivacy.LevelName()) : null);

            o.Add("created", member.CreatedFor(ctx)?.FormatExport());

            if (member.ProxyTags.Count > 0)
            {
                // Legacy compatibility only, TODO: remove at some point
                o.Add("prefix", member.ProxyTags?.FirstOrDefault().Prefix);
                o.Add("suffix", member.ProxyTags?.FirstOrDefault().Suffix);
            }

            return(o);
        }
コード例 #3
0
        public async Task <DiscordEmbed> CreateMemberEmbed(PKSystem system, PKMember member, DiscordGuild guild, LookupContext ctx)
        {
            // string FormatTimestamp(Instant timestamp) => DateTimeFormats.ZonedDateTimeFormat.Format(timestamp.InZone(system.Zone));

            var name = member.NameFor(ctx);

            if (system.Name != null)
            {
                name = $"{name} ({system.Name})";
            }

            DiscordColor color;

            try
            {
                color = member.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
            }
            catch (ArgumentException)
            {
                // Bad API use can cause an invalid color string
                // TODO: fix that in the API
                // 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 conn.QueryOrInsertMemberGuildConfig(guild.Id, member.Id) : null;

            var guildDisplayName = guildSettings?.DisplayName;
            var avatar           = guildSettings?.AvatarUrl ?? member.AvatarFor(ctx);

            var groups = (await conn.QueryMemberGroups(member.Id)).Where(g => g.Visibility.CanAccess(ctx)).ToList();

            var eb = new DiscordEmbedBuilder()
                     // TODO: add URL of website when that's up
                     .WithAuthor(name, iconUrl: DiscordUtils.WorkaroundForUrlBug(avatar))
                     // .WithColor(member.ColorPrivacy.CanAccess(ctx) ? color : DiscordUtils.Gray)
                     .WithColor(color)
                     .WithFooter($"System ID: {system.Hid} | Member ID: {member.Hid} {(member.MetadataPrivacy.CanAccess(ctx) ? $"| Created on {member.Created.FormatZoned(system)}":"")}");

            var description = "";

            if (member.MemberVisibility == PrivacyLevel.Private)
            {
                description += "*(this member is hidden)*\n";
            }
            if (guildSettings?.AvatarUrl != null)
            {
                if (member.AvatarFor(ctx) != null)
                {
                    description += $"*(this member has a server-specific avatar set; [click here]({member.AvatarUrl}) to see the global avatar)*\n";
                }
                else
                {
                    description += "*(this member has a server-specific avatar set)*\n";
                }
            }
            if (description != "")
            {
                eb.WithDescription(description);
            }

            if (avatar != null)
            {
                eb.WithThumbnail(avatar);
            }

            if (!member.DisplayName.EmptyOrNull() && member.NamePrivacy.CanAccess(ctx))
            {
                eb.AddField("Display Name", member.DisplayName.Truncate(1024), true);
            }
            if (guild != null && guildDisplayName != null)
            {
                eb.AddField($"Server Nickname (for {guild.Name})", guildDisplayName.Truncate(1024), true);
            }
            if (member.BirthdayFor(ctx) != null)
            {
                eb.AddField("Birthdate", member.BirthdayString, true);
            }
            if (member.PronounsFor(ctx) is {} pronouns&& !string.IsNullOrWhiteSpace(pronouns))
            {
                eb.AddField("Pronouns", pronouns.Truncate(1024), true);
            }
            if (member.MessageCountFor(ctx) is {} count&& count > 0)
            {
                eb.AddField("Message Count", member.MessageCount.ToString(), true);
            }
            if (member.HasProxyTags)
            {
                eb.AddField("Proxy Tags", member.ProxyTagsString("\n").Truncate(1024), true);
            }
            // --- For when this gets added to the member object itself or however they get added
            // if (member.LastMessage != null && member.MetadataPrivacy.CanAccess(ctx)) eb.AddField("Last message:" FormatTimestamp(DiscordUtils.SnowflakeToInstant(m.LastMessage.Value)));
            // if (member.LastSwitchTime != null && m.MetadataPrivacy.CanAccess(ctx)) eb.AddField("Last switched in:", FormatTimestamp(member.LastSwitchTime.Value));
            // if (!member.Color.EmptyOrNull() && member.ColorPrivacy.CanAccess(ctx)) eb.AddField("Color", $"#{member.Color}", true);
            if (!member.Color.EmptyOrNull())
            {
                eb.AddField("Color", $"#{member.Color}", true);
            }

            if (groups.Count > 0)
            {
                // More than 5 groups show in "compact" format without ID
                var content = groups.Count > 5
                    ? string.Join(", ", groups.Select(g => g.DisplayName ?? g.Name))
                    : string.Join("\n", groups.Select(g => $"[`{g.Hid}`] **{g.DisplayName ?? g.Name}**"));
                eb.AddField($"Groups ({groups.Count})", content.Truncate(1000));
            }

            if (member.DescriptionFor(ctx) is {} desc)
            {
                eb.AddField("Description", member.Description.NormalizeLineEndSpacing(), false);
            }

            return(eb.Build());
        }
コード例 #4
0
    public async Task <Embed> CreateMemberEmbed(PKSystem system, PKMember member, Guild guild, LookupContext ctx, DateTimeZone zone)
    {
        // string FormatTimestamp(Instant timestamp) => DateTimeFormats.ZonedDateTimeFormat.Format(timestamp.InZone(system.Zone));

        var name = member.NameFor(ctx);

        if (system.Name != null)
        {
            name = $"{name} ({system.Name})";
        }

        uint color;

        try
        {
            color = member.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
        }
        catch (ArgumentException)
        {
            // Bad API use can cause an invalid color string
            // this is now fixed in the API, but might still have some remnants in the database
            // so we just default to a blank color, yolo
            color = DiscordUtils.Gray;
        }

        var guildSettings = guild != null ? await _repo.GetMemberGuild(guild.Id, member.Id) : null;

        var guildDisplayName = guildSettings?.DisplayName;
        var avatar           = guildSettings?.AvatarUrl ?? member.AvatarFor(ctx);

        var groups = await _repo.GetMemberGroups(member.Id)
                     .Where(g => g.Visibility.CanAccess(ctx))
                     .OrderBy(g => g.Name, StringComparer.InvariantCultureIgnoreCase)
                     .ToListAsync();

        var eb = new EmbedBuilder()
                 .Author(new Embed.EmbedAuthor(name, IconUrl: avatar.TryGetCleanCdnUrl(), Url: $"https://dash.pluralkit.me/profile/m/{member.Hid}"))
                 // .WithColor(member.ColorPrivacy.CanAccess(ctx) ? color : DiscordUtils.Gray)
                 .Color(color)
                 .Footer(new Embed.EmbedFooter(
                             $"System ID: {system.Hid} | Member ID: {member.Hid} {(member.MetadataPrivacy.CanAccess(ctx) ? $"| Created on {member.Created.FormatZoned(zone)}" : "")}"));

        if (member.DescriptionPrivacy.CanAccess(ctx))
        {
            eb.Image(new Embed.EmbedImage(member.BannerImage));
        }

        var description = "";

        if (member.MemberVisibility == PrivacyLevel.Private)
        {
            description += "*(this member is hidden)*\n";
        }
        if (guildSettings?.AvatarUrl != null)
        {
            if (member.AvatarFor(ctx) != null)
            {
                description +=
                    $"*(this member has a server-specific avatar set; [click here]({member.AvatarUrl.TryGetCleanCdnUrl()}) to see the global avatar)*\n";
            }
            else
            {
                description += "*(this member has a server-specific avatar set)*\n";
            }
        }
        if (description != "")
        {
            eb.Description(description);
        }

        if (avatar != null)
        {
            eb.Thumbnail(new Embed.EmbedThumbnail(avatar.TryGetCleanCdnUrl()));
        }

        if (!member.DisplayName.EmptyOrNull() && member.NamePrivacy.CanAccess(ctx))
        {
            eb.Field(new Embed.Field("Display Name", member.DisplayName.Truncate(1024), true));
        }
        if (guild != null && guildDisplayName != null)
        {
            eb.Field(new Embed.Field($"Server Nickname (for {guild.Name})", guildDisplayName.Truncate(1024), true));
        }
        if (member.BirthdayFor(ctx) != null)
        {
            eb.Field(new Embed.Field("Birthdate", member.BirthdayString, true));
        }
        if (member.PronounsFor(ctx) is { } pronouns&& !string.IsNullOrWhiteSpace(pronouns))
        {
            eb.Field(new Embed.Field("Pronouns", pronouns.Truncate(1024), true));
        }
        if (member.MessageCountFor(ctx) is { } count&& count > 0)
        {
            eb.Field(new Embed.Field("Message Count", member.MessageCount.ToString(), true));
        }
        if (member.HasProxyTags)
        {
            eb.Field(new Embed.Field("Proxy Tags", member.ProxyTagsString("\n").Truncate(1024), true));
        }
        // --- For when this gets added to the member object itself or however they get added
        // if (member.LastMessage != null && member.MetadataPrivacy.CanAccess(ctx)) eb.AddField("Last message:" FormatTimestamp(DiscordUtils.SnowflakeToInstant(m.LastMessage.Value)));
        // if (member.LastSwitchTime != null && m.MetadataPrivacy.CanAccess(ctx)) eb.AddField("Last switched in:", FormatTimestamp(member.LastSwitchTime.Value));
        // if (!member.Color.EmptyOrNull() && member.ColorPrivacy.CanAccess(ctx)) eb.AddField("Color", $"#{member.Color}", true);
        if (!member.Color.EmptyOrNull())
        {
            eb.Field(new Embed.Field("Color", $"#{member.Color}", true));
        }

        if (groups.Count > 0)
        {
            // More than 5 groups show in "compact" format without ID
            var content = groups.Count > 5
                ? string.Join(", ", groups.Select(g => g.DisplayName ?? g.Name))
                : string.Join("\n", groups.Select(g => $"[`{g.Hid}`] **{g.DisplayName ?? g.Name}**"));
            eb.Field(new Embed.Field($"Groups ({groups.Count})", content.Truncate(1000)));
        }

        if (member.DescriptionFor(ctx) is { } desc)
        {
            eb.Field(new Embed.Field("Description", member.Description.NormalizeLineEndSpacing()));
        }

        return(eb.Build());
    }
コード例 #5
0
    public static JObject ToJson(this PKMember member, LookupContext ctx, bool needsLegacyProxyTags = false,
                                 string systemStr = null, APIVersion v = APIVersion.V1)
    {
        var includePrivacy = ctx == LookupContext.ByOwner;

        var o = new JObject();

        o.Add("id", member.Hid);

        if (v == APIVersion.V2)
        {
            o.Add("uuid", member.Uuid.ToString());
            if (systemStr != null)
            {
                o.Add("system", systemStr);
            }
        }

        o.Add("name", member.NameFor(ctx));

        // o.Add("color", member.ColorPrivacy.CanAccess(ctx) ? member.Color : null);
        o.Add("display_name", member.NamePrivacy.CanAccess(ctx) ? member.DisplayName : null);
        o.Add("color", member.Color);
        o.Add("birthday", member.BirthdayFor(ctx)?.FormatExport());
        o.Add("pronouns", member.PronounsFor(ctx));
        o.Add("avatar_url", member.AvatarFor(ctx).TryGetCleanCdnUrl());
        o.Add("banner", member.DescriptionPrivacy.Get(ctx, member.BannerImage).TryGetCleanCdnUrl());
        o.Add("description", member.DescriptionFor(ctx));
        o.Add("created", member.CreatedFor(ctx)?.FormatExport());
        o.Add("keep_proxy", member.KeepProxy);

        var tagArray = new JArray();

        foreach (var tag in member.ProxyTags)
        {
            tagArray.Add(new JObject {
                { "prefix", tag.Prefix }, { "suffix", tag.Suffix }
            });
        }
        o.Add("proxy_tags", tagArray);

        switch (v)
        {
        case APIVersion.V1:
        {
            o.Add("privacy", includePrivacy ? member.MemberVisibility.LevelName() : null);

            o.Add("visibility", includePrivacy ? member.MemberVisibility.LevelName() : null);
            o.Add("name_privacy", includePrivacy ? member.NamePrivacy.LevelName() : null);
            o.Add("description_privacy", includePrivacy ? member.DescriptionPrivacy.LevelName() : null);
            o.Add("birthday_privacy", includePrivacy ? member.BirthdayPrivacy.LevelName() : null);
            o.Add("pronoun_privacy", includePrivacy ? member.PronounPrivacy.LevelName() : null);
            o.Add("avatar_privacy", includePrivacy ? member.AvatarPrivacy.LevelName() : null);
            // o.Add("color_privacy", ctx == LookupContext.ByOwner ? (member.ColorPrivacy.LevelName()) : null);
            o.Add("metadata_privacy", includePrivacy ? member.MetadataPrivacy.LevelName() : null);

            if (member.ProxyTags.Count > 0 && needsLegacyProxyTags)
            {
                // Legacy compatibility only, TODO: remove at some point
                o.Add("prefix", member.ProxyTags?.FirstOrDefault().Prefix);
                o.Add("suffix", member.ProxyTags?.FirstOrDefault().Suffix);
            }

            break;
        }

        case APIVersion.V2:
        {
            if (includePrivacy)
            {
                var p = new JObject();

                p.Add("visibility", member.MemberVisibility.ToJsonString());
                p.Add("name_privacy", member.NamePrivacy.ToJsonString());
                p.Add("description_privacy", member.DescriptionPrivacy.ToJsonString());
                p.Add("birthday_privacy", member.BirthdayPrivacy.ToJsonString());
                p.Add("pronoun_privacy", member.PronounPrivacy.ToJsonString());
                p.Add("avatar_privacy", member.AvatarPrivacy.ToJsonString());
                p.Add("metadata_privacy", member.MetadataPrivacy.ToJsonString());

                o.Add("privacy", p);
            }
            else
            {
                o.Add("privacy", null);
            }

            break;
        }
        }

        return(o);
    }