mScriptBox Tutorials
How to write your own Nick Completer

Written by LocutusofBorg
Published with permission.

Table Of Contents

  1. Introduction
  2. Step 1: What do we want?
  3. Step 2: Getting started
  4. Step 3: Loop di Loop
  5. Step 4: Error checking
  6. Step 5: Grab a nick
  7. Step 6: The fun begins
  8. Step 7: Let's put the nick into our line
  9. Conclusion


1. Introduction  Back to Top

This tutorial is going to show you the principle of making a nick completer or nick styler. If you follow it, you will end up with one specific one. In this case one of mine. If you follow it, you should also when you are done understand how the process works, and thus be able to write your own.

IMPORTANT:
This tutorial is for people who already know a little scripting. You should be aware of basics like using loops and if statements, and knowing your way around tokens is a pro. Knowing how to look up specifics of commands used is a must. Enjoy!


2. Step 1: What do we want?  Back to Top

A big decision. It may not look like much, but it decides the level of complexity required for the script. Do we want something that just styles a nick, or do we want to actually have the script complete the nick as well? Do we want to do this only on the first word in a sentence, or on every word? Do we want it to trigger only when a word is tagged in some way, or do we want it on all words?

Let's break this down:

  • If we want to do this on all words (and this goes for just the first word too), you may end up with a problem. If the script completes the nick as well as styles it, odds increase exponentially of this happening. The problem is simple: what if you type a word in a sentence, that is someone's nick, or is the first handful of letters of someone's nick? If we don't have a tag on the words we want to complete, the script will turn these words into nicks, and style them. that is not what we want. So best is to someway tag the nick, by using an easy-to-use character, that isn't normally used in chat. You could use the tilde (~) for this, but the choice is yours. I suggest that whatever you pick, you pick a character that is not allowed in nicks. That means the tilde is just fine.

  • If we want the script to actually complete the nick, we have to match the tagged word against the nicklist. that sounds easier than it is. The problem is when we type only 2 or 3 letters, and there is more than one nick beginning with those letters. So we will need to add some error checking to prevent the script from kicking in and putting the wrong nick in your text. That would be a bummer.

  • If we want the script to act on all words in a line, we will need to repeat the previous two points on all words. We can simply search for the tag in our text though, so it makes it a tad simpler.

After thinking it through, I decided to make the following choices:

  • The tag will be the tilde. A to-be-replaced-completed-and-styled word will begin with a tilde;
  • The script will replace all the words in a sentence beginning with tilde;
  • The script will complete and style the nick;
  • The style will be: **nick**.


3. Step 2: Getting started  Back to Top

For the script to work at all, we will need the on input event. There, we will need to find all words starting with a tilde. We can use $wildtok for this.


ON *:INPUT:#: {
   IF (!$wildtok($strip($1-),~*,0,32)) { msg $target $1- | HALT }
}

Still with me? The $strip is there to prevent any mishappenings because you added a color code somewhere, and this part will just send the text to the target channel unaltered if no words begin with a tilde. The HALT is there to end processing and prevent your text being sent to the target channel twice.

Now we have two options:

  • there is only one word found, or
  • there are more than one words found.
We could script these options separately, but we could also write a decent loop and do them together.


4. Step 3: Loop di Loop  Back to Top

So this we add:

   VAR %nc.snd = $1-
   VAR %nc.x = 1
   WHILE (%nc.x <= $wildtok($strip($1-),~*,0,32)) {
      ; we need to replace the found token with the styled/completed nick
      ; we will also need to do some error checking 
      ; when we match the found word against the nicklist
      ; we will store our newly contructed sentence in %nc.snd
      INC %nc.x
   }
   msg $target %nc.snd
   UNSET %nc.*
   HALT

The /unset is to clean up the mess we made creating variables, and the HALT was described earlier. We store $1- in a variable so we can easily work with it. If we don't, or set it inside the while loop, only the last found token will end up styled.


5. Step 4: Error checking  Back to Top

We need to check for two problems:
  • there is more than one nick matching the text-to-be-replaced, and
  • there is no nick matching it.
One thing at a time though. Or maybe not...


   IF ($ialchan($+($right($wildtok($strip($1-),~*,%nc.x,32),-1),$chr(42)),$target,0) != 1) {
      echo -a Error! Problem locating the nick on $right($wildtok($strip($1-),~*,0,32),-1) !!
      HALT
   }

Now you could of course split this into two bits, with two different error messages, but I'll leave that to you.
The $right() is used to remove the tilde from the found word. Note the annoying use of $+() in the <$ialchan() - that is so we turn the letters we typed (eg: abc) into a wildcard that $ialchan can use (eg: abc*)


6. Step 5: Grab a nick  Back to Top

Now lets mess it up. First we have to expand the few letters we have into a nick. The first variable here is the token number (which word in the text are we replacing), we'll need that later to replace the text we typed with the styled nick. The second variable is the nick which we can then style.


   SET %nc.tok $wildtok($strip($1-),~*,%nc.x,32)
   SET %nc.nck $ialchan($+($right($wildtok($strip($1-),~*,%nc.x,32),-1),$chr(42)),$target,1).nick


7. Step 6: The fun begins  Back to Top

So lets start. First we need to divide the nick into two halves. We could always cut the nick nicely in two equal pieces (beware of uneven-length nicks), or make the first part always 5 characters long (would be fun when the nick is only 3 characters long), or make it random. We will also need to decide whether or not to cut the nick from the left or right.

   SET %nc.sep $rand(1,$len(%nc.nck) - 1)

Now split it! The left part will be $left(nick,N), the right part will be $right(nick,-N).

   ; Watch it, this is ONE line, not three - so you need to combine them).

   SET  %nc.new $+($chr(3),02,$chr(42),$chr(3),12,$chr(42),$chr(3),04,
        $left(%nc.nck,%nc.sep),$chr(3),07,$chr(31),$right(%nc.nck,$+(-,%nc.sep)),
        $chr(31),$chr(3),12,$chr(42),$chr(3),02,$chr(42),$chr(3))

Now you may ask why the hassle with $chr(3) and $chr(42). Simple: ctrl+k doesn't display all too well using HTML, and since * is a wildcard, we better be safe than sorry. This bit looks like a mess, you might wanna actually use CTRKL+k and CTRL+u in your code. Be sure to NOT close your replacer with CTRL+o (end-of-everything) cause that will mess up any control codes you used in your text.


8. Step 7: Let's put the nick into our line  Back to Top

This part is rather simple: replace the old token with the new:

   SET %nc.snd $reptok(%nc.snd,%nc.tok,%nc.new,1,32)

The loop will automatically repeat this for all the lines. Now we can start testing!


9. Conclusion  Back to Top

I hope this little tutorial gave you the basic idea how to code a Nick Completer.
Finally you may also look at this working example of a Nick Completer.