bot: add color change command
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Text.RegularExpressions;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Discord.Commands;
 | 
			
		||||
 | 
			
		||||
@@ -63,7 +64,7 @@ namespace PluralKit.Bot.Commands
 | 
			
		||||
                if (!await Context.PromptYesNo(msg)) throw new PKError("Member renaming cancelled.");
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Rename the mebmer
 | 
			
		||||
            // Rename the member
 | 
			
		||||
            ContextEntity.Name = newName;
 | 
			
		||||
            await Members.Save(ContextEntity);
 | 
			
		||||
 | 
			
		||||
@@ -73,10 +74,10 @@ namespace PluralKit.Bot.Commands
 | 
			
		||||
 | 
			
		||||
        [Command("description")]
 | 
			
		||||
        [Alias("info", "bio", "text")]
 | 
			
		||||
        [Remarks("member <member> description <description")]
 | 
			
		||||
        [Remarks("member <member> description <description>")]
 | 
			
		||||
        [MustPassOwnMember]
 | 
			
		||||
        public async Task MemberDescription([Remainder] string description = null) {
 | 
			
		||||
            if (description.Length > Limits.MaxDescriptionLength) throw Errors.DescriptionTooLongError(description.Length);
 | 
			
		||||
            if (description.IsLongerThan(Limits.MaxDescriptionLength)) throw Errors.DescriptionTooLongError(description.Length);
 | 
			
		||||
 | 
			
		||||
            ContextEntity.Description = description;
 | 
			
		||||
            await Members.Save(ContextEntity);
 | 
			
		||||
@@ -86,10 +87,10 @@ namespace PluralKit.Bot.Commands
 | 
			
		||||
 | 
			
		||||
        [Command("pronouns")]
 | 
			
		||||
        [Alias("pronoun")]
 | 
			
		||||
        [Remarks("member <member> pronouns <pronouns")]
 | 
			
		||||
        [Remarks("member <member> pronouns <pronouns>")]
 | 
			
		||||
        [MustPassOwnMember]
 | 
			
		||||
        public async Task MemberPronouns([Remainder] string pronouns = null) {
 | 
			
		||||
            if (pronouns.Length > Limits.MaxPronounsLength) throw Errors.MemberPronounsTooLongError(pronouns.Length);
 | 
			
		||||
            if (pronouns.IsLongerThan(Limits.MaxPronounsLength)) throw Errors.MemberPronounsTooLongError(pronouns.Length);
 | 
			
		||||
 | 
			
		||||
            ContextEntity.Pronouns = pronouns;
 | 
			
		||||
            await Members.Save(ContextEntity);
 | 
			
		||||
@@ -97,6 +98,24 @@ namespace PluralKit.Bot.Commands
 | 
			
		||||
            await Context.Channel.SendMessageAsync($"{Emojis.Success} Member pronouns {(pronouns == null ? "cleared" : "changed")}.");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Command("color")]
 | 
			
		||||
        [Alias("colour")]
 | 
			
		||||
        [Remarks("member <member> color <color>")]
 | 
			
		||||
        [MustPassOwnMember]
 | 
			
		||||
        public async Task MemberColor([Remainder] string color = null)
 | 
			
		||||
        {
 | 
			
		||||
            if (color != null)
 | 
			
		||||
            {
 | 
			
		||||
                if (color.StartsWith("#")) color = color.Substring(1);
 | 
			
		||||
                if (!Regex.IsMatch(color, "[0-9a-f]{6}")) throw Errors.InvalidColorError(color);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            ContextEntity.Color = color;
 | 
			
		||||
            await Members.Save(ContextEntity);
 | 
			
		||||
 | 
			
		||||
            await Context.Channel.SendMessageAsync($"{Emojis.Success} Member color {(color == null ? "cleared" : "changed")}.");
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public override async Task<PKMember> ReadContextParameterAsync(string value)
 | 
			
		||||
        {
 | 
			
		||||
            var res = await new PKMemberTypeReader().ReadAsync(Context, value, _services);
 | 
			
		||||
 
 | 
			
		||||
@@ -30,7 +30,7 @@ namespace PluralKit.Bot.Commands
 | 
			
		||||
        public async Task New([Remainder] string systemName = null)
 | 
			
		||||
        {
 | 
			
		||||
            if (ContextEntity != null) throw Errors.NotOwnSystemError;
 | 
			
		||||
            if (Context.SenderSystem != null) throw Errors.NoSystemError;
 | 
			
		||||
            if (Context.SenderSystem != null) throw Errors.ExistingSystemError;
 | 
			
		||||
 | 
			
		||||
            var system = await Systems.Create(systemName);
 | 
			
		||||
            await Systems.Link(system, Context.User.Id);
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ namespace PluralKit.Bot {
 | 
			
		||||
        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 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).");
 | 
			
		||||
@@ -13,5 +13,7 @@ namespace PluralKit.Bot {
 | 
			
		||||
        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 InvalidColorError(string color) => new PKError($"{color} is not a valid color. Color must be in hex format (eg. #ff0000).");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -21,6 +21,12 @@ namespace PluralKit
 | 
			
		||||
            if (str.Length < maxLength) return str;
 | 
			
		||||
            return str.Substring(0, maxLength - ellipsis.Length) + ellipsis;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static bool IsLongerThan(this string str, int length)
 | 
			
		||||
        {
 | 
			
		||||
            if (str != null) return str.Length > length;
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static class Emojis {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user