From bae883c11f7c2472d0a47da187e4f58f16493819 Mon Sep 17 00:00:00 2001 From: spiral Date: Mon, 13 Sep 2021 01:13:57 -0400 Subject: [PATCH] refactor: consolidate [x]TooLongErrors into StringTooLongError --- PluralKit.Bot/Commands/Groups.cs | 2 +- PluralKit.Bot/Commands/Member.cs | 3 ++- PluralKit.Bot/Commands/MemberEdit.cs | 7 ++++--- PluralKit.Bot/Commands/System.cs | 2 +- PluralKit.Bot/Commands/SystemEdit.cs | 9 +++++---- PluralKit.Bot/Errors.cs | 7 ++----- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/PluralKit.Bot/Commands/Groups.cs b/PluralKit.Bot/Commands/Groups.cs index b93bd55a..9e327dbe 100644 --- a/PluralKit.Bot/Commands/Groups.cs +++ b/PluralKit.Bot/Commands/Groups.cs @@ -194,7 +194,7 @@ namespace PluralKit.Bot { var description = ctx.RemainderOrNull().NormalizeLineEndSpacing(); if (description.IsLongerThan(Limits.MaxDescriptionLength)) - throw Errors.DescriptionTooLongError(description.Length); + throw Errors.StringTooLongError("Description", description.Length, Limits.MaxDescriptionLength); var patch = new GroupPatch { Description = Partial.Present(description) }; await _db.Execute(conn => _repo.UpdateGroup(conn, target.Id, patch)); diff --git a/PluralKit.Bot/Commands/Member.cs b/PluralKit.Bot/Commands/Member.cs index 5b248ee0..e73659b0 100644 --- a/PluralKit.Bot/Commands/Member.cs +++ b/PluralKit.Bot/Commands/Member.cs @@ -36,7 +36,8 @@ namespace PluralKit.Bot var memberName = ctx.RemainderOrNull() ?? throw new PKSyntaxError("You must pass a member name."); // Hard name length cap - if (memberName.Length > Limits.MaxMemberNameLength) throw Errors.MemberNameTooLongError(memberName.Length); + if (memberName.Length > Limits.MaxMemberNameLength) + throw Errors.StringTooLongError("Member name", memberName.Length, Limits.MaxMemberNameLength); // Warn if there's already a member by this name var existingMember = await _db.Execute(c => _repo.GetMemberByName(c, ctx.System.Id, memberName)); diff --git a/PluralKit.Bot/Commands/MemberEdit.cs b/PluralKit.Bot/Commands/MemberEdit.cs index 33cc773d..219dc4f0 100644 --- a/PluralKit.Bot/Commands/MemberEdit.cs +++ b/PluralKit.Bot/Commands/MemberEdit.cs @@ -31,7 +31,8 @@ namespace PluralKit.Bot var newName = ctx.RemainderOrNull() ?? throw new PKSyntaxError("You must pass a new name for the member."); // Hard name length cap - if (newName.Length > Limits.MaxMemberNameLength) throw Errors.MemberNameTooLongError(newName.Length); + if (newName.Length > Limits.MaxMemberNameLength) + throw Errors.StringTooLongError("Member name", newName.Length, Limits.MaxMemberNameLength); // Warn if there's already a member by this name var existingMember = await _db.Execute(conn => _repo.GetMemberByName(conn, ctx.System.Id, newName)); @@ -100,7 +101,7 @@ namespace PluralKit.Bot { var description = ctx.RemainderOrNull().NormalizeLineEndSpacing(); if (description.IsLongerThan(Limits.MaxDescriptionLength)) - throw Errors.DescriptionTooLongError(description.Length); + throw Errors.StringTooLongError("Description", description.Length, Limits.MaxDescriptionLength); var patch = new MemberPatch { Description = Partial.Present(description) }; await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch)); @@ -148,7 +149,7 @@ namespace PluralKit.Bot { var pronouns = ctx.RemainderOrNull().NormalizeLineEndSpacing(); if (pronouns.IsLongerThan(Limits.MaxPronounsLength)) - throw Errors.MemberPronounsTooLongError(pronouns.Length); + throw Errors.StringTooLongError("Pronouns", pronouns.Length, Limits.MaxPronounsLength); var patch = new MemberPatch { Pronouns = Partial.Present(pronouns) }; await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch)); diff --git a/PluralKit.Bot/Commands/System.cs b/PluralKit.Bot/Commands/System.cs index 2336d256..acd25cea 100644 --- a/PluralKit.Bot/Commands/System.cs +++ b/PluralKit.Bot/Commands/System.cs @@ -30,7 +30,7 @@ namespace PluralKit.Bot var systemName = ctx.RemainderOrNull(); if (systemName != null && systemName.Length > Limits.MaxSystemNameLength) - throw Errors.SystemNameTooLongError(systemName.Length); + throw Errors.StringTooLongError("System name", systemName.Length, Limits.MaxSystemNameLength); var system = _db.Execute(async c => { diff --git a/PluralKit.Bot/Commands/SystemEdit.cs b/PluralKit.Bot/Commands/SystemEdit.cs index af00fa97..566abef9 100644 --- a/PluralKit.Bot/Commands/SystemEdit.cs +++ b/PluralKit.Bot/Commands/SystemEdit.cs @@ -62,7 +62,7 @@ namespace PluralKit.Bot var newSystemName = ctx.RemainderOrNull(); if (newSystemName.Length > Limits.MaxSystemNameLength) - throw Errors.SystemNameTooLongError(newSystemName.Length); + throw Errors.StringTooLongError("System name", newSystemName.Length, Limits.MaxSystemNameLength); var patch = new SystemPatch { Name = newSystemName }; await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, patch)); @@ -109,7 +109,8 @@ namespace PluralKit.Bot else { var newDescription = ctx.RemainderOrNull()?.NormalizeLineEndSpacing(); - if (newDescription.Length > Limits.MaxDescriptionLength) throw Errors.DescriptionTooLongError(newDescription.Length); + if (newDescription.Length > Limits.MaxDescriptionLength) + throw Errors.StringTooLongError("Description", newDescription.Length, Limits.MaxDescriptionLength); var patch = new SystemPatch { Description = newDescription }; await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, patch)); @@ -195,7 +196,7 @@ namespace PluralKit.Bot var newTag = ctx.RemainderOrNull(skipFlags: false); if (newTag != null) if (newTag.Length > Limits.MaxSystemTagLength) - throw Errors.SystemTagTooLongError(newTag.Length); + throw Errors.StringTooLongError("System tag", newTag.Length, Limits.MaxSystemTagLength); var patch = new SystemPatch { Tag = newTag }; await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, patch)); @@ -234,7 +235,7 @@ namespace PluralKit.Bot { var newTag = ctx.RemainderOrNull(skipFlags: false); if (newTag != null && newTag.Length > Limits.MaxSystemTagLength) - throw Errors.SystemTagTooLongError(newTag.Length); + throw Errors.StringTooLongError("System server tag", newTag.Length, Limits.MaxSystemTagLength); var patch = new SystemGuildPatch { Tag = newTag }; await _db.Execute(conn => _repo.UpsertSystemGuild(conn, ctx.System.Id, ctx.Guild.Id, patch)); diff --git a/PluralKit.Bot/Errors.cs b/PluralKit.Bot/Errors.cs index 3a5ad547..59cf93d8 100644 --- a/PluralKit.Bot/Errors.cs +++ b/PluralKit.Bot/Errors.cs @@ -41,11 +41,8 @@ namespace PluralKit.Bot public static PKError ExistingSystemError => new PKError("You already have a system registered with PluralKit. To view it, type `pk;system`. If you'd like to delete your system and start anew, type `pk;system delete`, or if you'd like to unlink this account from it, type `pk;unlink`."); public static PKError MissingMemberError => new PKSyntaxError("You need to specify a member to run this command on."); - public static PKError SystemNameTooLongError(int length) => new PKError($"System name too long ({length}/{Limits.MaxSystemNameLength} characters)."); - public static PKError SystemTagTooLongError(int length) => new PKError($"System tag too long ({length}/{Limits.MaxSystemTagLength} characters)."); - public static PKError DescriptionTooLongError(int length) => new PKError($"Description too long ({length}/{Limits.MaxDescriptionLength} characters)."); - public static PKError MemberNameTooLongError(int length) => new PKError($"Member name too long ({length}/{Limits.MaxMemberNameLength} characters)."); - public static PKError MemberPronounsTooLongError(int length) => new PKError($"Member pronouns too long ({length}/{Limits.MaxMemberNameLength} characters)."); + public static PKError StringTooLongError(string name, int length, int maxLength) => new PKError($"{name} too long ({length}/{maxLength} characters)."); + public static PKError MemberLimitReachedError(int limit) => new PKError($"System has reached the maximum number of members ({limit}). Please delete unused members first in order to create new ones."); public static PKError InvalidColorError(string color) => new PKError($"\"{color}\" is not a valid color. Color must be in 6-digit RGB hex format (eg. #ff0000).");