From 0553e42eca643544db9bb220839493610e633e50 Mon Sep 17 00:00:00 2001 From: Spectralitree Date: Fri, 27 Aug 2021 19:20:14 -0400 Subject: [PATCH] refactor: tweak -raw usage (#341) Co-authored-by: spiral --- .../CommandSystem/ContextArgumentsExt.cs | 3 + PluralKit.Bot/Commands/Groups.cs | 112 +++++----- PluralKit.Bot/Commands/MemberEdit.cs | 200 ++++++++++-------- PluralKit.Bot/Commands/SystemEdit.cs | 109 ++++++---- 4 files changed, 242 insertions(+), 182 deletions(-) diff --git a/PluralKit.Bot/CommandSystem/ContextArgumentsExt.cs b/PluralKit.Bot/CommandSystem/ContextArgumentsExt.cs index 480d2814..f7061a89 100644 --- a/PluralKit.Bot/CommandSystem/ContextArgumentsExt.cs +++ b/PluralKit.Bot/CommandSystem/ContextArgumentsExt.cs @@ -71,6 +71,9 @@ namespace PluralKit.Bot return matched; } + public static bool MatchRaw(this Context ctx) => + ctx.Match("r", "raw") || ctx.MatchFlag("r", "raw"); + public static (ulong? messageId, ulong? channelId) MatchMessage(this Context ctx, bool parseRawMessageId) { if (ctx.Message.Type == Message.MessageType.Reply && ctx.Message.MessageReference?.MessageId != null) diff --git a/PluralKit.Bot/Commands/Groups.cs b/PluralKit.Bot/Commands/Groups.cs index 95c634b6..7d8b7c4f 100644 --- a/PluralKit.Bot/Commands/Groups.cs +++ b/PluralKit.Bot/Commands/Groups.cs @@ -97,46 +97,51 @@ namespace PluralKit.Bot public async Task GroupDisplayName(Context ctx, PKGroup target) { + var noDisplayNameSetMessage = "This group does not have a display name set."; + if (ctx.System?.Id == target.System) + noDisplayNameSetMessage += $" To set one, type `pk;group {target.Reference()} displayname `."; + + // No perms check, display name isn't covered by member privacy + + if (!ctx.HasNext()) + { + if (target.DisplayName == null) + await ctx.Reply(noDisplayNameSetMessage); + else + { + var eb = new EmbedBuilder() + .Field(new("Name", target.Name)) + .Field(new("Display Name", target.DisplayName)); + + if (ctx.System?.Id == target.System) + eb.Description($"To change display name, type `pk;group {target.Reference()} displayname `." + + "To clear it, type `pk;group {target.Reference()} displayname -clear`." + + "To print the raw display name, type `pk;group {target.Reference()} displayname -raw`."); + + await ctx.Reply(embed: eb.Build()); + } + return; + } + if (ctx.MatchRaw()) + { + if (target.DisplayName == null) + await ctx.Reply(noDisplayNameSetMessage); + else + await ctx.Reply($"```\n{target.DisplayName}\n```"); + return; + } + + ctx.CheckOwnGroup(target); + if (await ctx.MatchClear("this group's display name")) { - ctx.CheckOwnGroup(target); - var patch = new GroupPatch { DisplayName = Partial.Null() }; await _db.Execute(conn => _repo.UpdateGroup(conn, target.Id, patch)); await ctx.Reply($"{Emojis.Success} Group display name cleared."); } - else if (!ctx.HasNext()) - { - // No perms check, display name isn't covered by member privacy - if (ctx.MatchFlag("r", "raw")) - { - if (target.DisplayName == null) - { - if (ctx.System?.Id == target.System) - await ctx.Reply($"This group does not have a display name set. To set one, type `pk;group {target.Reference()} displayname `."); - else - await ctx.Reply("This group does not have a display name set."); - } - else - await ctx.Reply($"```\n{target.DisplayName}\n```"); - } - else - { - var eb = new EmbedBuilder() - .Field(new("Name", target.Name)) - .Field(new("Display Name", target.DisplayName ?? "*(none)*")); - - if (ctx.System?.Id == target.System) - eb.Description($"To change display name, type `pk;group {target.Reference()} displayname `.\nTo clear it, type `pk;group {target.Reference()} displayname -clear`.\nTo print the raw display name, type `pk;group {target.Reference()} displayname -raw`."); - - await ctx.Reply(embed: eb.Build()); - } - } else { - ctx.CheckOwnGroup(target); - var newDisplayName = ctx.RemainderOrNull(); var patch = new GroupPatch { DisplayName = Partial.Present(newDisplayName) }; @@ -148,26 +153,17 @@ namespace PluralKit.Bot public async Task GroupDescription(Context ctx, PKGroup target) { - if (await ctx.MatchClear("this group's description")) - { - ctx.CheckOwnGroup(target); + if (!target.DescriptionPrivacy.CanAccess(ctx.LookupContextFor(target.System))) + throw Errors.LookupNotAllowed; - var patch = new GroupPatch { Description = Partial.Null() }; - await _db.Execute(conn => _repo.UpdateGroup(conn, target.Id, patch)); - await ctx.Reply($"{Emojis.Success} Group description cleared."); - } - else if (!ctx.HasNext()) - { - if (!target.DescriptionPrivacy.CanAccess(ctx.LookupContextFor(target.System))) - throw Errors.LookupNotAllowed; + var noDescriptionSetMessage = "This group does not have a description set."; + if (ctx.System?.Id == target.System) + noDescriptionSetMessage += $" To set one, type `pk;group {target.Reference()} description `."; + if (!ctx.HasNext()) + { if (target.Description == null) - if (ctx.System?.Id == target.System) - await ctx.Reply($"This group does not have a description set. To set one, type `pk;group {target.Reference()} description `."); - else - await ctx.Reply("This group does not have a description set."); - else if (ctx.MatchFlag("r", "raw")) - await ctx.Reply($"```\n{target.Description}\n```"); + await ctx.Reply(noDescriptionSetMessage); else await ctx.Reply(embed: new EmbedBuilder() .Title("Group description") @@ -175,11 +171,27 @@ namespace PluralKit.Bot .Field(new("\u200B", $"To print the description with formatting, type `pk;group {target.Reference()} description -raw`." + (ctx.System?.Id == target.System ? $" To clear it, type `pk;group {target.Reference()} description -clear`." : ""))) .Build()); + return; + } + if (ctx.MatchRaw()) + { + if (target.Description == null) + await ctx.Reply(noDescriptionSetMessage); + else + await ctx.Reply($"```\n{target.Description}\n```"); + return; + } + + ctx.CheckOwnGroup(target); + + if (await ctx.MatchClear("this group's description")) + { + var patch = new GroupPatch { Description = Partial.Null() }; + await _db.Execute(conn => _repo.UpdateGroup(conn, target.Id, patch)); + await ctx.Reply($"{Emojis.Success} Group description cleared."); } else { - ctx.CheckOwnGroup(target); - var description = ctx.RemainderOrNull().NormalizeLineEndSpacing(); if (description.IsLongerThan(Limits.MaxDescriptionLength)) throw Errors.DescriptionTooLongError(description.Length); diff --git a/PluralKit.Bot/Commands/MemberEdit.cs b/PluralKit.Bot/Commands/MemberEdit.cs index 8953d616..0caf5bb1 100644 --- a/PluralKit.Bot/Commands/MemberEdit.cs +++ b/PluralKit.Bot/Commands/MemberEdit.cs @@ -59,25 +59,17 @@ namespace PluralKit.Bot public async Task Description(Context ctx, PKMember target) { - if (await ctx.MatchClear("this member's description")) - { - ctx.CheckOwnMember(target); + var noDescriptionSetMessage = "This member does not have a description set."; + if (ctx.System?.Id == target.System) + noDescriptionSetMessage += $" To set one, type `pk;member {target.Reference()} description `."; - var patch = new MemberPatch { Description = Partial.Null() }; - await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch)); - await ctx.Reply($"{Emojis.Success} Member description cleared."); - } - else if (!ctx.HasNext()) + if (!target.DescriptionPrivacy.CanAccess(ctx.LookupContextFor(target.System))) + throw Errors.LookupNotAllowed; + + if (!ctx.HasNext()) { - if (!target.DescriptionPrivacy.CanAccess(ctx.LookupContextFor(target.System))) - throw Errors.LookupNotAllowed; if (target.Description == null) - if (ctx.System?.Id == target.System) - await ctx.Reply($"This member does not have a description set. To set one, type `pk;member {target.Reference()} description `."); - else - await ctx.Reply("This member does not have a description set."); - else if (ctx.MatchFlag("r", "raw")) - await ctx.Reply($"```\n{target.Description}\n```"); + await ctx.Reply(noDescriptionSetMessage); else await ctx.Reply(embed: new EmbedBuilder() .Title("Member description") @@ -85,11 +77,27 @@ namespace PluralKit.Bot .Field(new("\u200B", $"To print the description with formatting, type `pk;member {target.Reference()} description -raw`." + (ctx.System?.Id == target.System ? $" To clear it, type `pk;member {target.Reference()} description -clear`." : ""))) .Build()); + return; + } + else if (ctx.MatchRaw()) + { + if (target.Description == null) + await ctx.Reply(noDescriptionSetMessage); + else + await ctx.Reply($"```\n{target.Description}\n```"); + return; + } + + ctx.CheckOwnMember(target); + + if (await ctx.MatchClear("this member's description")) + { + var patch = new MemberPatch { Description = Partial.Null() }; + await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch)); + await ctx.Reply($"{Emojis.Success} Member description cleared."); } else { - ctx.CheckOwnMember(target); - var description = ctx.RemainderOrNull().NormalizeLineEndSpacing(); if (description.IsLongerThan(Limits.MaxDescriptionLength)) throw Errors.DescriptionTooLongError(description.Length); @@ -103,33 +111,41 @@ namespace PluralKit.Bot public async Task Pronouns(Context ctx, PKMember target) { + var noPronounsSetMessage = "This member does not have pronouns set."; + if (ctx.System?.Id == target.System) + noPronounsSetMessage += $"To set some, type `pk;member {target.Reference()} pronouns `."; + + if (!target.PronounPrivacy.CanAccess(ctx.LookupContextFor(target.System))) + throw Errors.LookupNotAllowed; + + if (!ctx.HasNext()) + { + if (target.Pronouns == null) + await ctx.Reply(noPronounsSetMessage); + else + await ctx.Reply($"**{target.NameFor(ctx)}**'s pronouns are **{target.Pronouns}**.\nTo print the pronouns with formatting, type `pk;member {target.Reference()} pronouns -raw`." + + (ctx.System?.Id == target.System ? $" To clear them, type `pk;member {target.Reference()} pronouns -clear`." : "")); + return; + } + else if (ctx.MatchRaw()) + { + if (target.Pronouns == null) + await ctx.Reply(noPronounsSetMessage); + else + await ctx.Reply($"```\n{target.Pronouns}\n```"); + return; + } + + ctx.CheckOwnMember(target); + if (await ctx.MatchClear("this member's pronouns")) { - ctx.CheckOwnMember(target); - var patch = new MemberPatch { Pronouns = Partial.Null() }; await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch)); await ctx.Reply($"{Emojis.Success} Member pronouns cleared."); } - else if (!ctx.HasNext()) - { - if (!target.PronounPrivacy.CanAccess(ctx.LookupContextFor(target.System))) - throw Errors.LookupNotAllowed; - if (target.Pronouns == null) - if (ctx.System?.Id == target.System) - await ctx.Reply($"This member does not have pronouns set. To set some, type `pk;member {target.Reference()} pronouns `."); - else - await ctx.Reply("This member does not have pronouns set."); - else if (ctx.MatchFlag("r", "raw")) - await ctx.Reply($"```\n{target.Pronouns}\n```"); - else - await ctx.Reply($"**{target.NameFor(ctx)}**'s pronouns are **{target.Pronouns}**.\nTo print the pronouns with formatting, type `pk;member {target.Reference()} pronouns -raw`." - + (ctx.System?.Id == target.System ? $" To clear them, type `pk;member {target.Reference()} pronouns -clear`." : "")); - } else { - ctx.CheckOwnMember(target); - var pronouns = ctx.RemainderOrNull().NormalizeLineEndSpacing(); if (pronouns.IsLongerThan(Limits.MaxPronounsLength)) throw Errors.MemberPronounsTooLongError(pronouns.Length); @@ -332,42 +348,42 @@ namespace PluralKit.Bot await ctx.Reply(successStr); } + var noDisplayNameSetMessage = "This member does not have a display name set."; + if (ctx.System?.Id == target.System) + noDisplayNameSetMessage += $" To set one, type `pk;member {target.Reference()} displayname `."; + + // No perms check, display name isn't covered by member privacy + + if (!ctx.HasNext()) + { + var eb = await CreateMemberNameInfoEmbed(ctx, target); + if (ctx.System?.Id == target.System) + eb.Description($"To change display name, type `pk;member {target.Reference()} displayname `." + + "To clear it, type `pk;member {target.Reference()} displayname -clear`." + + "To print the raw display name, type `pk;member {target.Reference()} displayname -raw`."); + await ctx.Reply(embed: eb.Build()); + return; + } + else if (ctx.MatchRaw()) + { + if (target.DisplayName == null) + await ctx.Reply(noDisplayNameSetMessage); + else + await ctx.Reply($"```\n{target.DisplayName}\n```"); + return; + } + + ctx.CheckOwnMember(target); + if (await ctx.MatchClear("this member's display name")) { - ctx.CheckOwnMember(target); - var patch = new MemberPatch { DisplayName = Partial.Null() }; await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch)); await PrintSuccess($"{Emojis.Success} Member display name cleared. This member will now be proxied using their member name \"{target.NameFor(ctx)}\"."); } - else if (!ctx.HasNext()) - { - // No perms check, display name isn't covered by member privacy - if (ctx.MatchFlag("r", "raw")) - { - if (target.DisplayName == null) - { - if (ctx.System?.Id == target.System) - await ctx.Reply($"This member does not have a display name set. To set one, type `pk;member {target.Reference()} displayname `."); - else - await ctx.Reply("This member does not have a display name set."); - } - else - await ctx.Reply($"```\n{target.DisplayName}\n```"); - } - else - { - var eb = await CreateMemberNameInfoEmbed(ctx, target); - if (ctx.System?.Id == target.System) - eb.Description($"To change display name, type `pk;member {target.Reference()} displayname `.\nTo clear it, type `pk;member {target.Reference()} displayname -clear`.\nTo print the raw display name, type `pk;member {target.Reference()} displayname -raw`."); - await ctx.Reply(embed: eb.Build()); - } - } else { - ctx.CheckOwnMember(target); - var newDisplayName = ctx.RemainderOrNull(); var patch = new MemberPatch { DisplayName = Partial.Present(newDisplayName) }; @@ -381,10 +397,35 @@ namespace PluralKit.Bot { ctx.CheckGuildContext(); + var noServerNameSetMessage = "This member does not have a server name set."; + if (ctx.System?.Id == target.System) + noServerNameSetMessage += $" To set one, type `pk;member {target.Reference()} servername `."; + + // No perms check, display name isn't covered by member privacy + + if (!ctx.HasNext()) + { + var eb = await CreateMemberNameInfoEmbed(ctx, target); + if (ctx.System?.Id == target.System) + eb.Description($"To change server name, type `pk;member {target.Reference()} servername `.\nTo clear it, type `pk;member {target.Reference()} servername -clear`.\nTo print the raw server name, type `pk;member {target.Reference()} servername -raw`."); + await ctx.Reply(embed: eb.Build()); + return; + } + else if (ctx.MatchRaw()) + { + MemberGuildSettings memberGuildConfig = await _db.Execute(c => _repo.GetMemberGuild(c, ctx.Guild.Id, target.Id)); + + if (memberGuildConfig.DisplayName == null) + await ctx.Reply(noServerNameSetMessage); + else + await ctx.Reply($"```\n{memberGuildConfig.DisplayName}\n```"); + return; + } + + ctx.CheckOwnMember(target); + if (await ctx.MatchClear("this member's server name")) { - ctx.CheckOwnMember(target); - var patch = new MemberGuildPatch { DisplayName = null }; await _db.Execute(conn => _repo.UpsertMemberGuild(conn, target.Id, ctx.Guild.Id, patch)); @@ -393,35 +434,8 @@ namespace PluralKit.Bot else await ctx.Reply($"{Emojis.Success} Member server name cleared. This member will now be proxied using their member name \"{target.NameFor(ctx)}\" in this server ({ctx.Guild.Name})."); } - else if (!ctx.HasNext()) - { - // No perms check, display name isn't covered by member privacy - if (ctx.MatchFlag("r", "raw")) - { - MemberGuildSettings memberGuildConfig = await _db.Execute(c => _repo.GetMemberGuild(c, ctx.Guild.Id, target.Id)); - - if (memberGuildConfig.DisplayName == null) - { - if (ctx.System?.Id == target.System) - await ctx.Reply($"This member does not have a server name set. To set one, type `pk;member {target.Reference()} servername `."); - else - await ctx.Reply("This member does not have a server name set."); - } - else - await ctx.Reply($"```\n{memberGuildConfig.DisplayName}\n```"); - } - else - { - var eb = await CreateMemberNameInfoEmbed(ctx, target); - if (ctx.System?.Id == target.System) - eb.Description($"To change server name, type `pk;member {target.Reference()} servername `.\nTo clear it, type `pk;member {target.Reference()} servername -clear`.\nTo print the raw server name, type `pk;member {target.Reference()} servername -raw`."); - await ctx.Reply(embed: eb.Build()); - } - } else { - ctx.CheckOwnMember(target); - var newServerName = ctx.RemainderOrNull(); var patch = new MemberGuildPatch { DisplayName = newServerName }; diff --git a/PluralKit.Bot/Commands/SystemEdit.cs b/PluralKit.Bot/Commands/SystemEdit.cs index 6e6633e4..fe576408 100644 --- a/PluralKit.Bot/Commands/SystemEdit.cs +++ b/PluralKit.Bot/Commands/SystemEdit.cs @@ -29,40 +29,75 @@ namespace PluralKit.Bot public async Task Name(Context ctx) { + var noNameSetMessage = "Your system does not have a name set. Type `pk;system name ` to set one."; + ctx.CheckSystem(); + if (!ctx.HasNext()) + { + if (ctx.System.Name != null) + await ctx.Reply($"Your system's name is currently **{ctx.System.Name}**. Type `pk;system name -clear` to clear it."); + else + await ctx.Reply(noNameSetMessage); + return; + } + if (ctx.MatchRaw()) + { + if (ctx.System.Name != null) + await ctx.Reply($"```\n{ctx.System.Name}\n```"); + else + await ctx.Reply(noNameSetMessage); + return; + } + if (await ctx.MatchClear("your system's name")) { var clearPatch = new SystemPatch { Name = null }; await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, clearPatch)); await ctx.Reply($"{Emojis.Success} System name cleared."); - return; } - - var newSystemName = ctx.RemainderOrNull(); - if (newSystemName == null) + else { - if (ctx.System.Name != null) - await ctx.Reply($"Your system's name is currently **{ctx.System.Name}**. Type `pk;system name -clear` to clear it."); - else - await ctx.Reply("Your system currently does not have a name. Type `pk;system name ` to set one."); - return; + var newSystemName = ctx.RemainderOrNull(); + + if (newSystemName.Length > Limits.MaxSystemNameLength) + throw Errors.SystemNameTooLongError(newSystemName.Length); + + var patch = new SystemPatch { Name = newSystemName }; + await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, patch)); + + await ctx.Reply($"{Emojis.Success} System name changed."); } - - if (newSystemName != null && newSystemName.Length > Limits.MaxSystemNameLength) - throw Errors.SystemNameTooLongError(newSystemName.Length); - - var patch = new SystemPatch { Name = newSystemName }; - await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, patch)); - - await ctx.Reply($"{Emojis.Success} System name changed."); } public async Task Description(Context ctx) { + var noDescriptionSetMessage = "Your system does not have a description set. To set one, type `pk;s description `."; + ctx.CheckSystem(); + if (!ctx.HasNext()) + { + if (ctx.System.Description == null) + await ctx.Reply(noDescriptionSetMessage); + else + await ctx.Reply(embed: new EmbedBuilder() + .Title("System description") + .Description(ctx.System.Description) + .Footer(new("To print the description with formatting, type `pk;s description -raw`. To clear it, type `pk;s description -clear`. To change it, type `pk;s description `.")) + .Build()); + return; + } + if (ctx.MatchRaw()) + { + if (ctx.System.Description == null) + await ctx.Reply(noDescriptionSetMessage); + else + await ctx.Reply($"```\n{ctx.System.Description}\n```"); + return; + } + if (await ctx.MatchClear("your system's description")) { var patch = new SystemPatch { Description = null }; @@ -71,23 +106,9 @@ namespace PluralKit.Bot await ctx.Reply($"{Emojis.Success} System description cleared."); return; } - - var newDescription = ctx.RemainderOrNull()?.NormalizeLineEndSpacing(); - if (newDescription == null) - { - if (ctx.System.Description == null) - await ctx.Reply("Your system does not have a description set. To set one, type `pk;s description `."); - else if (ctx.MatchFlag("r", "raw")) - await ctx.Reply($"```\n{ctx.System.Description}\n```"); - else - await ctx.Reply(embed: new EmbedBuilder() - .Title("System description") - .Description(ctx.System.Description) - .Footer(new("To print the description with formatting, type `pk;s description -raw`. To clear it, type `pk;s description -clear`. To change it, type `pk;s description `.")) - .Build()); - } else { + var newDescription = ctx.RemainderOrNull()?.NormalizeLineEndSpacing(); if (newDescription.Length > Limits.MaxDescriptionLength) throw Errors.DescriptionTooLongError(newDescription.Length); var patch = new SystemPatch { Description = newDescription }; @@ -141,8 +162,25 @@ namespace PluralKit.Bot public async Task Tag(Context ctx) { + var noTagSetMessage = "You currently have no system tag. To set one, type `pk;s tag `."; + ctx.CheckSystem(); + if (!ctx.HasNext(skipFlags: false)) + { + if (ctx.System.Tag == null) + await ctx.Reply(noTagSetMessage); + else + await ctx.Reply($"Your current system tag is {ctx.System.Tag.AsCode()}. To change it, type `pk;s tag `. To clear it, type `pk;s tag -clear`."); + } + if (ctx.MatchRaw()) + { + if (ctx.System.Tag == null) + await ctx.Reply(noTagSetMessage); + else + await ctx.Reply($"```\n{ctx.System.Tag}\n```"); + } + if (await ctx.MatchClear("your system's tag")) { var patch = new SystemPatch { Tag = null }; @@ -150,13 +188,6 @@ namespace PluralKit.Bot await ctx.Reply($"{Emojis.Success} System tag cleared."); } - else if (!ctx.HasNext(skipFlags: false)) - { - if (ctx.System.Tag == null) - await ctx.Reply($"You currently have no system tag. To set one, type `pk;s tag `."); - else - await ctx.Reply($"Your current system tag is {ctx.System.Tag.AsCode()}. To change it, type `pk;s tag `. To clear it, type `pk;s tag -clear`."); - } else { var newTag = ctx.RemainderOrNull(skipFlags: false);