From fde7e4d3cc4f1abf81d1f067524835f81b8ca6d3 Mon Sep 17 00:00:00 2001 From: spiral Date: Sun, 22 Nov 2020 23:11:34 -0500 Subject: [PATCH] Move group add/remove response text logic into MiscUtils --- PluralKit.Bot/Commands/Groups.cs | 39 ++++++++++----------------- PluralKit.Bot/Commands/MemberGroup.cs | 16 +---------- PluralKit.Bot/Utils/MiscUtils.cs | 32 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 40 deletions(-) diff --git a/PluralKit.Bot/Commands/Groups.cs b/PluralKit.Bot/Commands/Groups.cs index 09ef467b..c055e191 100644 --- a/PluralKit.Bot/Commands/Groups.cs +++ b/PluralKit.Bot/Commands/Groups.cs @@ -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 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(members, toAction, op)); } public async Task ListGroupMembers(Context ctx, PKGroup target) diff --git a/PluralKit.Bot/Commands/MemberGroup.cs b/PluralKit.Bot/Commands/MemberGroup.cs index 482bbfa7..b78a5a10 100644 --- a/PluralKit.Bot/Commands/MemberGroup.cs +++ b/PluralKit.Bot/Commands/MemberGroup.cs @@ -20,20 +20,6 @@ namespace PluralKit.Bot _repo = repo; } - private String groupTerm(int groups) => groups == 1 ? "group" : "groups"; - - private String Response(List groupList, List 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(groups, toAction, op)); } public async Task List(Context ctx, PKMember target) diff --git a/PluralKit.Bot/Utils/MiscUtils.cs b/PluralKit.Bot/Utils/MiscUtils.cs index 4d0c2e22..7e8eb3b9 100644 --- a/PluralKit.Bot/Utils/MiscUtils.cs +++ b/PluralKit.Bot/Utils/MiscUtils.cs @@ -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(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(List entityList, List 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(notActionedOn, true)} not {opStr} {entityTerm(entityList.Count, false).ToLower()} ({entityTerm(notActionedOn, true).ToLower()} already {inStr} {entityTerm(entityList.Count, false).ToLower()})."; + else + if (notActionedOn == 0) + return $"{Emojis.Success} {entityTerm(actionedOn.Count, true)} {opStr} {entityTerm(actionedOn.Count, false).ToLower()}."; + else + return $"{Emojis.Success} {entityTerm(actionedOn.Count, true)} {opStr} {actionedOn.Count} {entityTerm(actionedOn.Count, false).ToLower()} ({memberNotActionedPosStr}{entityTerm(actionedOn.Count, true).ToLower()} already {inStr} {groupNotActionedPosStr}{entityTerm(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