Skip to content

Attributes

BIGDummyHead edited this page Apr 28, 2022 · 3 revisions

This will probably be one of my favorite sections of this entire library.

Attributes can be used to modify so many things!

We have two major types of attributes. The BaseCommandAttribute and CommandParameterAttribute, the former allows us to control the method that is being invoked and can only be applied overtop of Methods; The latter allows us to manipulate the string[] that the Handler is parsing, we can join multiple strings into one and return it back to the handler to fit a method so that it can be properly invoked.

Base Command Attribute

public class ExampleAttr : BaseCommandAttribute
{  
    public override async Task<bool> BeforeCommandExecute(object classInstance, object[] methodParams)
    {
       //in here we can do a few things, we can modify the method parameter array before the method is invoked.
       //we can also determine if the command is to be executed at all

       return true; //if we return true: the command will execute. if we return false: the command will not execute
       //it is important to see the HandlerConfig.ByPopularVote because that will also determine how this works with other Command attributes
    }
}

public class Module : ICommandModule
{
   public ValueTask OnCommandExecute(MethodInfo method, object instance, object[] invokes, object? returnInstance)
   {
       throw new NotImplementedException();
   }
   
   [Command]
   async Task Say(string arg1)
   {
      Console.WriteLine(arg1);
   }
}

Command Parameter Attribute

Collected and invoked when commands are invoked

public class ExampleAttribute : CommandParameterAttribute
{
    public override async Task<string[]> OnCollect(ParameterInfo applied, object[] preArgs, string[] args, object[] aftArgs, ParameterInfo[] methodParameters)
    {
        //in here we will return the string[] for our IStringHandler to parse

        return args;
    }

}

Built-in

OptionalAttribute and RemainingTextAttribute both inherit this below

Range Attribute

The range attribute is used to determine how long your string argument will be from, x to y.

Example:

void Ex([Range(1,3)] string a)
{
   // 'a' can be 1-3 in length
   // ex: "A" - "A B" - "A B C"
   // no: "" - "A B C D"
}

OptionalAttribute : base(0, 1). Allows a range from no arguments passed to one

RemainingTextAttribute : base(0, int.MaxValue). Allows a range from no arguments/1 to the max length of a string

Required Params Attribute

Specifies that a argument must have 'x' amount of args passed in.

void Ex([RequiredParams(2)] string a)
{
  // 'a' must have a arg len of 2
  // ex: "A B"
}

Command Attribute

Special attribute that cannot be replicated.

Registers a method for the handler, basically an indicator!

[Command] //use the method's sig name
//or
[Command("name")] //use a custom name
void Ex()
{
}

Ignore Attribute

Special attribute that cannot be replicated.

Stops that Handler from registering a method.

[Command]
[Ignore] //this command will not be registered.
void Ex()
{
}
Clone this wiki locally