feat(bot): add switch copying and more detailed switch editing

This commit is contained in:
Petal Ladenson 2024-10-03 02:29:38 -06:00 committed by GitHub
parent 23fe904464
commit 4bd4407771
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 70 additions and 6 deletions

View file

@ -82,6 +82,7 @@ public partial class CommandTree
public static Command SwitchMove = new Command("switch move", "switch move <date/time>", "Moves the latest switch in time");
public static Command SwitchEdit = new Command("switch edit", "switch edit <member> [member 2] [member 3...]", "Edits the members in the latest switch");
public static Command SwitchEditOut = new Command("switch edit out", "switch edit out", "Turns the latest switch into a switch-out");
public static Command SwitchCopy = new Command("switch copy", "switch copy <member> [member 2] [member 3...]", "Makes a new switch with the listed members added");
public static Command SwitchDelete = new Command("switch delete", "switch delete", "Deletes the latest switch");
public static Command SwitchDeleteAll = new Command("switch delete", "switch delete all", "Deletes all logged switches");
public static Command Link = new Command("link", "link <account>", "Links your system to another account");
@ -137,7 +138,7 @@ public partial class CommandTree
public static Command[] SwitchCommands =
{
Switch, SwitchOut, SwitchMove, SwitchEdit, SwitchEditOut, SwitchDelete, SwitchDeleteAll
Switch, SwitchOut, SwitchMove, SwitchEdit, SwitchEditOut, SwitchDelete, SwitchDeleteAll, SwitchCopy
};
public static Command[] ConfigCommands =

View file

@ -430,13 +430,15 @@ public partial class CommandTree
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, SystemFronter, SystemFrontHistory);
SwitchDelete, SwitchCopy, SystemFronter, SystemFrontHistory);
}
private async Task CommandHelpRoot(Context ctx)

View file

@ -7,6 +7,7 @@ namespace PluralKit.Bot;
public class Switch
{
public async Task SwitchDo(Context ctx)
{
ctx.CheckSystem();
@ -103,12 +104,67 @@ public class Switch
await ctx.Reply($"{Emojis.Success} Switch moved to <t:{newSwitchTime}> ({newSwitchDeltaStr} ago).");
}
public async Task SwitchEdit(Context ctx)
public async Task SwitchEdit(Context ctx, bool newSwitch = false)
{
ctx.CheckSystem();
var members = await ctx.ParseMemberList(ctx.System.Id);
await DoEditCommand(ctx, members);
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);
var currentSwitchMembers = await ctx.Repository.GetSwitchMembers(conn, currentSwitch.Id).ToListAsync().AsTask();
if (ctx.MatchFlag("first", "f"))
newMembers = FirstInSwitch(newMembers[0], currentSwitchMembers);
else if (ctx.MatchFlag("remove", "r"))
newMembers = RemoveFromSwitch(newMembers, currentSwitchMembers);
else if (ctx.MatchFlag("append", "a"))
newMembers = AppendToSwitch(newMembers, currentSwitchMembers);
else if (ctx.MatchFlag("prepend", "p"))
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"))
newMembers = AppendToSwitch(newMembers, currentSwitchMembers);
await DoSwitchCommand(ctx, newMembers);
}
else
await DoEditCommand(ctx, newMembers);
}
public List<PKMember> PrependToSwitch(List<PKMember> members, List<PKMember> currentSwitchMembers)
{
members.AddRange(currentSwitchMembers);
return members;
}
public List<PKMember> AppendToSwitch(List<PKMember> members, List<PKMember> currentSwitchMembers)
{
currentSwitchMembers.AddRange(members);
members = currentSwitchMembers;
return members;
}
public List<PKMember> RemoveFromSwitch(List<PKMember> members, List<PKMember> currentSwitchMembers)
{
var memberIds = members.Select(m => m.Id.Value);
currentSwitchMembers = currentSwitchMembers.Where(m => !memberIds.Contains(m.Id.Value)).ToList();
members = currentSwitchMembers;
return members;
}
public List<PKMember> FirstInSwitch(PKMember member, List<PKMember> currentSwitchMembers)
{
currentSwitchMembers = currentSwitchMembers.Where(m => m.Id != member.Id).ToList();
var members = new List<PKMember> { member };
members.AddRange(currentSwitchMembers);
return members;
}
public async Task SwitchEditOut(Context ctx)

View file

@ -123,7 +123,8 @@ You can have a space after `pk;`, e.g. `pk;system` and `pk; system` will do the
## Switching commands
- `pk;switch [member...]` - Registers a switch with the given members.
- `pk;switch out` - Registers a 'switch-out' - a switch with no associated members.
- `pk;switch edit <member...|out>` - Edits the members in the latest switch.
- `pk;switch edit <member...|out>` - Edits the members in the latest switch.
- `pk;switch add <member...>` - Makes a new switch based off the current switch with the listed members added or removed.
- `pk;switch move <time>` - Moves the latest switch backwards in time.
- `pk;switch delete` - Deletes the latest switch.
- `pk;switch delete all` - Deletes all logged switches.

View file

@ -82,6 +82,10 @@ You cannot look up private members or groups of another system.
|pk;edit|-nospace|-ns|Append/prepend without adding a space|
|pk;edit|-clear-embed|-ce|Remove embeds from a message|
|pk;edit|-regex|-x|Edit using a C# Regex formatted like s\|X\|Y or s\|X\|Y\|F, where \| is any character, X is a Regex, Y is a substitution string, and F is a set of Regex flags|
|pk;switch edit and pk;switch add|-append|-a|Append members to the current switch or make a new switch with members appended|
|pk;switch edit and pk;switch add|-prepend|-p|Prepend members to the current switch or make a new switch with members prepended|
|pk;switch edit and pk;switch add|-first|-f|Move member to the front of the current switch or make a new switch with the member at the front|
|pk;switch edit and pk;switch add|-remove|-r|Remove members from the current switch or make a new switch with members removed|
|Most commands|-all|-a|Show hidden/private information|
|Most commands|-raw|-r|Show text with formatting, for easier copy-pasting|
|All commands|-private|-priv|Show private information|