If you read a file the way you did it, basically the bot will load all that file content in the RAM memory of the PC it's running on and that's a bad idea if the file is big.
I've taken a different approach and used 'wc -l
file' to count the number of lines that particular file has, create a random number and use the
fetchQuote proc, that while reads the file one line at a time it doesn't store anything until it finds the exact line number we are looking for and stores only that line in RAM memory. Each time the bot is rehashed, the line count is also refreshed so this takes care of keeping the file up-to-date.
Code: Select all
namespace eval lovequotes {
variable version "0.5"
variable quote(count) [exec wc -l $quote(file)]
bind pub - "!lovequotes" [namespace current]::randomQuote
proc randomQuote {nick uhost hand chan text} {
variable quote
set quote [fetchQuote [rand $quote(count) +1]]
puthep "PRIVMSG $chan :$quote"
}
proc fetchQuote {no} {
variable quote
if {[catch {open $quote(file) r} fp]} {
putlog "Failed to open $quotefile, got error: $fp"
close $fp
return
}
set i 0
while {[gets $fp line] != -1} {
if {$i == $no} { break } else { incr i }
}
close $fp
return $line
}
putlog "Loaded: LoveQuotes v$version"
}
There may be other methods to read a specific line of that file, so I'm open for suggestions.
PS: I did only a few tests in tclsh, so don't test this on a dummy bot.
Edit: Another approach is by using bash commands exclusively by replacing
fetchQuote with this:
Code: Select all
proc fetchQuote {no} {
variable quote
if {[file exists $quote(file)]} {
return [exec sed -n "$no p" $quote(file)]
}
}
Later edit: Corrected a typo.
Once the game is over, the king and the pawn go back in the same box.