Shoutbox

Making "Commands". - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Making "Commands". (/showthread.php?tid=97739)

Making "Commands". by Vermillion on 06-03-2011 at 04:16 AM

Hi guys, this is probably something I missed in the documentation.

How exactly do I make commands (/sendemail, /block, everything starting with /)? I have been reading the initial documentation and I can't seem to find this. I have my function ready, I just want a way to call it with a command.

Thanks a lot in advance!


RE: Making "Commands". by whiz on 06-03-2011 at 02:57 PM

You can use OnGetScriptCommands() to return an XML list of commands, and then OnEvent_ChatWndSendMessage() to capture the event of the user sending the message.


RE: Making "Commands". by Matti on 06-03-2011 at 06:21 PM

As whiz said, you need to handle the command parsing yourself using an OnEvent_ChatWndSendMessage handler.

In its simplest form, that would look like this:

Javascript code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
    if(Message.indexOf("/mycommand") === 0) {
        // Do awesome stuff here
        // Then return something: this can be a replacement message
        // or an empty string. Either way, you must return something
        // to let Plus! know that your script handled this command.
        return "";
    }
}

This will work but there are a few problems with it:
  • The parser doesn't check whether the entered command actually ends after "/mycommand". It could be that the user sent "/mycommandisalie" and it would still be handled by the parser. Therefore, you would have to check whether the command is followed by a space character or is at the end of the message.
  • Commands should be case-insensitive, "/mycommand" should be treated the same way as "/myCommand" and "/MyCoMManD". This can easily be solved by converting the message to lowercase first.
  • If you want to accept parameters for your command, such as "/mycommand hello 123", you'll need to do some extra manipulations to retrieve those.
Luckily, CookieRevised wrote a handy regular expression which takes care of pretty much every aspect of command parsing. For the full explanation, have a look at CookieRevised's reply to Gettin data from "/" commands.
Javascript code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
    if (/^\/([^ \n\r\v\xA0\/][^ \n\r\v\xA0]*)[\s\xA0]?([\s\S]*)/.exec(Message)) {
        var command = RegExp.$1.toLowerCase();
        var parameter = RegExp.$2;
        if(command === "mycommand") {
            // Do awesome stuff here and return something
            return "";
        }
    }
}

The regular expression will be executed on the sent message and if the message is recognized as a command (a "truthy" result from .exec()), the captured contents are stored in command and parameter for you to use. At that point, the only check left is checking what command was found which can be done with a simple string comparison.

If your script only has one command, you can also use Cookie's single command version at the bottom of his post.