예제 #1
0
파일: Utilities.cs 프로젝트: kotx/Qmmands
        public static ParameterBuilder CreateParameterBuilder(CommandBuilder command, ParameterInfo parameterInfo, bool last)
        {
            var builder    = new ParameterBuilder(parameterInfo.ParameterType, command);
            var attributes = parameterInfo.GetCustomAttributes(false);

            for (var i = 0; i < attributes.Length; i++)
            {
                switch (attributes[i])
                {
                case NameAttribute nameAttribute:
                    builder.WithName(nameAttribute.Value);
                    break;

                case DescriptionAttribute descriptionAttribute:
                    builder.WithDescription(descriptionAttribute.Value);
                    break;

                case RemarksAttribute remarksAttribute:
                    builder.WithRemarks(remarksAttribute.Value);
                    break;

                case ParamArrayAttribute _:
                    if (!last)
                    {
                        throw new ParameterBuildingException(builder, $"A params array parameter must be the last parameter in a command. Parameter: {parameterInfo.Name} in {parameterInfo.Member.Name} in {parameterInfo.Member.DeclaringType}.");
                    }

                    builder.WithIsMultiple(true);
                    builder.Type = parameterInfo.ParameterType.GetElementType();
                    break;

                case RemainderAttribute _:
                    if (!last)
                    {
                        throw new ParameterBuildingException(builder, $"A remainder parameter must be the last parameter in a command. Parameter: {parameterInfo.Name} in {parameterInfo.Member.Name} in {parameterInfo.Member.DeclaringType}.");
                    }

                    builder.WithIsRemainder(true);
                    break;

                case OverrideTypeParserAttribute overrideTypeParserAttribute:
                    builder.WithCustomTypeParserType(overrideTypeParserAttribute.Value);

                    if (overrideTypeParserAttribute.GetType() != typeof(OverrideTypeParserAttribute))
                    {
                        builder.AddAttribute(overrideTypeParserAttribute);
                    }
                    break;

                case ParameterCheckAttribute parameterCheckAttribute:
                    builder.AddCheck(parameterCheckAttribute);
                    break;

                case Attribute attribute:
                    builder.AddAttribute(attribute);
                    break;
                }
            }

            if (parameterInfo.HasDefaultValue)
            {
                builder.WithIsOptional(true)
                .WithDefaultValue(parameterInfo.DefaultValue);
            }

            else if (builder.IsMultiple)
            {
                builder.WithIsOptional(true)
                .WithDefaultValue(Array.CreateInstance(builder.Type, 0));
            }

            if (builder.Name == null)
            {
                builder.WithName(parameterInfo.Name);
            }

            return(builder);
        }
예제 #2
0
 internal CommandBuildingException(CommandBuilder commandBuilder, string message)
     : base(message)
 {
     CommandBuilder = commandBuilder;
 }
예제 #3
0
파일: Utilities.cs 프로젝트: kotx/Qmmands
        public static CommandBuilder CreateCommandBuilder(CommandService service, ModuleBuilder module, TypeInfo typeInfo, MethodInfo methodInfo)
        {
            var builder    = new CommandBuilder(module, CreateModuleBaseCommandCallback(service, typeInfo, methodInfo));
            var attributes = methodInfo.GetCustomAttributes(false);

            for (var i = 0; i < attributes.Length; i++)
            {
                switch (attributes[i])
                {
                case NameAttribute nameAttribute:
                    builder.WithName(nameAttribute.Value);
                    break;

                case DescriptionAttribute descriptionAttribute:
                    builder.WithDescription(descriptionAttribute.Value);
                    break;

                case RemarksAttribute remarksAttribute:
                    builder.WithRemarks(remarksAttribute.Value);
                    break;

                case PriorityAttribute priorityAttribute:
                    builder.WithPriority(priorityAttribute.Value);
                    break;

                case RunModeAttribute runModeAttribute:
                    builder.WithRunMode(runModeAttribute.Value);
                    break;

                case CooldownAttribute cooldownAttribute:
                    builder.AddCooldown(new Cooldown(cooldownAttribute.Amount, cooldownAttribute.Per, cooldownAttribute.BucketType));
                    break;

                case IgnoresExtraArgumentsAttribute ignoreExtraArgumentsAttribute:
                    builder.WithIgnoresExtraArguments(ignoreExtraArgumentsAttribute.Value);
                    break;

                case OverrideArgumentParserAttribute overrideArgumentParserAttribute:
                    builder.WithCustomArgumentParserType(overrideArgumentParserAttribute.Value);

                    if (overrideArgumentParserAttribute.GetType() != typeof(OverrideArgumentParserAttribute))
                    {
                        builder.AddAttribute(overrideArgumentParserAttribute);
                    }
                    break;

                case CommandAttribute commandAttribute:
                    for (var j = 0; j < commandAttribute.Aliases.Length; j++)
                    {
                        builder.AddAlias(commandAttribute.Aliases[j]);
                    }
                    break;

                case CheckAttribute checkAttribute:
                    builder.AddCheck(checkAttribute);
                    break;

                case DisabledAttribute disabledAttribute:
                    builder.WithIsEnabled(!disabledAttribute.Value);
                    break;

                case Attribute attribute:
                    builder.AddAttribute(attribute);
                    break;
                }
            }

            var parameters = methodInfo.GetParameters();

            for (var i = 0; i < parameters.Length; i++)
            {
                builder.Parameters.Add(CreateParameterBuilder(builder, parameters[i], i + 1 == parameters.Length));
            }

            return(builder);
        }