Move group add/remove response text logic into MiscUtils
This commit is contained in:
parent
115017980e
commit
fde7e4d3cc
@ -320,7 +320,9 @@ namespace PluralKit.Bot
|
||||
{
|
||||
ctx.CheckOwnGroup(target);
|
||||
|
||||
var members = await ctx.ParseMemberList(ctx.System.Id);
|
||||
var members = (await ctx.ParseMemberList(ctx.System.Id))
|
||||
.Select(m => m.Id)
|
||||
.ToList();
|
||||
|
||||
await using var conn = await _db.Obtain();
|
||||
|
||||
@ -329,40 +331,27 @@ namespace PluralKit.Bot
|
||||
.Select(m => m.Id.Value)
|
||||
.ToHashSet();
|
||||
|
||||
List<MemberId> toAction;
|
||||
|
||||
if (op == AddRemoveOperation.Add)
|
||||
{
|
||||
var membersNotInGroup = members
|
||||
.Where(m => !existingMembersInGroup.Contains(m.Id.Value))
|
||||
.Select(m => m.Id)
|
||||
toAction = members
|
||||
.Where(m => !existingMembersInGroup.Contains(m.Value))
|
||||
.Distinct()
|
||||
.ToList();
|
||||
await _repo.AddMembersToGroup(conn, target.Id, membersNotInGroup);
|
||||
|
||||
if (membersNotInGroup.Count == members.Count)
|
||||
await ctx.Reply(members.Count == 0 ? $"{Emojis.Success} Member added to group." : $"{Emojis.Success} {"members".ToQuantity(membersNotInGroup.Count)} added to group.");
|
||||
else
|
||||
if (membersNotInGroup.Count == 0)
|
||||
await ctx.Reply(members.Count == 1 ? $"{Emojis.Error} Member not added to group (member already in group)." : $"{Emojis.Error} No members added to group (members already in group).");
|
||||
else
|
||||
await ctx.Reply($"{Emojis.Success} {"members".ToQuantity(membersNotInGroup.Count)} added to group ({"members".ToQuantity(members.Count - membersNotInGroup.Count)} already in group).");
|
||||
await _repo.AddMembersToGroup(conn, target.Id, toAction);
|
||||
}
|
||||
else if (op == AddRemoveOperation.Remove)
|
||||
{
|
||||
var membersInGroup = members
|
||||
.Where(m => existingMembersInGroup.Contains(m.Id.Value))
|
||||
.Select(m => m.Id)
|
||||
toAction = members
|
||||
.Where(m => existingMembersInGroup.Contains(m.Value))
|
||||
.Distinct()
|
||||
.ToList();
|
||||
await _repo.RemoveMembersFromGroup(conn, target.Id, membersInGroup);
|
||||
|
||||
if (membersInGroup.Count == members.Count)
|
||||
await ctx.Reply(members.Count == 0 ? $"{Emojis.Success} Member removed from group." : $"{Emojis.Success} {"members".ToQuantity(membersInGroup.Count)} removed from group.");
|
||||
else
|
||||
if (membersInGroup.Count == 0)
|
||||
await ctx.Reply(members.Count == 1 ? $"{Emojis.Error} Member not removed from group (member already not in group)." : $"{Emojis.Error} No members removed from group (members already not in group).");
|
||||
else
|
||||
await ctx.Reply($"{Emojis.Success} {"members".ToQuantity(membersInGroup.Count)} removed from group ({"members".ToQuantity(members.Count - membersInGroup.Count)} already not in group).");
|
||||
await _repo.RemoveMembersFromGroup(conn, target.Id, toAction);
|
||||
}
|
||||
else return; // otherwise toAction "may be undefined"
|
||||
|
||||
await ctx.Reply(MiscUtils.GroupAddRemoveResponse<MemberId>(members, toAction, op));
|
||||
}
|
||||
|
||||
public async Task ListGroupMembers(Context ctx, PKGroup target)
|
||||
|
@ -20,20 +20,6 @@ namespace PluralKit.Bot
|
||||
_repo = repo;
|
||||
}
|
||||
|
||||
private String groupTerm(int groups) => groups == 1 ? "group" : "groups";
|
||||
|
||||
private String Response(List<GroupId> groupList, List<GroupId> actionedOn, Groups.AddRemoveOperation op)
|
||||
{
|
||||
var opStr = op == Groups.AddRemoveOperation.Add ? "added to" : "removed from";
|
||||
var inStr = op == Groups.AddRemoveOperation.Add ? "in" : "not in";
|
||||
var notActionedOn = groupList.Count - actionedOn.Count;
|
||||
|
||||
if (notActionedOn == 0)
|
||||
return $"{Emojis.Success} Member {opStr} {groupTerm(actionedOn.Count)}.";
|
||||
else
|
||||
return $"{Emojis.Success} Member {opStr} {actionedOn.Count} {groupTerm(actionedOn.Count)} (member already {inStr} {notActionedOn} {groupTerm(notActionedOn)}).";
|
||||
}
|
||||
|
||||
public async Task AddRemove(Context ctx, PKMember target, Groups.AddRemoveOperation op)
|
||||
{
|
||||
ctx.CheckSystem().CheckOwnMember(target);
|
||||
@ -67,7 +53,7 @@ namespace PluralKit.Bot
|
||||
}
|
||||
else return; // otherwise toAction "may be unassigned"
|
||||
|
||||
await ctx.Reply(Response(groups, toAction, op));
|
||||
await ctx.Reply(MiscUtils.GroupAddRemoveResponse<GroupId>(groups, toAction, op));
|
||||
}
|
||||
|
||||
public async Task List(Context ctx, PKMember target)
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -17,6 +18,37 @@ namespace PluralKit.Bot
|
||||
public static string ProxyTagsString(this PKMember member, string separator = ", ") =>
|
||||
string.Join(separator, member.ProxyTags.Select(t => t.ProxyString.AsCode()));
|
||||
|
||||
|
||||
private static String entityTerm<T>(int count, bool isTarget)
|
||||
{
|
||||
var ret = "";
|
||||
ret += isTarget ? "Member" : "Group";
|
||||
if ((
|
||||
(typeof(T) == typeof(GroupId) && !isTarget) ||
|
||||
(typeof(T) == typeof(MemberId) && isTarget)
|
||||
) && count > 1)
|
||||
ret += "s";
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static String GroupAddRemoveResponse<T>(List<T> entityList, List<T> actionedOn, Groups.AddRemoveOperation op)
|
||||
{
|
||||
var opStr = op == Groups.AddRemoveOperation.Add ? "added to" : "removed from";
|
||||
var inStr = op == Groups.AddRemoveOperation.Add ? "in" : "not in";
|
||||
var notActionedOn = entityList.Count - actionedOn.Count;
|
||||
|
||||
var groupNotActionedPosStr = typeof(T) == typeof(GroupId) ? notActionedOn.ToString() + " " : "";
|
||||
var memberNotActionedPosStr = typeof(T) == typeof(MemberId) ? notActionedOn.ToString() + " " : "";
|
||||
|
||||
if (actionedOn.Count == 0)
|
||||
return $"{Emojis.Error} {entityTerm<T>(notActionedOn, true)} not {opStr} {entityTerm<T>(entityList.Count, false).ToLower()} ({entityTerm<T>(notActionedOn, true).ToLower()} already {inStr} {entityTerm<T>(entityList.Count, false).ToLower()}).";
|
||||
else
|
||||
if (notActionedOn == 0)
|
||||
return $"{Emojis.Success} {entityTerm<T>(actionedOn.Count, true)} {opStr} {entityTerm<T>(actionedOn.Count, false).ToLower()}.";
|
||||
else
|
||||
return $"{Emojis.Success} {entityTerm<T>(actionedOn.Count, true)} {opStr} {actionedOn.Count} {entityTerm<T>(actionedOn.Count, false).ToLower()} ({memberNotActionedPosStr}{entityTerm<T>(actionedOn.Count, true).ToLower()} already {inStr} {groupNotActionedPosStr}{entityTerm<T>(notActionedOn, false).ToLower()}).";
|
||||
}
|
||||
|
||||
public static bool IsOurProblem(this Exception e)
|
||||
{
|
||||
// This function filters out sporadic errors out of our control from being reported to Sentry
|
||||
|
Loading…
Reference in New Issue
Block a user