quote:
Originally posted by DarkGhost
also if i do
code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
if (Message == "/woot") {
Debug.Trace("WOOT");
}
}
how can i make it so it doesnt say "The command you entered was not recognized"
you need to
return something.
Either you must
return a string, which indicates that your script
has handled the message.
Or either you
don't use the return statement at all or you return the numerical value 0, which indicates that your script
hasn't handled the message (and thus the error "not a command") is shown if no other script exists which can handle the command...
In case your script does handle the message or command you can:
- choose to return an
empty string. This will remove the message (thus the command text) from the typing area in the conversation, so the user can continue typing. Nothing is send to the server.
- or you
return the result of the command handling, as a string.....
See the Plus! scripting documentation for the
OnEvent_ChatWndSendMessage event. It is explained there also.
Also see the examples given in the link which markee posted.
code:
function OnEvent_ChatWndSendMessage(oChatWnd, sMessage) {
if (/^\/([^ \n\r\v\xA0\/][^ \n\r\v\xA0]*)[\s\xA0]?([\s\S]*)/.exec(sMessage)) {
var command = RegExp.$1.toLowerCase();
var parameter = RegExp.$2;
switch (command) {
case 'myname':
// <- Lets tell the function we handled the command. Pass something back to the conversation.
MsgPlus.DisplayToast("Handled Command", "'" + command + "'\n'" + parameter + "'");
Debug.Trace("command handled/recognized: '" + command + "', parameter: '" + parameter + "'");
return "Hi, I'm a cookie!";
case 'donothing':
// <- We recognize the command, but we return the same message/command as-is.
// This is usually BAD code since it will cause the "command not recognized" error from
// Messenger Plus! to pop up if other scripts didn't recognized it. However, it could be
// used to intercept commands from other scripts and act upon them without disturbing the output.
MsgPlus.DisplayToast("Handled Command", "'" + command + "'\n'" + parameter + "'");
Debug.Trace("command handled/recognized: '" + command + "', parameter: '" + parameter + "'");
return sMessage;
case 'showcommand':
// <- We recognize the command, and we return the same message/command as a text by adding a
// second slash. Since it is interpreted as text from that point on by Messenger Plus!,
// you wont get the "command not recognized" error.
MsgPlus.DisplayToast("Handled Command", "'" + command + "'\n'" + parameter + "'");
Debug.Trace("command handled/recognized: '" + command + "', parameter: '" + parameter + "'");
return "/" + sMessage;
case 'clearme':
// <- Lets tell the function we handled the command. But nothing needs to be passed back to
// the conversation and nothing needs to be send to the server; so let's simply clear the
// text from the typing area.
MsgPlus.DisplayToast("Handled Command", "'" + command + "'\n'" + parameter + "'");
Debug.Trace("command handled/recognized: '" + command + "', parameter: '" + parameter + "'");
return "";
case 'wrongcode':
// <- We recognize the command and act upon it, but we return nothing or 0, so Messenger Plus!
// does not know we did handle the command. This is BAD code because it will also cause the
// "command not recognized" error from Messenger Plus! to pop up.
MsgPlus.DisplayToast("Handled Command", "'" + command + "'\n'" + parameter + "'");
Debug.Trace("command handled/recognized: '" + command + "', parameter: '" + parameter + "'");
return 0;
default:
// <- We don't recognize any other commands, so we return nothing or 0.
// Let the other installed and running scripts handle what is written in the conversation.
MsgPlus.DisplayToast("Unhandled Command", "'" + command + "'\n'" + parameter + "'");
Debug.Trace("command recieved but not handled/recognized: '" + command + "', parameter: '" + parameter + "'");
// no return statement here.... or return 0
return 0;
}
}
}
Thus, we handle five different commands in this script for popping up a toast:
/MyName The script will recognize it (and no error is shown). A different message is send back to the conversation.
/DoNothing The script will recognize it but an error is shown! Such code should be avoided.
/ShowCommand The script will recognize it (and no error is shown). The command text is shown in the conversation.
/ClearMe The script will recognize it (and no error is shown). Nothing is done, except for the typing area which is cleared.
/WrongCode The script will recognize it but an error is also shown! Such code should be avoided.
Any other command you try (and which isn't recognized by Messenger Plus! or by any other script) will generate the "command not recognized" error from Messenger Plus!.
Note: in JScript, ending a function with
return 0 is exactly the same as not using the
return statement at all. This is because, by default, if a function ends it always has 0 as its return value already.
PS: regular expression improved a bit, thanks to Markee.