Add group name/description/list commands
This commit is contained in:
@@ -47,6 +47,9 @@ namespace PluralKit.Bot
|
||||
public static Command MemberPrivacy = new Command("member privacy", "member <member> privacy <name|description|birthday|pronouns|metadata|visibility|all> <public|private>", "Changes a members's privacy settings");
|
||||
public static Command GroupInfo = new Command("group", "group <name>", "Looks up information about a group");
|
||||
public static Command GroupNew = new Command("group new", "group new <name>", "Creates a new group");
|
||||
public static Command GroupList = new Command("group list", "group list", "Lists all groups in this system");
|
||||
public static Command GroupRename = new Command("group rename", "group <group> name <new name>", "Renames a group");
|
||||
public static Command GroupDesc = new Command("group description", "group <group> description [description]", "Changes a group's description");
|
||||
public static Command Switch = new Command("switch", "switch <member> [member 2] [member 3...]", "Registers a switch");
|
||||
public static Command SwitchOut = new Command("switch out", "switch out", "Registers a switch with no members");
|
||||
public static Command SwitchMove = new Command("switch move", "switch move <date/time>", "Moves the latest switch in time");
|
||||
@@ -321,12 +324,24 @@ namespace PluralKit.Bot
|
||||
// Commands with no group argument
|
||||
if (ctx.Match("n", "new"))
|
||||
await ctx.Execute<Groups>(GroupNew, g => g.CreateGroup(ctx));
|
||||
|
||||
if (await ctx.MatchGroup() is {} group)
|
||||
else if (ctx.Match("list", "l"))
|
||||
await ctx.Execute<Groups>(GroupList, g => g.ListSystemGroups(ctx, null));
|
||||
else if (await ctx.MatchGroup() is {} target)
|
||||
{
|
||||
// Commands with group argument
|
||||
await ctx.Execute<Groups>(GroupInfo, g => g.ShowGroupCard(ctx, group));
|
||||
if (ctx.Match("rename", "name", "changename", "setname"))
|
||||
await ctx.Execute<Groups>(GroupRename, g => g.RenameGroup(ctx, target));
|
||||
else if (ctx.Match("description", "info", "bio", "text", "desc"))
|
||||
await ctx.Execute<Groups>(GroupDesc, g => g.GroupDescription(ctx, target));
|
||||
else if (!ctx.HasNext())
|
||||
await ctx.Execute<Groups>(GroupInfo, g => g.ShowGroupCard(ctx, target));
|
||||
else
|
||||
await PrintCommandNotFoundError(ctx, GroupInfo, GroupRename, GroupDesc);
|
||||
}
|
||||
else if (!ctx.HasNext())
|
||||
await PrintCommandNotFoundError(ctx, GroupInfo, GroupList, GroupNew, GroupRename, GroupDesc);
|
||||
else
|
||||
await ctx.Reply($"{Emojis.Error} {ctx.CreateGroupNotFoundError(ctx.PopArgument())}");
|
||||
}
|
||||
|
||||
private async Task HandleSwitchCommand(Context ctx)
|
||||
|
@@ -1,4 +1,7 @@
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using DSharpPlus.Entities;
|
||||
|
||||
@@ -29,6 +32,100 @@ namespace PluralKit.Bot
|
||||
await ctx.Reply($"{Emojis.Success} Group \"**{groupName}**\" (`{newGroup.Hid}`) registered!\nYou can now start adding members to the group:\n- **pk;group {newGroup.Hid} add <members...>**");
|
||||
}
|
||||
|
||||
public async Task RenameGroup(Context ctx, PKGroup target)
|
||||
{
|
||||
ctx.CheckOwnGroup(target);
|
||||
|
||||
var newName = ctx.RemainderOrNull() ?? throw new PKSyntaxError("You must pass a new group name.");
|
||||
if (newName.Length > Limits.MaxGroupNameLength)
|
||||
throw new PKError($"New group name too long ({newName.Length}/{Limits.MaxMemberNameLength} characters).");
|
||||
|
||||
await using var conn = await _db.Obtain();
|
||||
await conn.UpdateGroup(target.Id, new GroupPatch {Name = newName});
|
||||
|
||||
await ctx.Reply($"{Emojis.Success} Group name changed from \"**{target.Name}**\" to \"**{newName}**\".");
|
||||
}
|
||||
|
||||
public async Task GroupDescription(Context ctx, PKGroup target)
|
||||
{
|
||||
if (ctx.MatchClear())
|
||||
{
|
||||
ctx.CheckOwnGroup(target);
|
||||
|
||||
var patch = new GroupPatch {Description = Partial<string>.Null()};
|
||||
await _db.Execute(conn => conn.UpdateGroup(target.Id, patch));
|
||||
await ctx.Reply($"{Emojis.Success} Group description cleared.");
|
||||
}
|
||||
else if (!ctx.HasNext())
|
||||
{
|
||||
if (target.Description == null)
|
||||
if (ctx.System?.Id == target.System)
|
||||
await ctx.Reply($"This group does not have a description set. To set one, type `pk;group {target.Hid} description <description>`.");
|
||||
else
|
||||
await ctx.Reply("This group does not have a description set.");
|
||||
else if (ctx.MatchFlag("r", "raw"))
|
||||
await ctx.Reply($"```\n{target.Description}\n```");
|
||||
else
|
||||
await ctx.Reply(embed: new DiscordEmbedBuilder()
|
||||
.WithTitle("Group description")
|
||||
.WithDescription(target.Description)
|
||||
.AddField("\u200B", $"To print the description with formatting, type `pk;group {target.Hid} description -raw`."
|
||||
+ (ctx.System?.Id == target.System ? $" To clear it, type `pk;group {target.Hid} description -clear`." : ""))
|
||||
.Build());
|
||||
}
|
||||
else
|
||||
{
|
||||
ctx.CheckOwnGroup(target);
|
||||
|
||||
var description = ctx.RemainderOrNull().NormalizeLineEndSpacing();
|
||||
if (description.IsLongerThan(Limits.MaxDescriptionLength))
|
||||
throw Errors.DescriptionTooLongError(description.Length);
|
||||
|
||||
var patch = new GroupPatch {Description = Partial<string>.Present(description)};
|
||||
await _db.Execute(conn => conn.UpdateGroup(target.Id, patch));
|
||||
|
||||
await ctx.Reply($"{Emojis.Success} Group description changed.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ListSystemGroups(Context ctx, PKSystem system)
|
||||
{
|
||||
if (system == null)
|
||||
{
|
||||
ctx.CheckSystem();
|
||||
system = ctx.System;
|
||||
}
|
||||
|
||||
// TODO: integrate with the normal "search" system
|
||||
await using var conn = await _db.Obtain();
|
||||
|
||||
var groups = (await conn.QueryGroupsInSystem(system.Id)).ToList();
|
||||
if (groups.Count == 0)
|
||||
{
|
||||
if (system.Id == ctx.System?.Id)
|
||||
await ctx.Reply($"This system has no groups. To create one, use the command `pk;group new <name>`.");
|
||||
else
|
||||
await ctx.Reply($"This system has no groups.");
|
||||
return;
|
||||
}
|
||||
|
||||
var title = system.Name != null ? $"Groups of {system.Name} (`{system.Hid}`)" : $"Groups of `{system.Hid}`";
|
||||
await ctx.Paginate(groups.ToAsyncEnumerable(), groups.Count, 25, title, Renderer);
|
||||
|
||||
Task Renderer(DiscordEmbedBuilder eb, IEnumerable<PKGroup> page)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
foreach (var g in page)
|
||||
{
|
||||
sb.Append($"[`{g.Hid}`] **{g.Name}**\n");
|
||||
}
|
||||
|
||||
eb.WithDescription(sb.ToString());
|
||||
eb.WithFooter($"{groups.Count} total");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ShowGroupCard(Context ctx, PKGroup target)
|
||||
{
|
||||
await using var conn = await _db.Obtain();
|
||||
@@ -41,7 +138,7 @@ namespace PluralKit.Bot
|
||||
|
||||
var eb = new DiscordEmbedBuilder()
|
||||
.WithAuthor(nameField)
|
||||
.WithDescription(target.Description)
|
||||
.AddField("Description", target.Description)
|
||||
.WithFooter($"System ID: {system.Hid} | Group ID: {target.Hid} | Created on {target.Created.FormatZoned(system)}");
|
||||
|
||||
await ctx.Reply(embed: eb.Build());
|
||||
|
Reference in New Issue
Block a user