----------------------------------------------------------
Title: "Applying Commands to Nicklist"
Author: Martyr2
Updated: December 11, 2000
Written for [#clan-x-scripterz]
----------------------------------------------------------
Believe it or not this is often one of the most asked style of questions on IRC. Hopefully this article will clear up a few things for newbies and show that applying a command (or various commands) to each member of a channel will be a very useful skill to have in all your scripting projects. This article assumes you have mIRC 5.7 or above because we will be using a recent addition the program, while looping. (You can read about while looping article on this site for more info on how to use it.)

As with all our articles, we will try to be as simple as possible with our examples while still providing enough useful info to allow scripters to take it to the next level. Below is a simple while loop which shows a basic format for applying a command to the nicklist. While in previous articles we shown each line after the example, it will be essential to explain it as we go. Each line that is proceded with a semicolon (;) is treated as a comment in mIRC. So they will not effect your script in anyway. The commands are in red and the command to apply to the nicklist is in purple. *Note: All you would have to do is change the purple command to a command you want applied to all nicknames and it will be perfect for whatever you want.

;First we will start out with a simple on join event.
;This triggers our while loop to start rotating through.

on *:JOIN:#:{
   ;Next we set a local variable to 1
   ;Not zero because we will never have a zero number of nicks.
   var %i = 1
   ;Next we set up the while loop. 1 is our start out position.
   ;And $nick(#channel,0) will be the TOTAL amount of nicks.
   while (%i <= $nick(#,0)) {
      ;In here is where we put the command to apply to our list.
      echo -a $nick(#,%i)
      ;Never EVER forget to inc the variable or you will be stuck
      ;in an infinite loop. (Use ctrl+break to stop it in case you do)
      inc %i
   }
}

You can use place multiple or single commands in the area for the purple. For ease of reading, I suggest putting each command underneath the previous one. By using a lot of pipes, you will soon find it hard to read and debug later. In some case you may need to modify various parts to apply the loop at different times. For instance, you may want to change the on join event to an on text event to respond to something someone said. You may also want to change the less than or equal to sign in the while loop to just less than.

But what I want you to pay extra attention to is how the actual loop is constructed and formatted. Master this format and you will find looping through the nicklist easy as pie. Again the steps are...

1. Set a local variable (I use %i) to 1
2. Use $nick(#,0) to represent the total amount of nicks in the channel.
3. Use a while loop to apply that command to each nick.
4. Use inc to increment the variable one. Don't FORGET!
5. Use the loop in any event dealing with the channel specificly.

When I mention channel specific I mean events that are channel activated. Some events that are NOT channel activated are: on NICK, on INPUT, on DNS, on CONNECT, on CHAT etc.

Adding a Touch of Color (Nicklist Colorer Example):

You have seen above how with a while loop and a little imagination you can apply commands to everyone in the nicklist. By changing, adding, or deleting commands in the while loop, we can create wonderful and exciting features to your script or addon. Using our example from above, I will demonstrate how you can apply the /cline command, along with a few checks of rank, to beautifully color your nicklist! So that the ops names are red, the voices are orange, and the rest are green. Good color combination?? Eh.. it will do I guess. Below is the example from above with a few modifications. Pay attention to the lines in the light shade of purple...

;First we will start out with a simple on join event.
;This triggers our while loop to start rotating through.

on *:JOIN:#:{
   var %i = 1
   while (%i <= $nick(#,0)) {
      if ($nick(#,%i) isop $chan) { cline 4 # $nick(#,%i) }
      elseif ($nick(#,%i) isvo $chan) { cline 7 # $nick(#,%i) }
      else { cline 3 # $nick(#,%i) }
      inc %i
   }
}

The code above simply says if the current person is an op, /cline ("color the line") with number 4 color; which is red. If the person is not an op, but is a voice, color the line with number 7 (orange) and if they are neither, color it with number 3 (green). Then inc %i and go back around for the next person.

When a person then joins, it should make a nice color coded nicklist. Now it only updates when someone joins and I didn't want to do more to it because of example purposes. If you decide you like this feature, may I suggest that you put the same loop and lines into an alias (replacing $chan and # with $$1 so that you can supply the alias with a channel) and have it call this alias with a timer every few seconds. If you are still struggling with what I am talking about, please feel free to visit our channel and ask for help or look in the help file under aliases for more information on using $$1.

Conclusion:

Applying a command or set of commands to a nicklist involves local variable set to 1, a while loop to rotate through the nicknames, a specific channel event and inc to increment the variable. Make sure the event and commands are channel specific. Always practice good scripting form and comment your work. It will help you tremendously later. I hope you enjoyed the article and learned something new. Please feel free to read more of Clan-X-scripterz articles or join us at #clan-x-scripterz on DALnet IRC Network.

.::(Martyr2)::. © 2000 Clan-X-Scripterz