IRC Scripting Basics: Part 4
By Richard “GameGuru” Grant
Some Identifiers & If-Then-Else Statements
mIRC has an almost endless number of identifiers. They are used (like the name suggests) to identify things, and can be identified (ooer) by having a prefix of $. This tutorial will give basic uses of some simple identifiers, and show how they can be used in events and if statements to make specific choices and refine scripts.
Recall that an event is constructed using the following statement:
<prefix> <user level>:<event>:<window>:<commands>
In order to make choices within an event we need to create an if then else statement. If you’ve ever done programming before you’re gonna know exactly what an if then else statement is and how it works. But for anyone unaware of how it is constructed you can see the syntax below:
if ( v1 operator v2¹ ) { commands }²
elseif ( v1 operator v2 ) { commands }
else { commands }
¹: v2 is not used in unary operators
²: Code following is not necessary
v1 and v2 are values (identifiers/variables/text etc) and an operator is the comparison which will return a boolean result (true or false.) Now we have the syntax for constructing events and if then else statements, lets create a (useless) example involving some identifiers.
ON *:JOIN:#: {
if ( $nick == MyFriend ) {
msg $chan Hello $nick $+ ! Good to see you again
}
}
Now, whenever “MyFriend” joins a channel you currently are in, he will be greeted with the following message: “Hello MyFriend! Good to see you again” in the channel he just joined.
mIRC follows this by looking each time when someone joins a channel for the channel and name of the person who just joined. $chan is then replaced with the current channel (say #friends for instance) and $nick is replaced with the persons nickname (MyFriend in this example.) $+ is used to concatenate identifiers with words. in this case the nickname and ! (since u can’t have $nick!)
mIRC has a really useful generic identifier for examing whole lines of text or specific words within it. By using $1- you can reference a whole line of text ($1- translates to the first word to the last) or specific words like $2, $4, $20 etc. you can even do ranges of words using $1-10 for the first 10 words (or any other combination). This can be useful when creating bots and having them understand simple commands. The code below shows how to write code to allow people to get them to be opped by you.
on *:TEXT:#: {
if ( $1 == OP ) {
mode # +o $nick
}
}
This code checks if the first word the user said is op, and if it is then you will op the user. Note that # and $chan perform the same job in the two examples, and are interchangeable. $chan can be more complex though, as you can add extra parameters to specify a particular channel or get information from channels.
This pretty much covers use of identifiers and If-then-else statements in mIRC.