SUNiNET - Your Eggdrop & TCL resources site
[ Home Eggdrop TCL Links About ]

Guide to TCL scripting for Eggdrop 1.6

[ Previous ] [ Index ] [ Next ]
[ Text version ]


12. Miscellaneous

This chapter will cover various minor things that aren't big enough to have their own chapter, but are important and usefull enough to be mentioned.

12.1 Timers

You can also make your script execute something after a certain number of minutes or seconds.
This can be done with the timer and utimer commands.
The syntax of a timer command is timer <time> "<command> [parameters]".

The <time> is after how long your command should be executed.
If you are using timer this would be have to be in minutes and if you are using utimer this would have to be in seconds.

The <command> is the command you want to execute.
This can be a TCL command, an Eggdrop command or anything else just as long as it's a valid command.

The [parameters] are the parameters you want to give to your command.

With the commands timers and utimers you can see a list of the timers that are currently active. These commands do not need any parameters and return a list of which each object contains information about one of the timers that is currently running.

Note that timers are so called evaluated which means that if you have $foo that contains for instance "[foo]" and than call the timer with utimer 30 "test $foo" it would try to execute the command "foo" since it's enclosed in brackets and send the output of the command "foo" to the command as a parameter instead of what the string contains.
So be extra carefull when you are using timers and escape any brackets in your strings with a command like list or by putting backslashes in front of the brackets in the string itself like this: set foo "\\\[foo\\\]" which would result in $foo containing "\[foo\]".

Because of this evaluation it is best to keep the syntax of the timer commands as timer <time> [list <command> <parameters>]" when you are sending parameters to the procedure to prevent any errors from occuring, better safe than sound.

12.2 Random numbers

You can get a random number with the rand command.
The syntax of a rand command is rand <number>.

The <number> is the number possibilities from which you want rand to return a number.
If you want 2 possibilities you will have to use rand 2, but rand starts counting at zero so this will either return a 0 or a 1. Thus the return of a rand command is a random number between 0 and (<number> - 1).

For example rand 10 would give a random number between 0 and 9.

12.3 Checking for strings

When you try to access a string that doesn't exist TCL will give an error, because of that being able to check if a string exist in advance is very usefull. This can be done with the info exists command.
The syntax of a info exists command is info exists <string>.

The <string> is the name of the string you want to check.
If your string is an array you can either just check if the array exists or check if a specific string within the array exists, so both "test" for the array and "test(what)" for the string "what" in the array "test" specifcally are both valid.

The info exists command returns a 1 if the string exists and a 0 if it doesn't.
For example, if you would do set test(foo) "bar" and you would use info exists test it would result in a 1 and info exists test(foo) would result in a 1 aswell, so you can either check the array itself or a string inside of it.

12.4 Advanced math

You already know the incr command but sometimes you might want to do a bit more than just adding up and substracting from numbers.
You can perform more advanced math with the expr command.
The syntax of a expr command is expr <calculation>.

The <calculation> is what you want to calculate.
You can make the calculation as long as you like. The normal computer characters apply, which are a plus sign (+) for adding up, a minus sign (-) for substracting from, an asterix (*) for multiplying, a slash (/) for dividing and a percent sign (%) for the remainder.
The expr command goes through the calculation in the "official" way which means that asterix's are handle before the plus signs for example.

The expr command can do a lot more than just calculating, but that won't be discussed here.
You can find more about these other functions in the man pages of TCL to which a reference is in Chapter 3.4.
Also note that the expr command evaluates it's input like timer and utimer, therefor you must escape any brackets and such if you want those into your calculation.

12.5 Text decoration

It's also possible to decorate your text a little bit.
Like mentioned in Chapter 3.3 you can use special codes to underline your text for exmaple.
Besides underlining you can also make it bold and give it a color. You use these codes by just putting them between the pieces of text you want decorated.

The code for underlining is \037, the code for bold is \002 and the code for colors is \003 which is followed by the number of the color.
The newest versions of mIRC have a nice pop-up box that shows up when you type CTRL+K to make a color. The numbers inside the colors are the same numbers to use here in the color code.

You start and end the places where you want your decoration to start and stop with the same code you started the text decoration with.
For example "Hello \002$nick\002, welcome in $chan." would result in only "$nick" being in bold or "Hello \002\0034$nick\003\002, welcome in \037$chan\037." would result in "$nick" being in bold and in the color red and "$chan" underlined.
Note that when the end of the color decoration is defined only the code itself needs to be specified and not the number of the color, if you specify the number of the color again it would just make the text switch to that color again and not show any difference.

12.6 Checking user flags

At some points you might want to check if a user has a certain flag before you want the bot to execute something, you could want somebody to be at least a master for example.
This can be done with the matchattr command.
The syntax of a matchattr command is matchattr <handle> <flag(s)> [channel].

The <handle> is the name of the user you want to check, this has to be the nickname in which the bot knows the user and not the current nickname of the user.

The <flag(s)> are the flags you want to check.
This can be a number of flags or just one.
Note that if you use "of" for instance as <flag(s)> the bot will check if the user has +o OR +f, not +o AND +f.
You can seperate the different flags with a | or & to define whether you want the bot the check if the user has all those flags or just one of them, so "o&f" would make the bot check if the user has +o AND +f.

The [channel] is of which channel the flag will be checked.
Note that if you specify a channel, only the channel flags of the user are checked and not the global flags, so a global moderator without a +o channel flag would turn up negative in this command unless you make it "+o OR +o for #chan".

For example, matchattr $hand o|o $chan would check if the user is a global operator or a channel operator of $chan, matchattr $hand o&m $chan would check if the user is a global operator and a master of $chan.

12.7 Catching errors

A very usefull command is catch. With this command you can execute something and catch the error, if there is one and prevent your script from crashing.
Under some circumstances it just might be easier to just use the catch command instead of a bunch of checks.
The syntax of a catch command is catch <command>.

The <command> is what you want to execute, including the parameters.
Just make it exactly how you would've executed it if you weren't using the catch command.

The catch command will return a 0 if there were no errors and a positive number otherwise.

For example if you were to unbind the dcc command die, it is better to do it like catch { unbind dcc n die *dcc:server:die }, because if your bot is not connected to IRC you'd have to use catch { unbind dcc n die *dcc:die }.
This way you can easily unbind the die command without trouble, because if you try to unbind a command that doesn't exist your bot will give an error and crash.


[ Text version ]
[ Previous ] [ Index ] [ Next ]


Design & Graphics by Shawn Borton
Copyright © 2000-2005 Marijn van Zon