Is there a reason for complicating things and not using the example in
Basic File Operations?
Code: Select all
# open file for read access
set fp [open $filename "r"]
# read in all of the data
set data [read -nonewline $fp]
# split the data into lines
set lines [split $data "\n"]
# close the file, since we're done reading
close $fp
# count the number of lines
set numlines [llength $lines]
# grab a random line
set num [expr {int(rand()*$numlines) + 1}]
# grab the random picked line from the list
set randline [lindex $lines $num]
puthelp "PRIVMSG $ch :$randline"
I added the random part cos [rand 10] for instance starts from 0 instead of 1 and will go up to 9 instead of 10. But if you insist into using rand then make it
[expr [rand $numlines] +1] to fix the randomness. In Spike's example would be
[expr [rand $msglist] +1]
If you want to spice things up a bit by adding the name of the person that joined or the channel inside the line you are reading you can adjust your lines to have them in a format for example:
Code: Select all
hello %nick, welcome to %chan channel..
shhh! %nick joined, let's stop talking about him in %chan channel xD
... and so on
then that:
line becomes:
Code: Select all
puthelp "PRIVMSG $ch :[string map [list %nick $nk %chan $ch] $randline]"
and the bot will substitute %nick with the actual nick and %chan with the actual channel name.
Once the game is over, the king and the pawn go back in the same box.