bot: refactor length limits to use constants

This commit is contained in:
Ske 2019-04-29 20:28:53 +02:00
parent bf8387cf52
commit 6fe3154a10
4 changed files with 24 additions and 3 deletions

View File

@ -16,6 +16,9 @@ namespace PluralKit.Bot.Commands
[Remarks("member new <name>")]
[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?");

View File

@ -41,7 +41,7 @@ namespace PluralKit.Bot.Commands
[Remarks("system name <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 <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 <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;

View File

@ -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).");
}
}

8
PluralKit/Bot/Limits.cs Normal file
View File

@ -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;
}
}