コード例 #1
0
        public async Task Privacy(Context ctx, PKMember target, PrivacyLevel?newValueFromCommand)
        {
            if (ctx.System == null)
            {
                throw Errors.NoSystemError;
            }
            if (target.System != ctx.System.Id)
            {
                throw Errors.NotOwnMemberError;
            }

            // Display privacy settings
            if (!ctx.HasNext() && newValueFromCommand == null)
            {
                await ctx.Reply(embed : CreatePrivacyEmbed(ctx, target));

                return;
            }

            // Get guild settings (mostly for warnings and such)
            MemberGuildSettings guildSettings = null;

            if (ctx.Guild != null)
            {
                guildSettings = await _db.Execute(c => c.QueryOrInsertMemberGuildConfig(ctx.Guild.Id, target.Id));
            }

            // Set Privacy Settings
            PrivacyLevel PopPrivacyLevel(string subjectName)
            {
                if (ctx.Match("public", "show", "shown", "visible"))
                {
                    return(PrivacyLevel.Public);
                }

                if (ctx.Match("private", "hide", "hidden"))
                {
                    return(PrivacyLevel.Private);
                }

                if (!ctx.HasNext())
                {
                    throw new PKSyntaxError($"You must pass a privacy level for `{subjectName}` (`public` or `private`)");
                }
                throw new PKSyntaxError($"Invalid privacy level `{ctx.PopArgument()}` (must be `public` or `private`).");
            }

            // See if we have a subject given
            PrivacyLevel newLevel;

            if (PrivacyUtils.TryParseMemberPrivacy(ctx.PeekArgument(), out var subject))
            {
                // We peeked before, pop it now
                ctx.PopArgument();

                // Read the privacy level from args
                newLevel = PopPrivacyLevel(subject.Name());

                // Set the level on the given subject
                target.SetPrivacy(subject, newLevel);
                await _data.SaveMember(target);

                // Print response
                var explanation = (subject, newLevel) switch
                {
                    (MemberPrivacySubject.Name, PrivacyLevel.Private) => "This member's name is now hidden from other systems, and will be replaced by the member's display name.",
                    (MemberPrivacySubject.Description, PrivacyLevel.Private) => "This member's description is now hidden from other systems.",
                    (MemberPrivacySubject.Avatar, PrivacyLevel.Private) => "This member's avatar is now hidden from other systems.",
                    (MemberPrivacySubject.Birthday, PrivacyLevel.Private) => "This member's birthday is now hidden from other systems.",
                    (MemberPrivacySubject.Pronouns, PrivacyLevel.Private) => "This member's pronouns are now hidden from other systems.",
                    (MemberPrivacySubject.Metadata, PrivacyLevel.Private) => "This member's metadata (eg. created timestamp, message count, etc) is now hidden from other systems.",
                    (MemberPrivacySubject.Visibility, PrivacyLevel.Private) => "This member is now hidden from member lists.",

                    (MemberPrivacySubject.Name, PrivacyLevel.Public) => "This member's name is no longer hidden from other systems.",
                    (MemberPrivacySubject.Description, PrivacyLevel.Public) => "This member's description is no longer hidden from other systems.",
                    (MemberPrivacySubject.Avatar, PrivacyLevel.Public) => "This member's avatar is no longer hidden from other systems.",
                    (MemberPrivacySubject.Birthday, PrivacyLevel.Public) => "This member's birthday is no longer hidden from other systems.",
                    (MemberPrivacySubject.Pronouns, PrivacyLevel.Public) => "This member's pronouns are no longer hidden other systems.",
                    (MemberPrivacySubject.Metadata, PrivacyLevel.Public) => "This member's metadata (eg. created timestamp, message count, etc) is no longer hidden from other systems.",
                    (MemberPrivacySubject.Visibility, PrivacyLevel.Public) => "This member is no longer hidden from member lists.",

                    _ => throw new InvalidOperationException($"Invalid subject/level tuple ({subject}, {newLevel})")
                };

                await ctx.Reply($"{Emojis.Success} {target.NameFor(ctx)}'s {subject.Name()} has been set to **{newLevel.LevelName()}**. {explanation}");
            }
            else if (ctx.Match("all") || newValueFromCommand != null)
            {
                newLevel = newValueFromCommand ?? PopPrivacyLevel("all");
                target.SetAllPrivacy(newLevel);
                await _data.SaveMember(target);

                if (newLevel == PrivacyLevel.Private)
                {
                    await ctx.Reply($"All {target.NameFor(ctx)}'s privacy settings have been set to **{newLevel.LevelName()}**. Other accounts will now see nothing on the member card.");
                }
                else
                {
                    await ctx.Reply($"All {target.NameFor(ctx)}'s privacy settings have been set to **{newLevel.LevelName()}**. Other accounts will now see everything on the member card.");
                }
            }
            else
            {
                var subjectList = "`name`, `description`, `avatar`, `birthday`, `pronouns`, `metadata`, `visibility`, or `all`";
                throw new PKSyntaxError($"Invalid privacy subject `{ctx.PopArgument()}` (must be {subjectList}).");
            }

            // Name privacy only works given a display name
            if (subject == MemberPrivacySubject.Name && newLevel == PrivacyLevel.Private && target.DisplayName == null)
            {
                await ctx.Reply($"{Emojis.Warn} This member does not have a display name set, and name privacy **will not take effect**.");
            }
            // Avatar privacy doesn't apply when proxying if no server avatar is set
            if (subject == MemberPrivacySubject.Avatar && newLevel == PrivacyLevel.Private &&
                guildSettings?.AvatarUrl == null)
            {
                await ctx.Reply($"{Emojis.Warn} This member does not have a server avatar set, so *proxying* will **still show the member avatar**. If you want to hide your avatar when proxying here, set a server avatar: `pk;member {target.Hid} serveravatar`");
            }
        }