diff --git a/PluralKit.Bot/CommandMeta/CommandTree.cs b/PluralKit.Bot/CommandMeta/CommandTree.cs index 51448a3c..1749aaec 100644 --- a/PluralKit.Bot/CommandMeta/CommandTree.cs +++ b/PluralKit.Bot/CommandMeta/CommandTree.cs @@ -231,6 +231,11 @@ public partial class CommandTree Commands.GroupFronterPercent(var param, var flags) => ctx.Execute(GroupFrontPercent, g => g.FrontPercent(ctx, null, flags.duration, flags.fronters_only, flags.flat, param.target)), Commands.TokenDisplay => ctx.Execute(TokenGet, m => m.GetToken(ctx)), Commands.TokenRefresh => ctx.Execute(TokenRefresh, m => m.RefreshToken(ctx)), + Commands.AutoproxyShow => ctx.Execute(AutoproxySet, m => m.SetAutoproxyMode(ctx, null)), + Commands.AutoproxyOff => ctx.Execute(AutoproxySet, m => m.SetAutoproxyMode(ctx, new Autoproxy.Mode.Off())), + Commands.AutoproxyLatch => ctx.Execute(AutoproxySet, m => m.SetAutoproxyMode(ctx, new Autoproxy.Mode.Latch())), + Commands.AutoproxyFront => ctx.Execute(AutoproxySet, m => m.SetAutoproxyMode(ctx, new Autoproxy.Mode.Front())), + Commands.AutoproxyMember(var param, _) => ctx.Execute(AutoproxySet, m => m.SetAutoproxyMode(ctx, new Autoproxy.Mode.Member(param.target))), _ => // this should only ever occur when deving if commands are not implemented... ctx.Reply( @@ -238,8 +243,6 @@ public partial class CommandTree }; if (ctx.Match("commands", "cmd", "c")) return CommandHelpRoot(ctx); - if (ctx.Match("ap", "autoproxy", "auto")) - return HandleAutoproxyCommand(ctx); if (ctx.Match("config", "cfg", "configure")) return HandleConfigCommand(ctx); if (ctx.Match("serverconfig", "guildconfig", "scfg")) @@ -451,17 +454,6 @@ public partial class CommandTree } } - private Task HandleAutoproxyCommand(Context ctx) - { - // ctx.CheckSystem(); - // oops, that breaks stuff! PKErrors before ctx.Execute don't actually do anything. - // so we just emulate checking and throwing an error. - if (ctx.System == null) - return ctx.Reply($"{Emojis.Error} {Errors.NoSystemError(ctx.DefaultPrefix).Message}"); - - return ctx.Execute(AutoproxySet, m => m.SetAutoproxyMode(ctx)); - } - private Task HandleConfigCommand(Context ctx) { if (ctx.System == null) diff --git a/PluralKit.Bot/Commands/Autoproxy.cs b/PluralKit.Bot/Commands/Autoproxy.cs index ddff335b..97d6ddd2 100644 --- a/PluralKit.Bot/Commands/Autoproxy.cs +++ b/PluralKit.Bot/Commands/Autoproxy.cs @@ -11,37 +11,51 @@ public class Autoproxy { private readonly IClock _clock; + public abstract record Mode() + { + public record Off() : Mode; + public record Latch() : Mode; + public record Front() : Mode; + public record Member(PKMember member) : Mode; + } + public Autoproxy(IClock clock) { _clock = clock; } - public async Task SetAutoproxyMode(Context ctx) + public async Task SetAutoproxyMode(Context ctx, Mode? mode = null) { - // no need to check account here, it's already done at CommandTree - ctx.CheckGuildContext(); + ctx.CheckSystem().CheckGuildContext(); // for now, just for guild // this also creates settings if there are none present var settings = await ctx.Repository.GetAutoproxySettings(ctx.System.Id, ctx.Guild.Id, null); - if (ctx.Match("off", "stop", "cancel", "no", "disable", "remove")) - await AutoproxyOff(ctx, settings); - else if (ctx.Match("latch", "last", "proxy", "stick", "sticky", "l")) - await AutoproxyLatch(ctx, settings); - else if (ctx.Match("front", "fronter", "switch", "f")) - await AutoproxyFront(ctx, settings); - else if (ctx.Match("member")) - throw new PKSyntaxError($"Member-mode autoproxy must target a specific member. Use the `{ctx.DefaultPrefix}autoproxy ` command, where `member` is the name or ID of a member in your system."); - else if (await ctx.MatchMember() is PKMember member) - await AutoproxyMember(ctx, member); - else if (!ctx.HasNext()) - await ctx.Reply(embed: await CreateAutoproxyStatusEmbed(ctx, settings)); - else - throw new PKSyntaxError($"Invalid autoproxy mode {ctx.PopArgument().AsCode()}."); + if (mode == null) + { + await AutoproxyShow(ctx, settings); + return; + } + + switch (mode) + { + case Mode.Off: + await AutoproxyOff(ctx, settings); + break; + case Mode.Latch: + await AutoproxyLatch(ctx, settings); + break; + case Mode.Front: + await AutoproxyFront(ctx, settings); + break; + case Mode.Member(var member): + await AutoproxyMember(ctx, member); + break; + } } - private async Task AutoproxyOff(Context ctx, AutoproxySettings settings) + public async Task AutoproxyOff(Context ctx, AutoproxySettings settings) { if (settings.AutoproxyMode == AutoproxyMode.Off) { @@ -54,7 +68,7 @@ public class Autoproxy } } - private async Task AutoproxyLatch(Context ctx, AutoproxySettings settings) + public async Task AutoproxyLatch(Context ctx, AutoproxySettings settings) { if (settings.AutoproxyMode == AutoproxyMode.Latch) { @@ -67,7 +81,7 @@ public class Autoproxy } } - private async Task AutoproxyFront(Context ctx, AutoproxySettings settings) + public async Task AutoproxyFront(Context ctx, AutoproxySettings settings) { if (settings.AutoproxyMode == AutoproxyMode.Front) { @@ -80,7 +94,7 @@ public class Autoproxy } } - private async Task AutoproxyMember(Context ctx, PKMember member) + public async Task AutoproxyMember(Context ctx, PKMember member) { ctx.CheckOwnMember(member); @@ -90,6 +104,11 @@ public class Autoproxy await ctx.Reply($"{Emojis.Success} Autoproxy set to **{member.NameFor(ctx)}** in this server."); } + public async Task AutoproxyShow(Context ctx, AutoproxySettings settings) + { + await ctx.Reply(embed: await CreateAutoproxyStatusEmbed(ctx, settings)); + } + private async Task CreateAutoproxyStatusEmbed(Context ctx, AutoproxySettings settings) { var commandList = $"**{ctx.DefaultPrefix}autoproxy latch** - Autoproxies as last-proxied member" diff --git a/crates/command_definitions/src/autoproxy.rs b/crates/command_definitions/src/autoproxy.rs index 8b137891..68a1925b 100644 --- a/crates/command_definitions/src/autoproxy.rs +++ b/crates/command_definitions/src/autoproxy.rs @@ -1 +1,21 @@ +use super::*; +pub fn autoproxy() -> (&'static str, [&'static str; 2]) { + ("autoproxy", ["ap", "auto"]) +} + +pub fn cmds() -> impl Iterator { + let ap = autoproxy(); + + [ + command!(ap => "autoproxy_show").help("Shows your current autoproxy settings"), + command!(ap, ("off", ["stop", "cancel", "no", "disable", "remove"]) => "autoproxy_off") + .help("Disables autoproxy"), + command!(ap, ("latch", ["last", "proxy", "stick", "sticky", "l"]) => "autoproxy_latch") + .help("Sets autoproxy to latch mode"), + command!(ap, ("front", ["fronter", "switch", "f"]) => "autoproxy_front") + .help("Sets autoproxy to front mode"), + command!(ap, MemberRef => "autoproxy_member").help("Sets autoproxy to a specific member"), + ] + .into_iter() +} diff --git a/crates/command_definitions/src/lib.rs b/crates/command_definitions/src/lib.rs index df197cbd..e79558e4 100644 --- a/crates/command_definitions/src/lib.rs +++ b/crates/command_definitions/src/lib.rs @@ -32,6 +32,7 @@ pub fn all() -> impl Iterator { .chain(switch::cmds()) .chain(random::cmds()) .chain(api::cmds()) + .chain(autoproxy::cmds()) .map(|cmd| { cmd.hidden_flag(("plaintext", ["pt"])) .hidden_flag(("raw", ["r"]))