What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Making "Commands".

Making "Commands".
Author: Message:
Vermillion
New Member
*


Posts: 1
Joined: Jun 2011
O.P. Making "Commands".
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!
06-03-2011 04:16 AM
Profile E-Mail PM Find Quote Report
whiz
Senior Member
****


Posts: 568
Reputation: 8
– / – / Flag
Joined: Nov 2008
RE: Making "Commands".
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.
06-03-2011 02:57 PM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Making "Commands".
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:
js 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.
js 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.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
06-03-2011 06:21 PM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


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