mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-04 13:06:50 +00:00
feat: implement switch commands
This commit is contained in:
parent
15191171f5
commit
10dd499835
9 changed files with 85 additions and 45 deletions
|
|
@ -152,6 +152,13 @@ public partial class CommandTree
|
|||
Commands.SystemShowPrivacy(var param, _) => ctx.Execute<SystemEdit>(SystemPrivacy, m => m.ShowSystemPrivacy(ctx, ctx.System)),
|
||||
Commands.SystemChangePrivacyAll(var param, _) => ctx.Execute<SystemEdit>(SystemPrivacy, m => m.ChangeSystemPrivacyAll(ctx, ctx.System, param.level)),
|
||||
Commands.SystemChangePrivacy(var param, _) => ctx.Execute<SystemEdit>(SystemPrivacy, m => m.ChangeSystemPrivacy(ctx, ctx.System, param.privacy, param.level)),
|
||||
Commands.SwitchOut(_, _) => ctx.Execute<Switch>(SwitchOut, m => m.SwitchOut(ctx)),
|
||||
Commands.SwitchDo(var param, _) => ctx.Execute<Switch>(Switch, m => m.SwitchDo(ctx, param.targets)),
|
||||
Commands.SwitchMove(var param, _) => ctx.Execute<Switch>(SwitchMove, m => m.SwitchMove(ctx, param.@string)),
|
||||
Commands.SwitchEdit(var param, var flags) => ctx.Execute<Switch>(SwitchEdit, m => m.SwitchEdit(ctx, param.targets, false, flags.first, flags.remove, flags.append, flags.prepend)),
|
||||
Commands.SwitchEditOut(_, _) => ctx.Execute<Switch>(SwitchEditOut, m => m.SwitchEditOut(ctx)),
|
||||
Commands.SwitchDelete(var param, var flags) => ctx.Execute<Switch>(SwitchDelete, m => m.SwitchDelete(ctx, flags.all)),
|
||||
Commands.SwitchCopy(var param, var flags) => ctx.Execute<Switch>(SwitchCopy, m => m.SwitchEdit(ctx, param.targets, true, flags.first, flags.remove, flags.append, flags.prepend)),
|
||||
_ =>
|
||||
// this should only ever occur when deving if commands are not implemented...
|
||||
ctx.Reply(
|
||||
|
|
@ -521,26 +528,8 @@ public partial class CommandTree
|
|||
|
||||
private async Task HandleSwitchCommand(Context ctx)
|
||||
{
|
||||
if (ctx.Match("out"))
|
||||
await ctx.Execute<Switch>(SwitchOut, m => m.SwitchOut(ctx));
|
||||
else if (ctx.Match("move", "m", "shift", "offset"))
|
||||
await ctx.Execute<Switch>(SwitchMove, m => m.SwitchMove(ctx));
|
||||
else if (ctx.Match("edit", "e", "replace"))
|
||||
if (ctx.Match("out"))
|
||||
await ctx.Execute<Switch>(SwitchEditOut, m => m.SwitchEditOut(ctx));
|
||||
else
|
||||
await ctx.Execute<Switch>(SwitchEdit, m => m.SwitchEdit(ctx));
|
||||
else if (ctx.Match("delete", "remove", "erase", "cancel", "yeet"))
|
||||
await ctx.Execute<Switch>(SwitchDelete, m => m.SwitchDelete(ctx));
|
||||
else if (ctx.Match("copy", "add", "duplicate", "dupe"))
|
||||
await ctx.Execute<Switch>(SwitchCopy, m => m.SwitchEdit(ctx, true));
|
||||
else if (ctx.Match("commands", "help"))
|
||||
await PrintCommandList(ctx, "switching", SwitchCommands);
|
||||
else if (ctx.HasNext()) // there are following arguments
|
||||
await ctx.Execute<Switch>(Switch, m => m.SwitchDo(ctx));
|
||||
else
|
||||
await PrintCommandNotFoundError(ctx, Switch, SwitchOut, SwitchMove, SwitchEdit, SwitchEditOut,
|
||||
SwitchDelete, SwitchCopy, SystemFronter, SystemFrontHistory);
|
||||
await PrintCommandNotFoundError(ctx, Switch, SwitchOut, SwitchMove, SwitchEdit, SwitchEditOut,
|
||||
SwitchDelete, SwitchCopy, SystemFronter, SystemFrontHistory);
|
||||
}
|
||||
|
||||
private async Task CommandHelpRoot(Context ctx)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
using Humanizer;
|
||||
using Myriad.Types;
|
||||
using PluralKit.Core;
|
||||
using uniffi.commands;
|
||||
|
|
@ -8,6 +9,7 @@ namespace PluralKit.Bot;
|
|||
public abstract record Parameter()
|
||||
{
|
||||
public record MemberRef(PKMember member): Parameter;
|
||||
public record MemberRefs(List<PKMember> members): Parameter;
|
||||
public record SystemRef(PKSystem system): Parameter;
|
||||
public record GuildRef(Guild guild): Parameter;
|
||||
public record MemberPrivacyTarget(MemberPrivacySubject target): Parameter;
|
||||
|
|
@ -56,14 +58,21 @@ public class Parameters
|
|||
|
||||
private async Task<Parameter?> ResolveFfiParam(Context ctx, uniffi.commands.Parameter ffi_param)
|
||||
{
|
||||
var byId = HasFlag("id", "by-id");
|
||||
switch (ffi_param)
|
||||
{
|
||||
case uniffi.commands.Parameter.MemberRef memberRef:
|
||||
var byId = HasFlag("id", "by-id");
|
||||
return new Parameter.MemberRef(
|
||||
await ctx.ParseMember(memberRef.member, byId)
|
||||
?? throw new PKError(ctx.CreateNotFoundError("Member", memberRef.member, byId))
|
||||
);
|
||||
case uniffi.commands.Parameter.MemberRefs memberRefs:
|
||||
return new Parameter.MemberRefs(
|
||||
await memberRefs.members.ToAsyncEnumerable().SelectAwait(async m =>
|
||||
await ctx.ParseMember(m, byId)
|
||||
?? throw new PKError(ctx.CreateNotFoundError("Member", m, byId))
|
||||
).ToListAsync()
|
||||
);
|
||||
case uniffi.commands.Parameter.SystemRef systemRef:
|
||||
// todo: do we need byId here?
|
||||
return new Parameter.SystemRef(
|
||||
|
|
|
|||
|
|
@ -8,11 +8,10 @@ namespace PluralKit.Bot;
|
|||
public class Switch
|
||||
{
|
||||
|
||||
public async Task SwitchDo(Context ctx)
|
||||
public async Task SwitchDo(Context ctx, ICollection<PKMember> members)
|
||||
{
|
||||
ctx.CheckSystem();
|
||||
|
||||
var members = await ctx.ParseMemberList(ctx.System.Id);
|
||||
await DoSwitchCommand(ctx, members);
|
||||
}
|
||||
|
||||
|
|
@ -21,7 +20,7 @@ public class Switch
|
|||
ctx.CheckSystem();
|
||||
|
||||
// Switch with no members = switch-out
|
||||
await DoSwitchCommand(ctx, new PKMember[] { });
|
||||
await DoSwitchCommand(ctx, []);
|
||||
}
|
||||
|
||||
private async Task DoSwitchCommand(Context ctx, ICollection<PKMember> members)
|
||||
|
|
@ -57,12 +56,10 @@ public class Switch
|
|||
$"{Emojis.Success} Switch registered. Current fronters are now {string.Join(", ", members.Select(m => m.NameFor(ctx)))}.");
|
||||
}
|
||||
|
||||
public async Task SwitchMove(Context ctx)
|
||||
public async Task SwitchMove(Context ctx, string timeToMove)
|
||||
{
|
||||
ctx.CheckSystem();
|
||||
|
||||
var timeToMove = ctx.RemainderOrNull() ??
|
||||
throw new PKSyntaxError("Must pass a date or time to move the switch to.");
|
||||
var tz = TzdbDateTimeZoneSource.Default.ForId(ctx.Config?.UiTz ?? "UTC");
|
||||
|
||||
var result = DateUtils.ParseDateTime(timeToMove, true, tz);
|
||||
|
|
@ -104,31 +101,29 @@ public class Switch
|
|||
await ctx.Reply($"{Emojis.Success} Switch moved to <t:{newSwitchTime}> ({newSwitchDeltaStr} ago).");
|
||||
}
|
||||
|
||||
public async Task SwitchEdit(Context ctx, bool newSwitch = false)
|
||||
public async Task SwitchEdit(Context ctx, List<PKMember> newMembers, bool newSwitch = false, bool first = false, bool remove = false, bool append = false, bool prepend = false)
|
||||
{
|
||||
ctx.CheckSystem();
|
||||
|
||||
var newMembers = await ctx.ParseMemberList(ctx.System.Id);
|
||||
|
||||
await using var conn = await ctx.Database.Obtain();
|
||||
var currentSwitch = await ctx.Repository.GetLatestSwitch(ctx.System.Id);
|
||||
if (currentSwitch == null)
|
||||
throw Errors.NoRegisteredSwitches;
|
||||
var currentSwitchMembers = await ctx.Repository.GetSwitchMembers(conn, currentSwitch.Id).ToListAsync().AsTask();
|
||||
|
||||
if (ctx.MatchFlag("first", "f"))
|
||||
if (first)
|
||||
newMembers = FirstInSwitch(newMembers[0], currentSwitchMembers);
|
||||
else if (ctx.MatchFlag("remove", "r"))
|
||||
else if (remove)
|
||||
newMembers = RemoveFromSwitch(newMembers, currentSwitchMembers);
|
||||
else if (ctx.MatchFlag("append", "a"))
|
||||
else if (append)
|
||||
newMembers = AppendToSwitch(newMembers, currentSwitchMembers);
|
||||
else if (ctx.MatchFlag("prepend", "p"))
|
||||
else if (prepend)
|
||||
newMembers = PrependToSwitch(newMembers, currentSwitchMembers);
|
||||
|
||||
if (newSwitch)
|
||||
{
|
||||
// if there's no edit flag, assume we're appending
|
||||
if (!ctx.MatchFlag("first", "f", "remove", "r", "append", "a", "prepend", "p"))
|
||||
if (!prepend && !append && !remove && !first)
|
||||
newMembers = AppendToSwitch(newMembers, currentSwitchMembers);
|
||||
await DoSwitchCommand(ctx, newMembers);
|
||||
}
|
||||
|
|
@ -172,7 +167,7 @@ public class Switch
|
|||
public async Task SwitchEditOut(Context ctx)
|
||||
{
|
||||
ctx.CheckSystem();
|
||||
await DoEditCommand(ctx, new PKMember[] { });
|
||||
await DoEditCommand(ctx, []);
|
||||
}
|
||||
|
||||
public async Task DoEditCommand(Context ctx, ICollection<PKMember> members)
|
||||
|
|
@ -217,11 +212,11 @@ public class Switch
|
|||
await ctx.Reply($"{Emojis.Success} Switch edited. Current fronters are now {newSwitchMemberStr}.");
|
||||
}
|
||||
|
||||
public async Task SwitchDelete(Context ctx)
|
||||
public async Task SwitchDelete(Context ctx, bool all)
|
||||
{
|
||||
ctx.CheckSystem();
|
||||
|
||||
if (ctx.Match("all", "clear") || ctx.MatchFlag("all", "clear", "c"))
|
||||
if (all)
|
||||
{
|
||||
// Subcommand: "delete all"
|
||||
var purgeMsg =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue