Custom Chat Commands
A chat command is when the user types messages into chat like /give LogItem, /unstuck, etc.
You can write custom chat commands easily by implementing the IChatComanndHandler interface on any public class on the server then attaching the ChatCommandAttribute to any static method in that class.
For example:
public class MyModCommands : IChatCommandHandler { [ChatCommand("View your current skill title")] public static void WhoAmI(User user) { user.Player.SendTemporaryMessage(user.Player.SkillTitle); } }
ChatCommandAttribute[edit | edit source]
- public ChatCommandAttribute(string command, string helpText, ChatAuthorizationLevel level)
- public ChatCommandAttribute(string helpText, ChatAuthorizationLevel level)
- public ChatCommandAttribute(ChatAuthorizationLevel level)
Attach the chat attribute to any static method in a class that implements the IChatCommandHandler interface.
If no command is specified, the command will default to the method name. The following commands are invoked with “/bhop”.
/bhop
[ChatCommand()] public static void Bhop(User user) { user.Player.Client.RPC("ToggleHopping"); }
/bhop
[ChatCommand(“bhop”, “hop hop hop”)] public static void DoSomething(User user) { user.Player.Client.RPC("ToggleHopping"); }
Chat command methods can take the following types as arguments:
- int
- float
- string
- bool - parsed as int.parse(string input) != 0
- User - parsed from the username, guaranteed to not be null.
All of these types are guaranteed to not be null. The chat manager catches null before invoking the command.
If the first argument is of type User, the command handler assigns that argument to the invoking user.
/dosomething otherUsername
[ChatCommand()] public static void DoSomething(User user, User otherUser) { user.Player // the player invoking the command otherUser.Player // the player defined by the first argument }
/dosomething 42, myUsername, otherUsername
[ChatCommand()] public static void DoSomethingElse(int i, User user, User otherUser) { user.Player // the player defined by the first argument otherUser.Player // the player defined by the second argument }
Chat command methods support default values in method arguments.
/dosomething 5_ Or _/dosomething 5, 3.14
[ChatCommand()] public static void DoSomething(int number, float value = 0.0f) { number //must be declared by the command value //player can choose to alter or not }
/help, /?[edit | edit source]
Chat commands can be given a string to describe their function in the declaration of the ChatCommand attribute. When a player invokes _"/help"_, all commands are written to the notifications log with their help text, if it has any.
If a player invokes "/help dosomething", the command is written to the notifications log with it's help text and its syntax.
"/help dosomething" Or "/? dosomething"
[ChatCommand("The best command there ever was")] public static void DoSomething(int number, string name, float value = 0.0f, bool spawnWithTrees = false) { }
This produces the following output in the chatlog. Arguments that have default values appear with an asterisk over their property name.
dosomething: The best command there ever was /dosomething number, name, value*, spawnWithTrees*