What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Creating a Command?

Creating a Command?
Author: Message:
Samo502
New Member
*


Posts: 9
Joined: Sep 2009
O.P. Creating a Command?
js code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message)
{
    if(Message.substring(0, 6) == "/dispc")
    {
        var Contacts = Messenger.MyContacts;
        var e = new Enumerator(Contacts);
        for(; !e.atEnd(); e.moveNext())
        {
            var Contact = e.item();
            var status = Contact.Status;
            switch(status)
            {
                case 1:
                    status = "Offline";
                    break;
                case 3:
                    status = "Online";
                    break;
                case 4:
                    status = "Busy";
                    break;
                case 5:
                    status = "BRB";
                    break;
                case 6:
                    status = "Idle";
                    break;
                case 7:
                    status = "Away";
                    break;
                case 8:
                    status = "In A Call";
                    break;
                case 9:
                    status = "Eating";
                    break;
            }
            if(status == "Online")
            {
                ChatWnd.SendMessage("[N]" + Contact.Name + " is " + status + "[/N]");
            }
        }
    }
}
I'm trying to make a script to display my online contacts, and it works great but I don't know how to make a window come up with it or actually create a command so I used substring and SendMessage.
10-01-2009 05:37 PM
Profile E-Mail PM Find Quote Report
m0nst3rkill3r
Junior Member
**

Avatar

Posts: 51
Joined: Sep 2009
RE: Creating a Command?
js code:
function OnGetScriptCommands()
{
    var ScriptCommands = "<ScriptCommands>";
    ScriptCommands    +=     "<Command>";
    ScriptCommands    +=         "<Name>command</Name>";
    ScriptCommands    +=         "<Description>DISCRIPTION</Description>";
    ScriptCommands    +=     "</Command>";
    ScriptCommands    += "</ScriptCommands>";

    return ScriptCommands;
}



Think about, that the command[name] have to be the same with

js code:
if (Message.toLowerCase() == "/command"){
return "BLAH";
}

That's how i do it.
10-01-2009 05:41 PM
Profile E-Mail PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: Creating a Command?
Class file
js code:
/*
* -----
* Screenshot Sender - command_class.js
* -----
* Command class for Screenshot Sender
* -----
*/

var Commands = function() {
    this.Commands = '';
}

Commands.prototype = {
    /*
        Name:     AddCommand
        Purpose:    Adds a custom command to Plus!
        Parameters:    sCommand - The command to add
                    sDescription - A description of the command
                    sParameter - Any additional optional parameters
        Return:    None
    */
    "AddCommand" : function(sCommand, sDescription, sParameter) {
        this.Commands += '<Command>'+
                            '<Name>'+sCommand+'</Name>'+
                            '<Description>'+sDescription+'</Description>'+
                            (typeof sParameter === 'undefined' ? '' : '<Parameters>&lt;'+sParameter+'&gt;</Parameters>')+
                         '</Command>';
    },
   
    /*
        Name:     ExportCommands
        Purpose:    Export the Plus! commands in xml form
        Parameters:    None
        Return:    XML formed Plus! commands
    */
    "ExportCommands" : function() {
        return '<ScriptCommands>'+this.Commands+'</ScriptCommands>';
    }
}

Usage:
js code:
function OnGetScriptCommands(){
    var bEnabled = (Messenger.MyStatus > STATUS_INVISIBLE);
   
    var oCommand = new Commands();

    with(oCommand) {
        if ( bEnabled === true ) {
            AddCommand('mycommand', 'my command description', 'optional parameter');
            AddCommand('mycommand2', 'my second command description');
        return ExportCommands();
    }   
}
10-01-2009 05:44 PM
Profile E-Mail PM Find Quote Report
Samo502
New Member
*


Posts: 9
Joined: Sep 2009
O.P. RE: Creating a Command?
It works now to a point, it brings up my command in the list but when I hit enter it says it isn't a command.

This post was edited on 10-01-2009 at 06:07 PM by Samo502.
10-01-2009 05:47 PM
Profile E-Mail PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: Creating a Command?
What code did you use and can you post it?
10-01-2009 06:10 PM
Profile E-Mail PM Find Quote Report
Samo502
New Member
*


Posts: 9
Joined: Sep 2009
O.P. RE: Creating a Command?
js code:
function OnGetScriptCommands()
{
    var bEnabled = (Messenger.MyStatus > STATUS_INVISIBLE);
   
    var oCommand = new Commands();

    with(oCommand) {
        if ( bEnabled === true ) {
            AddCommand('dispc', 'Displays online contacts in the current chat.');
        return ExportCommands();
    } 
}
}
function OnEvent_ChatWndSendMessage(ChatWnd, Message)
{
    if (Message.toLowerCase() == "/dispc")
    {
        var Contacts = Messenger.MyContacts;
        var e = new Enumerator(Contacts);
        for(; !e.atEnd(); e.moveNext())
        {
            var Contact = e.item();
            var status = Contact.Status;
            switch(status)
            {
                case 1:
                    status = "Offline";
                    break;
                case 3:
                    status = "Online";
                    break;
                case 4:
                    status = "Busy";
                    break;
                case 5:
                    status = "BRB";
                    break;
                case 6:
                    status = "Idle";
                    break;
                case 7:
                    status = "Away";
                    break;
                case 8:
                    status = "In A Call";
                    break;
                case 9:
                    status = "Eating";
                    break;
            }
            if(status == "Online")
            {
                ChatWnd.SendMessage("[N]" + Contact.Name + " is " + status + "[/N]");
            }
        }
    }
}
Also your class file is there too, copied and pasted in another file.
10-01-2009 06:12 PM
Profile E-Mail PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Creating a Command?
quote:
Originally posted by Samo502
It works now to a point, it brings up my command in the list but when I hit enter it says it isn't a command.
That's because you need to tell Messenger Plus! that you've actually processed the command. Otherwise it will think there wasn't any script which processed it and thus will show you the error.
(note that this behaviour only occurs with text strings starting with / , thus so called 'commands')

Remember that many scripts can run at the same time. So, if one of those scripts processes a specific command, you need to tell Plus! that. Otherwise it simple launches the next OnEvent_ChatWndSendMessage function from the next script until there are no OnEvent_ChatWndSendMessage functions anymore.

Not telling Plus! that you've processed the command is the same as ignoring it and thus the same as not processing it as far as Plus! is concearned. And thus Plus! will think there is no such command.

Now, to tell Plus! that you have processed the command (or text line for that manner), you simply end your processing by returning the string you want to see in place of the entered string/command.

If you simply want the command string to do something without actually sending the command string to your contact you return with an empty string:
    Return ""

If you want the command string to be replaced by another string and then be send to your contact, you return with the new string:
    Return "I just entered a command"

Returning with a string (even if it is empty) will tell Plus! that your script has processed the recieved text line and thus 'accepted' the command in case it was a command.

Also all explained in the Scripting Docs > Index > OnEvent_ChatWndSendMessage
quote:
Return Value:
A string containing the message to be sent instead of Message. If you do not want to modify the message, simply return Message without changing it. No size restriction applies to the new message except for the maximum size allowed by Messenger. If the event handler returns an empty string, the message is ignored and not sent to the server.



;)

jscript code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message)
{
    if (Message.toLowerCase() == "/dispc")
    {
        for(var e = new Enumerator(Messenger.MyContacts); !e.atEnd(); e.moveNext())
        {
            var Contact = e.item();
            var status = Contact.Status;
            switch(status)
            {
                case 1:
                case 2:
                    status = "Offline";
                    break;
                case 3:
                    status = "Online";
                    break;
...
...
                case 9:
                    status = "Eating";
                    break;
            }
            if(status == "Online")
            {
                ChatWnd.SendMessage("[N]" + Contact.Name + " is " + status + "[/N]");
            }
        }
    Return "";
    }
}
PS: - note that this code can be simplified a lot using arrays.
- Also, before using the SendMessage function you should test if you actually can send a string back because it is not always possible.
- You forgot to include case 2 in the switch.

This post was edited on 10-01-2009 at 06:53 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
10-01-2009 06:36 PM
Profile PM Find Quote Report
Samo502
New Member
*


Posts: 9
Joined: Sep 2009
O.P. RE: Creating a Command?
quote:
Originally posted by CookieRevised

Also, before using the SendMessage function you should test if you actually can send a string back because it is not always possible
Meaning?

@the comment about forgetting case 2
STATUS_OFFLINE (1)
STATUS_ONLINE (3)
STATUS_BUSY (4)
STATUS_BRB (5)
STATUS_IDLE (6)
STATUS_AWAY (7)
STATUS_INCALL (8)
STATUS_OUTLUNCH (9)

It had no need being there since there is no enumeration for 2, it skips it in the statuslist.

This post was edited on 10-01-2009 at 06:53 PM by Samo502.
10-01-2009 06:51 PM
Profile E-Mail PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Creating a Command?
quote:
Originally posted by Samo502
quote:
Originally posted by CookieRevised

Also, before using the SendMessage function you should test if you actually can send a string back because it is not always possible
Meaning?
Scripting Docs > Index > ChatWnd:: SendMessage tells you:
quote:
Remarks:
If the text being set is longer than the maximum allowed (typically 1100 characters), it is truncated. If the typing area already contains text when this function is called, it is saved before the message is sent and automatically restored after that. The EditChangeAllowed property should be checked before this function is called.
Thus:

jscript code:
if (ChatWnd.EditChangeAllowed) {
    ChatWnd.SendMessage("[N]" + Contact.Name + " is " + status + "[/N]")
}

quote:
Originally posted by Samo502
It had no need being there since there is no enumeration for 2, it skips it in the statuslist.
true, but for completeness sake (and maybe for futur compatibility) you can put it there (just as I showed in the example code, only 1 extra line).

Note that there actually is an enumeration constant for that. See Scripting Docs > Index > Messenger:MyStatus:
quote:
STATUS_INVISIBLE (2)

;)

This post was edited on 10-01-2009 at 07:01 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
10-01-2009 06:56 PM
Profile PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: Creating a Command?
This is how I would do it:

js code:
function OnEvent_ChatWndSendMessage ( oChatWnd , sMessage ) {
    if ( /\/dispc$/.test ( sMessage ) ) {
        var oStatusCount = {};
        for ( var oContact = new Enumerator ( Messenger.MyContacts ) !oContact.atEnd( ); oContact.moveNext( ) ) {
            ++oStatusCount [ oContact.item( ).Status ];
        }
       
        var s = 'Offline: '+oStatusContact [ STATUS_INVISIBLE ]+'\n'+
                'Online: '+oStatusContact [ STATUS_ONLINE ]+'\n'+
                'Busy: '+oStatusContact [ STATUS_BUSY ]+'\n'+
                'BRB: '+oStatusContact [ STATUS_BRB ]+'\n'+
                'Idle: '+oStatusContact [ STATUS_IDLE ]+'\n'+
                'Away: '+oStatusContact [ STATUS_AWAY ]+'\n'+
                'In a call: '+oStatusContact [ STATUS_INCALL ]+'\n'+
                'Eating: '+oStatusContact [ STATUS_OUTLUNCH ]
        if ( oChatWnd.EditChangeAllowed === true ) return s;
    }
}
10-01-2009 07:56 PM
Profile E-Mail PM 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