This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

A Short Joke Script

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
EVRAMP
Voice
Posts: 7
Joined: Fri Apr 28, 2006 9:13 am
Location: /home/czech_republic/

A Short Joke Script

Post by EVRAMP »

Hi,
i made script which sends jokes to channel form file joke.txt, but it doesnt work.
Here is source:

Code: Select all

bind pub - !joke pub:joke
bind pub w !addjoke pub:addjoke

proc pub:addjoke { nick uhost handle channel arg } {
	if { $arg == "" } {
		putserv "NOTICE $nick :USAGE: !addjoke <joke>"
		return 0
	}
	set joke [open "joke.txt" a]
	puts $joke "$arg"
	close $joke
	putserv "NOTICE $nick :joke added"
}

proc pub:joke { nick uhost handle channel arg } {
	set jokefile [open "joke.txt" r]
	set i 0
	while { [eof $jokefile] != 1 } {
		incr i 1
		set joke($i) [gets $jokefile]
	}
	set w [rand $i]
	set outjoke $joke($w)
	putserv "PRIVMSG $channel :$outjoke"
}

putlog "joke.tcl successfully loaded"
Please can you help me where i did mistake? Thx!
m
metroid
Owner
Posts: 771
Joined: Wed Jun 16, 2004 2:46 am

Post by metroid »

What's the error then?
User avatar
EVRAMP
Voice
Posts: 7
Joined: Fri Apr 28, 2006 9:13 am
Location: /home/czech_republic/

Post by EVRAMP »

the one and only error is that it doesnt work. bot doesnt send a joke to channel x|
User avatar
DragnLord
Owner
Posts: 711
Joined: Sat Jan 24, 2004 4:58 pm
Location: C'ville, Virginia, USA

Post by DragnLord »

post the information from

Code: Select all

.set errorInfo
you need to show the error(s) you get
User avatar
EVRAMP
Voice
Posts: 7
Joined: Fri Apr 28, 2006 9:13 am
Location: /home/czech_republic/

Post by EVRAMP »

Tcl error [pub:joke]: can't read "joke(0)": no such element in array
m
metroid
Owner
Posts: 771
Joined: Wed Jun 16, 2004 2:46 am

Post by metroid »

it's because your incr before setting it.

Place the incr after it and it should work fine
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

why not simply use 'lappend joke [gets $jokefile]' and '[lindex $joke $w)]' instead of those arrays? If the file ends with an empty line, this will also be included, but could be removed with an lreplace or even more simple, lower i by 1, so it cannot be chosen (should be considered for a bigger file to lower memory usage).
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Post Reply