The best way to parse commands is to use regular expressions. An example would be:
code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message){
var checkExp = /^\/testcmd/i
if(checkExp.test(Message)){
return "I've send you a test command!"
}
}
var checkExp = /^\/testcmd/i declares a new regular expression object, which will give a match when the input string begins with "/testcmd". A regular expression is always placed between two slashes and ends in an optional flag, in this case the
i flag which specifies that the expression is not case-sensitive. The ^ defines the beginning of the string and the slash is escaped by a backslash. (otherwise you should get an error)
The
test() method of the regular expression object returns
true when the input string matches the expression, otherwise it returns
false. This method is very handy for making an if-statement to check if the message matches and then execute a block of code.
The
OnEvent_ChatWndSendMessage event can return a new message which will replace the entered message. In this case, instead of sending "/mycommand" to your contact, it'll send "I've send you a test command!".
And there you have the basics of script command parsing!