Lesson 2: Triggering EventsTo activate a script, something has to be triggered. The most common way to way to trigger a script is:
- Code: Select all
on *:TEXT:*:#: {
This means that a message from someone else will activate your script. If you don't know yet, * is a wildcard meaning "everything." If you might have guessed, the
event trigger above will be activated when anyone says anything. The first * has to do with users which are slightly complicated, so always leave it as *. The second * can specify which text you want to trigger your script, but it's clumsy and just leave it as *. The # means all channels. You can change this to:
- Code: Select all
on *:TEXT:*:#isml: {
...and that script will only activate if the text is said in #isml. Note that this doesn't specify the server, so as long as there is a channel called #isml it will activate.
You notice that you are opening a bracket { in a non-tabbed position. This is important. At some point in the script, you will have to close it.
So then, what do you want to script? Let's say you want to say something every time someone says the word "gay" in any channel. Now we use what is called an if-check:
- Code: Select all
on *:TEXT:*:#: {
if (gay isin $1-) {
Note that we are in another level, which means one tab. (We say tab but in reality we're actually moving in 2 spaces.) This means that in order for the if-check to be activated, the first level (on *:TEXT:*:#:) has to be satisfied. The on TEXT specifies that "gay" has to be something that is said in a channel, not over a query, not in an emote, and can't be something you yourself typed in.
Now let's look at the if-check, which is a
statement. Every time you use if, you open a parenthesis. The
isin is called an
operator. The
$1- is called an
identifier, and it stands for the whole line that someone said. The if statement says, "if the word 'gay' is in all of what someone says, then do something." So what are we doing?
- Code: Select all
on *:TEXT:*:#: {
if (gay isin $1-) {
msg $chan No, you're gay.
}
}
The command itself is in the third level, which means both the first and second levels have to be satisfied for the script to output "No, you're gay." to the channel. Notice that I closed both parentheses at their respective levels. That indicates that I'm done with the if-check and also done with the event. If you are wondering,
msg is how to type to the channel using scripts, and
$chan is an idenfier which stands for the channel that activated the script. If you are in #isml and someone said gay in #yumsauce, then the response will be outputted to #yumsauce.
Now what if you want to trigger off another word? You have to watch the placement of your next if-check:
- Code: Select all
on *:TEXT:*:#: {
if (gay isin $1-) {
msg $chan No, you're gay.
}
if (fuck isin $1-) {
msg $chan Watch your language you fucking retard.
}
}
The next if-check is place after the first }, which means that it is not part of the last if-check. The gay is something else and the fuck is something else. But don't be confused. If the sentence has both gay and fuck, then both both messages will be outputted. If the sentence only has fuck, then only the second message will be outputted. But if the script is:
- Code: Select all
on *:TEXT:*:#: {
if (gay isin $1-) {
msg $chan No, you're gay.
if (fuck isin $1-) {
msg $chan Watch your language you fucking retard.
}
}
}
Then the second message won't be outputted unless the first statement is satisfied. In short, if the sentence only has gay, then the script will say the first message. If the sentence has both gay and fuck, then the script will say both messages. But if the sentence only has fuck, then nothing happens, because the gay if-check has to be satisfied. This is why if you are finished with an if statement, then close its bracket.
Other event triggers include:
on *:ACTION:*:#: { }
-The event will trigger on an emote (/me).
on *:INPUT:#: { }
-The event will trigger only on things you type in.
on *:JOIN:#: { }
-The event till trigger when someone joins the channel.
on *:BAN:#: { }
-The event will trigger when someone is banned.
on *:MODE:#: { }
-The event will trigger when a channel mode is change.