diff --git a/PluralKit/Bot/Commands/MemberCommands.cs b/PluralKit/Bot/Commands/MemberCommands.cs index 380b45f1..0f6e2d58 100644 --- a/PluralKit/Bot/Commands/MemberCommands.cs +++ b/PluralKit/Bot/Commands/MemberCommands.cs @@ -16,6 +16,9 @@ namespace PluralKit.Bot.Commands [Remarks("member new ")] [MustHaveSystem] public async Task NewMember([Remainder] string memberName) { + // Hard name length cap + if (memberName.Length > Limits.MaxMemberNameLength) throw Errors.MemberNameTooLongError(memberName.Length); + // Warn if member name will be unproxyable (with/without tag) if (memberName.Length > Context.SenderSystem.MaxMemberNameLength) { var msg = await Context.Channel.SendMessageAsync($"{Emojis.Warn} Member name too long ({memberName.Length} > {Context.SenderSystem.MaxMemberNameLength} characters), this member will be unproxyable. Do you want to create it anyway? (You can change the name later)"); @@ -44,6 +47,9 @@ namespace PluralKit.Bot.Commands public async Task RenameMember([Remainder] string newName) { // TODO: this method is pretty much a 1:1 copy/paste of the above creation method, find a way to clean? + // Hard name length cap + if (newName.Length > Limits.MaxMemberNameLength) throw Errors.MemberNameTooLongError(newName.Length); + // Warn if member name will be unproxyable (with/without tag) if (newName.Length > Context.SenderSystem.MaxMemberNameLength) { var msg = await Context.Channel.SendMessageAsync($"{Emojis.Warn} New member name too long ({newName.Length} > {Context.SenderSystem.MaxMemberNameLength} characters), this member will be unproxyable. Do you want to change it anyway?"); diff --git a/PluralKit/Bot/Commands/SystemCommands.cs b/PluralKit/Bot/Commands/SystemCommands.cs index ba1b1cb3..851c5968 100644 --- a/PluralKit/Bot/Commands/SystemCommands.cs +++ b/PluralKit/Bot/Commands/SystemCommands.cs @@ -41,7 +41,7 @@ namespace PluralKit.Bot.Commands [Remarks("system name ")] [MustHaveSystem] public async Task Name([Remainder] string newSystemName = null) { - if (newSystemName != null && newSystemName.Length > 250) throw new PKError($"Your chosen system name is too long. ({newSystemName.Length} > 250 characters)"); + if (newSystemName != null && newSystemName.Length > Limits.MaxSystemNameLength) throw Errors.SystemNameTooLongError(newSystemName.Length); Context.SenderSystem.Name = newSystemName; await Systems.Save(Context.SenderSystem); @@ -52,7 +52,7 @@ namespace PluralKit.Bot.Commands [Remarks("system description ")] [MustHaveSystem] public async Task Description([Remainder] string newDescription = null) { - if (newDescription != null && newDescription.Length > 1000) throw new PKError($"Your chosen description is too long. ({newDescription.Length} > 250 characters)"); + if (newDescription != null && newDescription.Length > Limits.MaxDescriptionLength) throw Errors.DescriptionTooLongError(newDescription.Length); Context.SenderSystem.Description = newDescription; await Systems.Save(Context.SenderSystem); @@ -63,7 +63,7 @@ namespace PluralKit.Bot.Commands [Remarks("system tag ")] [MustHaveSystem] public async Task Tag([Remainder] string newTag = null) { - if (newTag.Length > 30) throw new PKError($"Your chosen description is too long. ({newTag.Length} > 30 characters)"); + if (newTag.Length > Limits.MaxSystemTagLength) throw Errors.SystemNameTooLongError(newTag.Length); Context.SenderSystem.Tag = newTag; diff --git a/PluralKit/Bot/Errors.cs b/PluralKit/Bot/Errors.cs index 38396e67..12938bf1 100644 --- a/PluralKit/Bot/Errors.cs +++ b/PluralKit/Bot/Errors.cs @@ -1,9 +1,16 @@ namespace PluralKit.Bot { public static class Errors { + // TODO: is returning constructed errors and throwing them at call site a good idea, or should these be methods that insta-throw instead? + public static PKError NotOwnSystemError => new PKError($"You can only run this command on your own system."); public static PKError NotOwnMemberError => new PKError($"You can only run this command on your own member."); public static PKError NoSystemError => new PKError("You do not have a system registered with PluralKit. To create one, type `pk;system new`."); public static PKError ExistinSystemError => 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 nameLength) => new PKError($"Your chosen system name is too long ({nameLength}/{Limits.MaxSystemNameLength} characters)."); + public static PKError SystemTagTooLongError(int nameLength) => new PKError($"Your chosen system tag is too long ({nameLength}/{Limits.MaxSystemTagLength} characters)."); + public static PKError DescriptionTooLongError(int nameLength) => new PKError($"Your chosen description is too long ({nameLength}/{Limits.MaxDescriptionLength} characters)."); + public static PKError MemberNameTooLongError(int nameLength) => new PKError($"Your chosen member name is too long ({nameLength}/{Limits.MaxMemberNameLength} characters)."); } } \ No newline at end of file diff --git a/PluralKit/Bot/Limits.cs b/PluralKit/Bot/Limits.cs new file mode 100644 index 00000000..e0fe0b29 --- /dev/null +++ b/PluralKit/Bot/Limits.cs @@ -0,0 +1,8 @@ +namespace PluralKit.Bot { + public static class Limits { + public static readonly int MaxSystemNameLength = 100; + public static readonly int MaxSystemTagLength = 31; + public static readonly int MaxDescriptionLength = 1000; + public static readonly int MaxMemberNameLength = 50; + } +} \ No newline at end of file