implement autoproxy commands

This commit is contained in:
dusk 2025-10-01 14:44:56 +00:00
parent c42385f01c
commit 2b304457cc
No known key found for this signature in database
4 changed files with 66 additions and 34 deletions

View file

@ -231,6 +231,11 @@ public partial class CommandTree
Commands.GroupFronterPercent(var param, var flags) => ctx.Execute<SystemFront>(GroupFrontPercent, g => g.FrontPercent(ctx, null, flags.duration, flags.fronters_only, flags.flat, param.target)),
Commands.TokenDisplay => ctx.Execute<Api>(TokenGet, m => m.GetToken(ctx)),
Commands.TokenRefresh => ctx.Execute<Api>(TokenRefresh, m => m.RefreshToken(ctx)),
Commands.AutoproxyShow => ctx.Execute<Autoproxy>(AutoproxySet, m => m.SetAutoproxyMode(ctx, null)),
Commands.AutoproxyOff => ctx.Execute<Autoproxy>(AutoproxySet, m => m.SetAutoproxyMode(ctx, new Autoproxy.Mode.Off())),
Commands.AutoproxyLatch => ctx.Execute<Autoproxy>(AutoproxySet, m => m.SetAutoproxyMode(ctx, new Autoproxy.Mode.Latch())),
Commands.AutoproxyFront => ctx.Execute<Autoproxy>(AutoproxySet, m => m.SetAutoproxyMode(ctx, new Autoproxy.Mode.Front())),
Commands.AutoproxyMember(var param, _) => ctx.Execute<Autoproxy>(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<Autoproxy>(AutoproxySet, m => m.SetAutoproxyMode(ctx));
}
private Task HandleConfigCommand(Context ctx)
{
if (ctx.System == null)

View file

@ -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 <member>` 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<Embed> CreateAutoproxyStatusEmbed(Context ctx, AutoproxySettings settings)
{
var commandList = $"**{ctx.DefaultPrefix}autoproxy latch** - Autoproxies as last-proxied member"

View file

@ -1 +1,21 @@
use super::*;
pub fn autoproxy() -> (&'static str, [&'static str; 2]) {
("autoproxy", ["ap", "auto"])
}
pub fn cmds() -> impl Iterator<Item = Command> {
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()
}

View file

@ -32,6 +32,7 @@ pub fn all() -> impl Iterator<Item = Command> {
.chain(switch::cmds())
.chain(random::cmds())
.chain(api::cmds())
.chain(autoproxy::cmds())
.map(|cmd| {
cmd.hidden_flag(("plaintext", ["pt"]))
.hidden_flag(("raw", ["r"]))