From 5fc91d895cb231e4d033542223dce428d9ce26b7 Mon Sep 17 00:00:00 2001 From: Ske Date: Mon, 13 May 2019 22:56:22 +0200 Subject: [PATCH] bot: add proxy tag edit command --- PluralKit.Bot/Commands/MemberCommands.cs | 30 +++++++++++++++++++++++- PluralKit.Bot/Errors.cs | 2 ++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/PluralKit.Bot/Commands/MemberCommands.cs b/PluralKit.Bot/Commands/MemberCommands.cs index bf511d1f..aead0f82 100644 --- a/PluralKit.Bot/Commands/MemberCommands.cs +++ b/PluralKit.Bot/Commands/MemberCommands.cs @@ -138,6 +138,35 @@ namespace PluralKit.Bot.Commands await Context.Channel.SendMessageAsync($"{Emojis.Success} Member birthdate {(date == null ? "cleared" : $"changed to {ContextEntity.BirthdayString}")}."); } + [Command("proxy")] + [Alias("proxy", "tags", "proxytags", "brackets")] + [Remarks("member proxy ")] + [MustPassOwnMember] + public async Task MemberProxy([Remainder] string exampleProxy = null) + { + // Handling the clear case in an if here to keep the body dedented + if (exampleProxy == null) + { + // Just reset and send OK message + ContextEntity.Prefix = null; + ContextEntity.Suffix = null; + await Members.Save(ContextEntity); + await Context.Channel.SendMessageAsync($"{Emojis.Success} Member proxy tags cleared."); + return; + } + + // Make sure there's one and only one instance of "text" in the example proxy given + var prefixAndSuffix = exampleProxy.Split("text"); + if (prefixAndSuffix.Length < 2) throw Errors.ProxyMustHaveText; + if (prefixAndSuffix.Length > 2) throw Errors.ProxyMultipleText; + + // If the prefix/suffix is empty, use "null" instead (for DB) + ContextEntity.Prefix = prefixAndSuffix[0].Length > 0 ? prefixAndSuffix[0] : null; + ContextEntity.Suffix = prefixAndSuffix[1].Length > 0 ? prefixAndSuffix[1] : null; + await Members.Save(ContextEntity); + await Context.Channel.SendMessageAsync($"{Emojis.Success} Member proxy tags changed to `{ContextEntity.ProxyString}`. Try proxying now!"); + } + [Command] [Alias("view", "show", "info")] [Remarks("member")] @@ -146,7 +175,6 @@ namespace PluralKit.Bot.Commands var system = await Systems.GetById(member.System); await Context.Channel.SendMessageAsync(embed: await Embeds.CreateMemberEmbed(system, member)); } - public override async Task ReadContextParameterAsync(string value) { var res = await new PKMemberTypeReader().ReadAsync(Context, value, _services); diff --git a/PluralKit.Bot/Errors.cs b/PluralKit.Bot/Errors.cs index 45b4e0df..4653fdf3 100644 --- a/PluralKit.Bot/Errors.cs +++ b/PluralKit.Bot/Errors.cs @@ -16,5 +16,7 @@ namespace PluralKit.Bot { public static PKError InvalidColorError(string color) => new PKError($"\"{color}\" is not a valid color. Color must be in hex format (eg. #ff0000)."); public static PKError BirthdayParseError(string birthday) => new PKError($"\"{birthday}\" could not be parsed as a valid date. Try a format like \"2016-12-24\" or \"May 3 1996\"."); + public static PKError ProxyMustHaveText => new PKSyntaxError("Example proxy message must contain the string 'text'."); + public static PKError ProxyMultipleText => new PKSyntaxError("Example proxy message must contain the string 'text' exactly once."); } } \ No newline at end of file