From 6395872f2d91bdca6e61e2bb9ed7aafd0a9917a3 Mon Sep 17 00:00:00 2001 From: Mikaela Szekely Date: Mon, 5 Apr 2021 22:25:13 -0600 Subject: [PATCH] allow pk;system proxy to take a server ID So people can disable proxying in a server without outing themselves :) --- .../ContextEntityArgumentsExt.cs | 20 ++++++++++++++- PluralKit.Bot/Commands/CommandTree.cs | 2 +- PluralKit.Bot/Commands/SystemEdit.cs | 25 +++++++++++++------ docs/content/command-list.md | 2 +- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/PluralKit.Bot/CommandSystem/ContextEntityArgumentsExt.cs b/PluralKit.Bot/CommandSystem/ContextEntityArgumentsExt.cs index 00acf4b7..a5995f89 100644 --- a/PluralKit.Bot/CommandSystem/ContextEntityArgumentsExt.cs +++ b/PluralKit.Bot/CommandSystem/ContextEntityArgumentsExt.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Myriad.Extensions; using Myriad.Types; @@ -168,5 +169,22 @@ namespace PluralKit.Bot ctx.PopArgument(); return channel; } + + public static Guild MatchGuild(this Context ctx) + { + try + { + var id = ulong.Parse(ctx.PeekArgument()); + ctx.Cache.TryGetGuild(id, out var guild); + if (guild != null) + ctx.PopArgument(); + + return guild; + } + catch (FormatException) + { + return null; + } + } } } \ No newline at end of file diff --git a/PluralKit.Bot/Commands/CommandTree.cs b/PluralKit.Bot/Commands/CommandTree.cs index 717a5667..8c3ade92 100644 --- a/PluralKit.Bot/Commands/CommandTree.cs +++ b/PluralKit.Bot/Commands/CommandTree.cs @@ -18,7 +18,7 @@ namespace PluralKit.Bot public static Command SystemAvatar = new Command("system icon", "system icon [url|@mention]", "Changes your system's icon"); public static Command SystemDelete = new Command("system delete", "system delete", "Deletes your system"); public static Command SystemTimezone = new Command("system timezone", "system timezone [timezone]", "Changes your system's time zone"); - public static Command SystemProxy = new Command("system proxy", "system proxy [on|off]", "Enables or disables message proxying in a specific server"); + public static Command SystemProxy = new Command("system proxy", "system proxy [server id] [on|off]", "Enables or disables message proxying in a specific server"); public static Command SystemList = new Command("system list", "system [system] list [full]", "Lists a system's members"); public static Command SystemFind = new Command("system find", "system [system] find [full] ", "Searches a system's members given a search term"); public static Command SystemFronter = new Command("system fronter", "system [system] fronter", "Shows a system's fronter(s)"); diff --git a/PluralKit.Bot/Commands/SystemEdit.cs b/PluralKit.Bot/Commands/SystemEdit.cs index 068fe838..1e1988ec 100644 --- a/PluralKit.Bot/Commands/SystemEdit.cs +++ b/PluralKit.Bot/Commands/SystemEdit.cs @@ -4,6 +4,7 @@ using System.Text.RegularExpressions; using System.Threading.Tasks; using Myriad.Builders; +using Myriad.Types; using NodaTime; using NodaTime.Text; @@ -232,8 +233,18 @@ namespace PluralKit.Bot public async Task SystemProxy(Context ctx) { - ctx.CheckSystem().CheckGuildContext(); - var gs = await _db.Execute(c => _repo.GetSystemGuild(c, ctx.Guild.Id, ctx.System.Id)); + ctx.CheckSystem(); + + var guild = ctx.MatchGuild() ?? ctx.Guild ?? + throw new PKError("You must run this command in a server or pass a server ID."); + + var gs = await _db.Execute(c => _repo.GetSystemGuild(c, guild.Id, ctx.System.Id)); + + string serverText; + if (guild.Id == ctx.Guild?.Id) + serverText = $"this server ({guild.Name.EscapeMarkdown()})"; + else + serverText = $"the server {guild.Name.EscapeMarkdown()}"; bool newValue; if (ctx.Match("on", "enabled", "true", "yes")) newValue = true; @@ -242,19 +253,19 @@ namespace PluralKit.Bot else { if (gs.ProxyEnabled) - await ctx.Reply("Proxying in this server is currently **enabled** for your system. To disable it, type `pk;system proxy off`."); + await ctx.Reply($"Proxying in {serverText} is currently **enabled** for your system. To disable it, type `pk;system proxy off`."); else - await ctx.Reply("Proxying in this server is currently **disabled** for your system. To enable it, type `pk;system proxy on`."); + await ctx.Reply($"Proxying in {serverText} is currently **disabled** for your system. To enable it, type `pk;system proxy on`."); return; } var patch = new SystemGuildPatch {ProxyEnabled = newValue}; - await _db.Execute(conn => _repo.UpsertSystemGuild(conn, ctx.System.Id, ctx.Guild.Id, patch)); + await _db.Execute(conn => _repo.UpsertSystemGuild(conn, ctx.System.Id, guild.Id, patch)); if (newValue) - await ctx.Reply($"Message proxying in this server ({ctx.Guild.Name.EscapeMarkdown()}) is now **enabled** for your system."); + await ctx.Reply($"Message proxying in {serverText} is now **enabled** for your system."); else - await ctx.Reply($"Message proxying in this server ({ctx.Guild.Name.EscapeMarkdown()}) is now **disabled** for your system."); + await ctx.Reply($"Message proxying in {serverText} is now **disabled** for your system."); } public async Task SystemTimezone(Context ctx) diff --git a/docs/content/command-list.md b/docs/content/command-list.md index 93ba23b9..66b34675 100644 --- a/docs/content/command-list.md +++ b/docs/content/command-list.md @@ -21,7 +21,7 @@ Words in **\** or **[square brackets]** mean fill-in-the-blank. - `pk;system privacy ` - Changes your systems privacy settings. - `pk;system tag [tag]` - Changes the system tag of your system. - `pk;system timezone [location]` - Changes the time zone of your system. -- `pk;system proxy [on|off]` - Toggles message proxying for a specific server. +- `pk;system proxy [server id] [on|off]` - Toggles message proxying for a specific server. - `pk;system delete` - Deletes your system. - `pk;system [system] fronter` - Shows the current fronter of a system. - `pk;system [system] fronthistory` - Shows the last 10 fronters of a system.