From 47d5ad00049d6b8c60b28a22f129dfaf32f6f60c Mon Sep 17 00:00:00 2001 From: Ske Date: Sat, 18 Jul 2020 13:30:54 +0200 Subject: [PATCH] Enforce group count limit on creation --- PluralKit.Bot/Commands/Groups.cs | 8 +++++++- PluralKit.Core/Utils/Limits.cs | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/PluralKit.Bot/Commands/Groups.cs b/PluralKit.Bot/Commands/Groups.cs index 2580eb3f..d381dcdb 100644 --- a/PluralKit.Bot/Commands/Groups.cs +++ b/PluralKit.Bot/Commands/Groups.cs @@ -3,6 +3,8 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Dapper; + using DSharpPlus.Entities; using NodaTime; @@ -29,8 +31,12 @@ namespace PluralKit.Bot throw new PKError($"Group name too long ({groupName.Length}/{Limits.MaxMemberNameLength} characters)."); await using var conn = await _db.Obtain(); - var newGroup = await conn.CreateGroup(ctx.System.Id, groupName); + var existingGroupCount = await conn.QuerySingleAsync("select count(*) from groups where system = @System", ctx.System.Id); + if (existingGroupCount >= Limits.MaxGroupCount) + throw new PKError($"System has reached the maximum number of groups ({Limits.MaxGroupCount}). Please delete unused groups first in order to create new ones."); + + var newGroup = await conn.CreateGroup(ctx.System.Id, groupName); await ctx.Reply($"{Emojis.Success} Group \"**{groupName}**\" (`{newGroup.Hid}`) registered!\nYou can now start adding members to the group like this:\n> **pk;group `{newGroup.Hid}` add `member1` `member2...`**"); } diff --git a/PluralKit.Core/Utils/Limits.cs b/PluralKit.Core/Utils/Limits.cs index afcf0dd8..769acb3a 100644 --- a/PluralKit.Core/Utils/Limits.cs +++ b/PluralKit.Core/Utils/Limits.cs @@ -7,6 +7,7 @@ namespace PluralKit.Core { public static readonly int MaxSystemTagLength = MaxProxyNameLength - 1; public static readonly int MaxMemberCount = 1500; public static readonly int MaxMembersWarnThreshold = MaxMemberCount - 50; + public static readonly int MaxGroupCount = 50; // TODO: up to 100+? public static readonly int MaxDescriptionLength = 1000; public static readonly int MaxMemberNameLength = 100; // Fair bit larger than MaxProxyNameLength for bookkeeping public static readonly int MaxGroupNameLength = 100;