Kalleth
28-05-2002, 19:50
(In all my guides, in syntax, things in brackets are optional.)
IRC Help and scripting guide #5 !
Okay, its been a while since the last one, so perhaps i should think about doing one more.
i dont really have that much time right now, so i'll just do one sample script, but a long one.
First, seeing as GameGuru isnt around, i'll explain a few things i'll be using in this script.
Command: $read()
Used To: Return a single line from a file.
Syntax: $read(filename,[ntsw],[matchtext],[N])
where filename is the name of the file you want to get the line from
ntsw is a set of flags, which are not that important really (not used much, /help /$read if you REALLY need to learn about them)
matchtext is a string to search for in a file
and N is a number, usually a line number.
Examples:
$read(filename.txt) would return a random line from filename.txt
$read(filename.txt,6) would return the 6th line from filename.txt
Command: /write
Used To: Write lines to a text file.
Syntax: /write [-cida l# s# w#] filename [text]
where -cida l# s# w# are a set of flags, to read up on the flags, type in mIRC /help /write, its at the bottom
Examples:
/write filename.txt hello would write "hello" to the end of filename.txt. if the file does not exist, it will be created and [text] will be the first line.
/write -dl40 filename.txt would delete line 40 from filename.txt
/write -l5 filename.txt hello would overwrite line 5 in filename.txt with the word "hello".
i will also be using if then else's and while statements - i'll explain them as we go through, but i think they were explained in more detail in one of GameGuru's other guides.
The script i will be showing you is a !quote script - one that is actually useful for a change :).
so, what do we want to do first? Well, we want the script to trigger when someone types !quote in a channel.
So, we do this:
on *:TEXT:!quote*:#: {
you *should* all understand what this means if you've read the other guides.
First, we want to check if there are any other words with the !quote statement. If there arent, we want to message the channel a random line from the quote file. i shall store the name of the quotes file in the variable %quotefile.
IF ($2 == $null) {
msg $chan [RANDOM QUOTE] $read(%quotefile)
}
That was pretty easy. Thats the script to just return a random quote. (i'll cover spam protection at the end).
Now, secondly, we want to check if they specified a line number, and if they did, return that line.
ELSEIF ($2 isnum) {
msg $chan [QUOTE $2 $+ ] $read(%quotefile,$2)
}
This needs some explanation - i say ELSEIF (regarding the if statement we started above) the second part is a number, (isnum is an operator that means is a number, ie if $2 is a number) message the channel QUOTE #number followed by the line they specified from the quotes file.
The next bit is slightly more complex. If they specified a word, we want to return a random line that contains that word from the quotes file. Seeing as we have covered the only other possibilities for the 2nd part of the !quote thing they said above, here we can just use ELSE.
ELSE {
var %i 1
while (%i <= $lines(%quotefile)) {
if ($2 isin $read(%quotefile,%i)) {
write tempfile.tmp $read(%quotefile,%i)
}
inc %i
}
msg $chan [QUOTE: $2 $+ ] $read(tempfile.tmp)
.remove tempfile.tmp
}
}
okay, now why do i feel i need to explain this bit?
:P
okay, the first line sets the local variable (one that is unset when the script finishes) %i to 1.
The second says that the following code will execute until the variable %i reaches the same number as the number of lines in the quote file.
The third says that if the searched-for word is in the %i'th line, write it to a temporary file.
Then, it increments %i (increases it by 1) and the while loop repeats unless %i has reached the total number of lines in the quotefile, ie all lines have been checked.
Once this has happened, the script messages the channel a random line from the temporary file, then removes the temporary file.
The entire code for this script is this:
on *:TEXT:!quote*:#: {
IF ($2 == $null) {
msg $chan [RANDOM QUOTE] $read(%quotefile)
}
ELSEIF ($2 isnum) {
msg $chan [QUOTE $2 $+ ] $read(%quotefile,$2)
}
ELSE {
var %i 1
while (%i <= $lines(%quotefile)) {
if ($2 isin $read(%quotefile,%i)) {
write tempfile.tmp $read(%quotefile,%i)
}
inc %i
}
msg $chan [QUOTE: $2 $+ ] $read(tempfile.tmp)
.remove tempfile.tmp
}
}
Now, there are a few things still missing.
Firstly, there are, unfortunately, llamas on IRC. They will abuse your script to try and get you to flood out. There is a way to prevent this.
We just add a couple of checks, so our code looks like this:
on *:TEXT:!quote*:#: {
if (%quote.flood == $null) {
IF ($2 == $null) {
msg $chan [RANDOM QUOTE] $read(%quotefile)
}
ELSEIF ($2 isnum) {
msg $chan [QUOTE $2 $+ ] $read(%quotefile,$2)
}
ELSE {
var %i 1
while (%i <= $lines(%quotefile)) {
if ($2 isin $read(%quotefile,%i)) {
write tempfile.tmp $read(%quotefile,%i)
}
inc %i
}
msg $chan [QUOTE: $2 $+ ] $read(tempfile.tmp)
.remove tempfile.tmp
}
set -u5 %quote.flood on
}
}
Basically, i added another if statement around everything to say that the script inside it should execute ONLY if the variable %quote.flood is null, ie if it doesnt exist. Then, after the script has messaged the channel, i set the variable %quote.flood to on for 5 seconds. (-u<time before it unsets itself>)
Finally, we need some way of setting what the quotes file is!
Just do this via a simple popup that will appear when you right click in a channel. This also goes in remotes with the script above.
menu channel {
Set Quotefile:/set %quotefile $$?="Enter the filename of the quotes file."
}
The quotes file must be in the mIRC directory.
Therefore this is the code, in its entirety. Copy and paste into mIRC remotes window (alt+r):
on *:TEXT:!quote*:#: {
if (%quote.flood == $null) {
IF ($2 == $null) {
msg $chan [RANDOM QUOTE] $read(%quotefile)
}
ELSEIF ($2 isnum) {
msg $chan [QUOTE $2 $+ ] $read(%quotefile,$2)
}
ELSE {
var %i 1
while (%i <= $lines(%quotefile)) {
if ($2 isin $read(%quotefile,%i)) {
write tempfile.tmp $read(%quotefile,%i)
}
inc %i
}
msg $chan [QUOTE: $2 $+ ] $read(tempfile.tmp)
.remove tempfile.tmp
}
set -u5 %quote.flood on
}
}
menu channel {
Set Quotefile:/set %quotefile $$?="Enter the filename of the quotes file."
}
I Do hope this helps you with your mIRC scripting.. and yes, i will try and get another guide up soonish :P
-----------------------------------[GameGuru's Stuff]-----------------------------
Well, here was my original part 5, Kalleth obviously forgot I'd done it :P
Part 5 - The basics - Timers and Variables
For no particular reason, I've banded Timers and variables together in one tutorial.
mIRC allows you to create a limitless number of timers using the basic syntax:
/timer[name] [flags] [time of activation] <repetitions> <interval> <command>
We can create a simple timer to echo "Hello" to our active mIRC window after 5 seconds by typing this in the editbox:
/timer 1 5 echo -a Hello
This will cause mIRC to create a new Timer (which it will call Timer1 (or TimerX where X is the next available free timer number)) which will perform the command specified once and after 5 seconds.
A simple use for a timer is shown below:
;This event puts the current time in the mIRC titlebar and updates it every second indefinitely
on *:START: {
.timer -com 0 1000 titlebar $!time
}
Quick rundown of the code, -com makes mIRC keep the timer accurate, forces it to run whether you are on a server or not, and lets you specify milliseconds instead of seconds respectively. The 0 means the timer will repeat indefinately every 1000 milliseconds (one second) and $!time means the titlebar will have the current time placed in it. You can see the difference between $!time and $time by removing it/adding it and restarting mIRC.
Variables allow you to store values of any type, you can even store dynamic values (like identifiers or other variables) and there are two types, local and global. Local variables are created using the /var command and are automatically removed when the script that uses them finishes whereas global variables created by typing /set remain even after the script that creates them ends and can be removed using /unset. All variables in mIRC must be prefixed with a % but can have any name you like (and can even be created dynamically using variables and identifiers.)
Variables can also be created and be told to unset themselves after a specific amount of time, this is done by adding a -u<time> flag to the set command. This works much in the same way as creating a timer which performs the unset command on a variable.
;Example of usage of local and global variables
on ^*:TEXT:*:#wireplay: {
%text = $1-
set %name = $nick
var %numberofwords = $0
echo $chan $timestamp < $+ %name $+ > %text
echo $chan There were %numberofwords said in that line
}
alias displayinfo
{
echo -a last recorded line was: " $+ %text $+ " spoken by %name
}
%numberofwords could not be used in the displayinfo alias as it was only a local variable, if I attempted to display it $null would be returned, which would not be displayed in an echo.
[edit - ahem ahem, fixed error ;0 + added GG's stuff]
IRC Help and scripting guide #5 !
Okay, its been a while since the last one, so perhaps i should think about doing one more.
i dont really have that much time right now, so i'll just do one sample script, but a long one.
First, seeing as GameGuru isnt around, i'll explain a few things i'll be using in this script.
Command: $read()
Used To: Return a single line from a file.
Syntax: $read(filename,[ntsw],[matchtext],[N])
where filename is the name of the file you want to get the line from
ntsw is a set of flags, which are not that important really (not used much, /help /$read if you REALLY need to learn about them)
matchtext is a string to search for in a file
and N is a number, usually a line number.
Examples:
$read(filename.txt) would return a random line from filename.txt
$read(filename.txt,6) would return the 6th line from filename.txt
Command: /write
Used To: Write lines to a text file.
Syntax: /write [-cida l# s# w#] filename [text]
where -cida l# s# w# are a set of flags, to read up on the flags, type in mIRC /help /write, its at the bottom
Examples:
/write filename.txt hello would write "hello" to the end of filename.txt. if the file does not exist, it will be created and [text] will be the first line.
/write -dl40 filename.txt would delete line 40 from filename.txt
/write -l5 filename.txt hello would overwrite line 5 in filename.txt with the word "hello".
i will also be using if then else's and while statements - i'll explain them as we go through, but i think they were explained in more detail in one of GameGuru's other guides.
The script i will be showing you is a !quote script - one that is actually useful for a change :).
so, what do we want to do first? Well, we want the script to trigger when someone types !quote in a channel.
So, we do this:
on *:TEXT:!quote*:#: {
you *should* all understand what this means if you've read the other guides.
First, we want to check if there are any other words with the !quote statement. If there arent, we want to message the channel a random line from the quote file. i shall store the name of the quotes file in the variable %quotefile.
IF ($2 == $null) {
msg $chan [RANDOM QUOTE] $read(%quotefile)
}
That was pretty easy. Thats the script to just return a random quote. (i'll cover spam protection at the end).
Now, secondly, we want to check if they specified a line number, and if they did, return that line.
ELSEIF ($2 isnum) {
msg $chan [QUOTE $2 $+ ] $read(%quotefile,$2)
}
This needs some explanation - i say ELSEIF (regarding the if statement we started above) the second part is a number, (isnum is an operator that means is a number, ie if $2 is a number) message the channel QUOTE #number followed by the line they specified from the quotes file.
The next bit is slightly more complex. If they specified a word, we want to return a random line that contains that word from the quotes file. Seeing as we have covered the only other possibilities for the 2nd part of the !quote thing they said above, here we can just use ELSE.
ELSE {
var %i 1
while (%i <= $lines(%quotefile)) {
if ($2 isin $read(%quotefile,%i)) {
write tempfile.tmp $read(%quotefile,%i)
}
inc %i
}
msg $chan [QUOTE: $2 $+ ] $read(tempfile.tmp)
.remove tempfile.tmp
}
}
okay, now why do i feel i need to explain this bit?
:P
okay, the first line sets the local variable (one that is unset when the script finishes) %i to 1.
The second says that the following code will execute until the variable %i reaches the same number as the number of lines in the quote file.
The third says that if the searched-for word is in the %i'th line, write it to a temporary file.
Then, it increments %i (increases it by 1) and the while loop repeats unless %i has reached the total number of lines in the quotefile, ie all lines have been checked.
Once this has happened, the script messages the channel a random line from the temporary file, then removes the temporary file.
The entire code for this script is this:
on *:TEXT:!quote*:#: {
IF ($2 == $null) {
msg $chan [RANDOM QUOTE] $read(%quotefile)
}
ELSEIF ($2 isnum) {
msg $chan [QUOTE $2 $+ ] $read(%quotefile,$2)
}
ELSE {
var %i 1
while (%i <= $lines(%quotefile)) {
if ($2 isin $read(%quotefile,%i)) {
write tempfile.tmp $read(%quotefile,%i)
}
inc %i
}
msg $chan [QUOTE: $2 $+ ] $read(tempfile.tmp)
.remove tempfile.tmp
}
}
Now, there are a few things still missing.
Firstly, there are, unfortunately, llamas on IRC. They will abuse your script to try and get you to flood out. There is a way to prevent this.
We just add a couple of checks, so our code looks like this:
on *:TEXT:!quote*:#: {
if (%quote.flood == $null) {
IF ($2 == $null) {
msg $chan [RANDOM QUOTE] $read(%quotefile)
}
ELSEIF ($2 isnum) {
msg $chan [QUOTE $2 $+ ] $read(%quotefile,$2)
}
ELSE {
var %i 1
while (%i <= $lines(%quotefile)) {
if ($2 isin $read(%quotefile,%i)) {
write tempfile.tmp $read(%quotefile,%i)
}
inc %i
}
msg $chan [QUOTE: $2 $+ ] $read(tempfile.tmp)
.remove tempfile.tmp
}
set -u5 %quote.flood on
}
}
Basically, i added another if statement around everything to say that the script inside it should execute ONLY if the variable %quote.flood is null, ie if it doesnt exist. Then, after the script has messaged the channel, i set the variable %quote.flood to on for 5 seconds. (-u<time before it unsets itself>)
Finally, we need some way of setting what the quotes file is!
Just do this via a simple popup that will appear when you right click in a channel. This also goes in remotes with the script above.
menu channel {
Set Quotefile:/set %quotefile $$?="Enter the filename of the quotes file."
}
The quotes file must be in the mIRC directory.
Therefore this is the code, in its entirety. Copy and paste into mIRC remotes window (alt+r):
on *:TEXT:!quote*:#: {
if (%quote.flood == $null) {
IF ($2 == $null) {
msg $chan [RANDOM QUOTE] $read(%quotefile)
}
ELSEIF ($2 isnum) {
msg $chan [QUOTE $2 $+ ] $read(%quotefile,$2)
}
ELSE {
var %i 1
while (%i <= $lines(%quotefile)) {
if ($2 isin $read(%quotefile,%i)) {
write tempfile.tmp $read(%quotefile,%i)
}
inc %i
}
msg $chan [QUOTE: $2 $+ ] $read(tempfile.tmp)
.remove tempfile.tmp
}
set -u5 %quote.flood on
}
}
menu channel {
Set Quotefile:/set %quotefile $$?="Enter the filename of the quotes file."
}
I Do hope this helps you with your mIRC scripting.. and yes, i will try and get another guide up soonish :P
-----------------------------------[GameGuru's Stuff]-----------------------------
Well, here was my original part 5, Kalleth obviously forgot I'd done it :P
Part 5 - The basics - Timers and Variables
For no particular reason, I've banded Timers and variables together in one tutorial.
mIRC allows you to create a limitless number of timers using the basic syntax:
/timer[name] [flags] [time of activation] <repetitions> <interval> <command>
We can create a simple timer to echo "Hello" to our active mIRC window after 5 seconds by typing this in the editbox:
/timer 1 5 echo -a Hello
This will cause mIRC to create a new Timer (which it will call Timer1 (or TimerX where X is the next available free timer number)) which will perform the command specified once and after 5 seconds.
A simple use for a timer is shown below:
;This event puts the current time in the mIRC titlebar and updates it every second indefinitely
on *:START: {
.timer -com 0 1000 titlebar $!time
}
Quick rundown of the code, -com makes mIRC keep the timer accurate, forces it to run whether you are on a server or not, and lets you specify milliseconds instead of seconds respectively. The 0 means the timer will repeat indefinately every 1000 milliseconds (one second) and $!time means the titlebar will have the current time placed in it. You can see the difference between $!time and $time by removing it/adding it and restarting mIRC.
Variables allow you to store values of any type, you can even store dynamic values (like identifiers or other variables) and there are two types, local and global. Local variables are created using the /var command and are automatically removed when the script that uses them finishes whereas global variables created by typing /set remain even after the script that creates them ends and can be removed using /unset. All variables in mIRC must be prefixed with a % but can have any name you like (and can even be created dynamically using variables and identifiers.)
Variables can also be created and be told to unset themselves after a specific amount of time, this is done by adding a -u<time> flag to the set command. This works much in the same way as creating a timer which performs the unset command on a variable.
;Example of usage of local and global variables
on ^*:TEXT:*:#wireplay: {
%text = $1-
set %name = $nick
var %numberofwords = $0
echo $chan $timestamp < $+ %name $+ > %text
echo $chan There were %numberofwords said in that line
}
alias displayinfo
{
echo -a last recorded line was: " $+ %text $+ " spoken by %name
}
%numberofwords could not be used in the displayinfo alias as it was only a local variable, if I attempted to display it $null would be returned, which would not be displayed in an echo.
[edit - ahem ahem, fixed error ;0 + added GG's stuff]