In Torch, you can define chat commands using two primary methods: by providing a manual CommandAction delegate or by using reflection on methods within a CommandModule class decorated with attributes.
Method 1: Manual Command Definition
You can instantiate a Command directly by passing a name, description, and a CommandAction delegate. The CommandAction delegate has the signature void (CommandContext context, object[] arguments).
Method 2: Attribute-based Command Modules
For a more structured approach, you can define commands as methods within a class that inherits from CommandModule. This method uses reflection to automatically parse parameters and permissions.
Requirements for Attribute-based Commands:
- The declaring type must be a subclass of
CommandModule. - The method must be decorated with a
[Command] attribute. - (Optional) The method can be decorated with a
[Permission] attribute to set the MinimumPromoteLevel. - (Optional) The class can be decorated with a
[Category] attribute to define a command path (e.g., !module command).
When using the attribute-based approach, Torch automatically handles parameter conversion from strings to the types defined in your method signature.
// Example of the attribute-based approach
[Category(Path = "admin")]
public class MyModule : CommandModule
{
[Command(Name = "greet", Description = "Greets a player")]
[Permission(PromoteLevel = MyPromoteLevel.Admin)]
public void Greet(string playerName, int age)
{
// Logic here
}
}