What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Gettin data from "/" commands

Gettin data from "/" commands
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Gettin data from "/" commands
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.

This post was edited on 03-13-2011 at 11:17 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
05-23-2007 12:09 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Gettin data from "/" commands - by Lobo on 05-22-2007 at 07:46 PM
RE: Gettin data from "/" commands - by saralk on 05-22-2007 at 08:03 PM
RE: Gettin data from "/" commands - by vikke on 05-22-2007 at 08:05 PM
RE: Gettin data from "/" commands - by markee on 05-22-2007 at 08:59 PM
RE: Gettin data from "/" commands - by DarkGhost on 05-22-2007 at 11:41 PM
RE: Gettin data from "/" commands - by CookieRevised on 05-23-2007 at 12:09 AM
RE: Gettin data from "/" commands - by DarkGhost on 05-23-2007 at 12:32 AM
RE: Gettin data from "/" commands - by CookieRevised on 05-23-2007 at 12:56 AM
RE: Gettin data from "/" commands - by Lobo on 05-23-2007 at 07:14 PM
RE: Gettin data from "/" commands - by markee on 02-04-2008 at 11:52 AM
RE: Gettin data from "/" commands - by CookieRevised on 12-07-2009 at 01:39 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On