Shoutbox

/ 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: / commands (/showthread.php?tid=71380)

/ commands by JANiT0R on 02-04-2007 at 05:17 PM

i'm working on a script, i've never done anything like this before.
how do i make it so that if i put "/command" and then my message, the script will take effect?
any help is appreciated.


RE: / commands by Matti on 02-04-2007 at 06:47 PM

Aaah... here's where regular expressions can help. :-)

I'll try to simplify it for you, since I guess you've never worked with those.

code:
var myCommand = "myscriptcmd"; //This is the name of your command! Please don't use any special characters to avoid problems with the regular expression
var myCommandDesc = "My first script command!"; //Here you can add a description for your command which will be shown in the shortcut menu when you press "/"

//Register our command
function OnGetScriptCommands() {
   var s = "<ScriptCommands>";
   s += "<Command>";
   s += "<Name>"+myCommand+"</Name>";
   s += "<Description>"+myCommandDesc+"</Description>";
   s += "<Parameters>&lt;message&gt;</Parameters>";
   s += "</Command>";
   s += "</ScriptCommands>";
   return s;
}

//The send event!
function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
   var reCheck = MakeRegExp(myCommand); //Get a regular expression
   if(reCheck.test(Message)) { //Checks if the message is our command
      var Param = RegExp.$1; //This thing is where the captured parameter is stored
      Message = MyFunction(Param); //Change this to your function name, and eventually add other parameters to it.
   }
   return Message;
}

//A simple function to create a regular expression
function MakeRegExp(str) {
   return new RegExp("/^\/"+str+"\s(.+)$", "i");
}

//An example function. Note that you always have to return something to be sent as replacement for the original message. If you want it to stop sending a message (eg if you want the command to only open a window), just return an empty string. (return "")
function MyFunction(Param) {
   var newMsg = "[c=4]"+Param+"[/c]";
   return newMsg;
}
Note: this code is not tested. It's possible that something wasn't noticed while I reviewed my post. If so, please let me know (or you can try to fix it yourself of course)!
RE: RE: / commands by CookieRevised on 02-05-2007 at 01:16 AM

quote:
Originally posted by Mattike
Aaah... here's where regular expressions can help. :-)

I'll try to simplify it for you, since I guess you've never worked with those.
code:
//Register our command
function OnGetScriptCommands() {
...
}

OnGetScriptCommands has got absolutely nothing todo with "registering" a command and is absolutely not needed.

A command is reconized as a command of your script ONLY if you handle it in the ChatWndSendMessage event and return an empty string.

If you do not return an empty string the command line is passed further to other scripts and eventually to Plus! itself. Though this returning of something or an empty string is not mandatory and you can still perfectly 'react' on something the user has typed.

So...
quote:
Originally posted by Mattike
//An example function. Note that you always have to return something to be sent as replacement for the original message.
No you don't. This only depends on what you wanna do and is not mandatory for handling commands (though it is recommended and the 'right' thing todo).

quote:
Originally posted by Mattike
Note: this code is not tested. It's possible that something wasn't noticed while I reviewed my post. If so, please let me know (or you can try to fix it yourself of course)!
For stuff like this always test the code yourself first (and verify the things you claim are correct like "OnGetScriptCommands registers our command").
RE: / commands by JANiT0R on 02-10-2007 at 09:02 PM

Firstly: thanks for the quick assistance.

but this is really confusing, someone wouldn't happen to be able to do this for me would they?
Basically i wanted a script to turn a message into l33t. i have made one that kinda works, but requires manually switching on and off in scripts menu and i had to copy stuff multiple times. I'm posting the script so feel free to have a shot at it.
any help is appreciated


RE: / commands by roflmao456 on 02-10-2007 at 11:49 PM

why will you have to repeat the same thing again and again?

just use regular expression:

code:
sMessage = sMessage.replace(/whatever/gi,"something")

/whatever/gi << change the "whatever" to a letter; the g means that it will search for multiple entries; the i is ignore case.


edit: i do not think you should put words in there also.. because the letters will take care of it?

i will try and fix this.. i will update this post once its finished so hold on to your butt :happy:

------------


i did it some other way.. so here. i tested it and it works - not sure if you wanted this kind
RE: / commands by CookieRevised on 02-11-2007 at 10:25 AM

That method you used, roflmao456, is not a proper way to do it. In fact, it will render messages wrong.

You first replace a letter 'A' with a letter 'B' and then in the next iteration you search for 'B' to change it to 'C'. Of course, in the end, 'A' will be replaced by 'C' and not by 'B'...

Never use many replace statements after each other for things like this!


RE: / commands by roflmao456 on 02-11-2007 at 03:33 PM

quote:
Originally posted by CookieRevised
That method you used, roflmao456, is not a proper way to do it. In fact, it will render messages wrong.

You first replace a letter 'A' with a letter 'B' and then in the next iteration you search for 'B' to change it to 'C'. Of course, in the end, 'A' will be replaced by 'C' and not by 'B'...

Never use many replace statements after each other for things like this!

check again? it only replaces 'a' with 4 and 'b' with itself and 'c' with (.. etc.

i dont see how it will replace 'a' with a 'b'

RE: / commands by CookieRevised on 02-12-2007 at 11:07 AM

:dodgy: ... the 'A', 'B', 'C' are examples which stand for something, it could be anything. I could have called it 1 2 3 too...

The point is that using many replace statements after eachother and using the same input variable as the output variable is not a good programming method. You should use a different variable as output to avoid that 'A' becomes 'C' in the end.

Explained many times on the forum too though. The type of script asked here, and the scripts shown here already exist in many forms. So search forums first instead of creating yet another script which does the same thing (in a bad way).