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

Pages: (2): « First [ 1 ] 2 » Last »
Gettin data from "/" commands
Author: Message:
Lobo
New Member
*


Posts: 7
Joined: May 2007
O.P. Gettin data from "/" commands
Hi all!

I know how to add and trap "/" commands, but my question is how to grab the param data...for example If I type:
/Joel somedata
How do I get only somedata?
Thanks! (6)
05-22-2007 07:46 PM
Profile E-Mail PM Find Quote Report
saralk
Veteran Member
*****

Avatar

Posts: 2598
Reputation: 38
35 / Male / Flag
Joined: Feb 2003
RE: Gettin data from "/" commands
I don't know what method you use to trap "/" commands, but you normally use ChatWndSendMessage(Message).

Then the variable Message, is the whole message that was sent, including to bit after the parameter, so you can get all the data you want from there.

e.g.
code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
var checkExpCommand = /^\/command/i
if ( checkExpCommand.test(Message) ) {
  Param = Message.replace(checkExpCommand, "");           
}
}

This post was edited on 05-22-2007 at 08:04 PM by saralk.
The Artist Formerly Known As saralk
London · New York · Paris
Est. 1989
05-22-2007 08:03 PM
Profile PM Find Quote Report
vikke
Senior Member
****

Avatar

Posts: 900
Reputation: 28
31 / Male / Flag
Joined: May 2006
RE: Gettin data from "/" commands
You can do:
code:
var Command = "/Joel";
if(Message.substr(0, Command.length) == Command)
{
  // The /Joel command was used.
}

And then you get the param by:
code:
var Param = Message.substr(Command.length, Message.length);

Edit: This is all done in the ChatWndSendMessage event. Read the scripting documentation for more information.

This post was edited on 05-22-2007 at 08:07 PM by vikke.
4 8 15 16 23 42
05-22-2007 08:05 PM
Profile E-Mail PM Find Quote Report
markee
Veteran Member
*****

Avatar

Posts: 1622
Reputation: 50
36 / Male / Flag
Joined: Jan 2006
RE: Gettin data from "/" commands
This method is my favourite and it VERY well written.  If there is anyway that people should get commands in general it is using this method posted by CookieRevised

CookieRevised's reply to [Release] Exit WLM
[Image: markee.png]
05-22-2007 08:59 PM
Profile PM Find Quote Report
DarkGhost
New Member
*


Posts: 14
Joined: May 2007
RE: Gettin data from "/" commands
ahh thank you i wqas looking for this

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 "Messenger Plus CAN NOT FIND THIS COMMAND" or w.e
05-22-2007 11:41 PM
Profile E-Mail PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
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
DarkGhost
New Member
*


Posts: 14
Joined: May 2007
RE: Gettin data from "/" commands
k ty
    if (/^\/([^\s\/]+)\s*([\s\S]*)$/.exec(sMessage) !== null) { whats this mean? - also is there a way i can just like make it so the person who typed /command can only see the message

This post was edited on 05-23-2007 at 12:43 AM by DarkGhost.
05-23-2007 12:32 AM
Profile E-Mail PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Gettin data from "/" commands
quote:
Originally posted by DarkGhost
if (/^\/([^\s\/]+)\s*([\s\S]*)$/.exec(sMessage) !== null) { whats this mean?
It is a regular expression.

Download and read the JScript 5.6 Documentation > "Regular Expression Object" and "Regular Expression Syntax"

There are also many threads on these forums which are about regular expressions, including snippets and code examples.

To see what the above regular expression does, see the link posted by markee which explains it.

In short: this one checks if the message of the user is a command and it splits up the message into the command part and the optional parameter part.

quote:
Originally posted by DarkGhost
also is there a way i can just like make it so the person who typed /command can only see the message
Show a messagebox to the user. Or show a Messenger Plus! toast with the MsgPlus.DisplayToast function, etc....

;)




PS: Try not to double post. Instead, click on the edit button beneath your last post if you want to add something before someone replies.

PS: always read the given links; They are given for a reaon. Try to use the search function of the forum.

This post was edited on 05-23-2007 at 01:05 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
05-23-2007 12:56 AM
Profile PM Find Quote Report
Lobo
New Member
*


Posts: 7
Joined: May 2007
O.P. RE: Gettin data from "/" commands
Ah...thanks all for your tips!!! :)
05-23-2007 07:14 PM
Profile E-Mail PM Find Quote Report
markee
Veteran Member
*****

Avatar

Posts: 1622
Reputation: 50
36 / Male / Flag
Joined: Jan 2006
RE: Gettin data from "/" commands
I just wanted to update Cookie's regex to something that is a little better IMHO

code:
if (/^\/([^\s\/]\S*)\s*(.*)/.exec(sMessage) !== null) {

Cookie's old code didn't allow for / to be used elsewhere through the command (you can't use it to begin the command, but it is possible to use it later.  There is no point having $ at the end of the expression due to the greedy nature of quantifiers.  And finally [\s\S] is simply the equivalent of ".".

I think I should also give people an example of having parameters that are ALWAYS a single word, it is best to use the following instead:

code:
if (/^\/([^\s\/]\S*)\s*(\S*)/.exec(sMessage) !== null) {

and then for multiple paramters seperated by spaces (you will still need to do a split on the RegExp.$2 variable to get all of these variables seperated, or use of methods involving regex if you want to avoid blank variables):

code:
if (/^\/([^\s\/]\S*)\s*((?:\S+\s)*)/.exec(sMessage) !== null) {

If you want a different way to seperate between the parameters then you can use something like square braces (ie. "[param and with spaces]").  I'll use up to 3 params in this example:

code:
if (/^\/([^\s\/]\S*)\s*(?:\[.*?\]\s*(?:\[.*?\]\s*(?:\[.*?\])?)?)?/.exec(sMessage) !== null) {

This then becomes useful for when you are trying to find matching Plus! tags.  But this is getting off-topic and a lot more advanced when it comes to Regex.  If you have any questions, or want to see some other examples then please ask and I'll be happy to help (better to do it here so everyone can apreciate the beauty of regex)
[Image: markee.png]
02-04-2008 11:52 AM
Profile PM Find Quote Report
Pages: (2): « First [ 1 ] 2 » Last »
« 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