Basic TCL examples

From RacHelp

Jump to: navigation, search

Here are some basic examples of using TCL with Racbot. Feel free to contribute to this page.

Contents

[edit] Example 1 : Public commands

When somebody uses the command !hugme, the bot will hug the person.

bind pub - hugme lovebot
proc lovebot {nick userhost handle channel language speech} {
  putserv "PRIVMSG $channel :\001ACTION hugs $nick \001"
}

[edit] Example 2 : Public messages

When a someone says a sentence conatining the word cheese on the channel, the bot will say I love cheese!

bind pubm - cheese mousey
proc mousey {nick userhost handle channel speech} {
  putserv "PRIVMSG $channel :i love cheese!"
}

[edit] Example 3 : Checking input

Will check the whether the given string is composed of letters or numbers when somebody uses !checkme STRING

bind pub - checkme check_string
proc check_string {nick userhost handle channel language speech} {
  if {[string is alpha $speech]} {
    putserv "PRIVMSG $channel :This string is made of letters"
  } elseif {[string is digit $speech]} {
    putserv "PRIVMSG $channel :This string is made of numbers"
  } elseif {[string is alnum $speech]} {
    putserv "PRIVMSG $channel :This string is made of letters and numbers"
  } else {
    putserv "PRIVMSG $channel :This string contains non alphanumeric characters"
  }
}

[edit] Example 4 : Writing to a file

This example will print hello world! to somefile.txt twice on the first line, then twice on new lines following it when somebody uses the command !hithere. If the file exists, it will be overwritten, if it does not exist, it will be created.

bind pub - hithere do_helloworld
proc do_helloworld {nick userhost handle channel language speech} {
  set myfile [open somefile.txt w]
  puts $myfile "hello world!"
  puts -nonewline $myfile "hello world"
  puts $myfile "hello world!"
  puts $myfile "hello world!"
  flush $myfile
  close $myfile
}

[edit] Example 5 : Reading from a file

This example will print the first line of yummeh.txt to the channel when the command !food is used.

bind pub - food whatsinthebox
proc whatsinthebox {nick userhost handle channel language speech} {
  set thefoodbox [open yummeh.txt r]
  gets $thefoodbox omgwhatisit
  close $thefoodbox
  putserv "PRIVMSG $channel :$omgwhatisit"
}

[edit] Example 6 : Voicing users with capitalised nicknames

This example will voice all users with capitalised nicknames who join any channel the bot is an operator in.

bind join - * voiceuser
proc voiceuser {nick userhost handle channel} {
	set firstletter [string range $nick 0 0]
	if {[string is upper -strict $firstletter]} {
		putserv "MODE $channel +v $nick"
	}
}

[edit] See also

The TCL commands manual

In depth TCL tutorial

Racbot TCL commands

Racbot TCL bindings

Tcl compatibility notes

Views
Personal tools