PluralKit/PluralKit.Bot/Commands/MemberGroup.cs

87 lines
2.6 KiB
C#
Raw Normal View History

using Myriad.Builders;
2020-11-22 19:57:01 -05:00
using PluralKit.Core;
namespace PluralKit.Bot;
2020-11-22 19:57:01 -05:00
public class MemberGroup
{
private readonly IDatabase _db;
private readonly ModelRepository _repo;
2020-11-22 19:57:01 -05:00
public MemberGroup(IDatabase db, ModelRepository repo)
{
_db = db;
_repo = repo;
}
2020-11-22 19:57:01 -05:00
public async Task AddRemove(Context ctx, PKMember target, Groups.AddRemoveOperation op)
{
ctx.CheckSystem().CheckOwnMember(target);
2020-11-22 19:57:01 -05:00
var groups = (await ctx.ParseGroupList(ctx.System.Id))
.Select(g => g.Id)
.Distinct()
.ToList();
2020-11-22 19:57:01 -05:00
var existingGroups = (await _repo.GetMemberGroups(target.Id).ToListAsync())
.Select(g => g.Id)
.Distinct()
.ToList();
2020-11-22 19:57:01 -05:00
List<GroupId> toAction;
2020-11-22 19:57:01 -05:00
if (op == Groups.AddRemoveOperation.Add)
{
toAction = groups
.Where(group => !existingGroups.Contains(group))
.ToList();
2020-11-22 19:57:01 -05:00
await _repo.AddGroupsToMember(target.Id, toAction);
2020-11-22 19:57:01 -05:00
}
else if (op == Groups.AddRemoveOperation.Remove)
{
toAction = groups
.Where(group => existingGroups.Contains(group))
.ToList();
2020-11-22 19:57:01 -05:00
await _repo.RemoveGroupsFromMember(target.Id, toAction);
}
else
2020-11-22 19:57:01 -05:00
{
return; // otherwise toAction "may be unassigned"
}
await ctx.Reply(GroupMemberUtils.GenerateResponse(op, 1, groups.Count, toAction.Count,
groups.Count - toAction.Count));
}
2020-11-22 19:57:01 -05:00
public async Task List(Context ctx, PKMember target)
{
var pctx = ctx.LookupContextFor(target.System);
2020-11-22 19:57:01 -05:00
var groups = await _repo.GetMemberGroups(target.Id)
.Where(g => g.Visibility.CanAccess(pctx))
.OrderBy(g => g.Name, StringComparer.InvariantCultureIgnoreCase)
.ToListAsync();
2020-11-22 19:57:01 -05:00
var description = "";
var msg = "";
2021-08-27 11:03:47 -04:00
if (groups.Count == 0)
description = "This member has no groups.";
else
description = string.Join("\n", groups.Select(g => $"[`{g.Hid}`] **{g.DisplayName ?? g.Name}**"));
2021-08-27 11:03:47 -04:00
if (pctx == LookupContext.ByOwner)
{
msg +=
$"\n\nTo add this member to one or more groups, use `pk;m {target.Reference()} group add <group> [group 2] [group 3...]`";
if (groups.Count > 0)
msg +=
$"\nTo remove this member from one or more groups, use `pk;m {target.Reference()} group remove <group> [group 2] [group 3...]`";
2020-11-22 19:57:01 -05:00
}
await ctx.Reply(msg, new EmbedBuilder().Title($"{target.Name}'s groups").Description(description).Build());
2020-11-22 19:57:01 -05:00
}
}