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.
Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Football
Master
Posts: 205 Joined: Fri Dec 26, 2008 3:08 pm
Location: Quakenet, #Football
Post
by Football » Sat Jun 09, 2012 11:40 am
Hi all!
Was wondering if someone could help me out.
I need a script that will check every X minutes (determined in the script) if there are any lines written in a file called 'Stats.txt', if there is - it will write the first line in the file to channel(s) set in the .tcl file and erase that file.
If there are no lines in the file - it won't do anything, just keeping on checking the file and read/erase once there are lines in Stats.txt
Thanks
Idling at #Football, Quakenet.
willyw
Revered One
Posts: 1203 Joined: Thu Jan 15, 2009 12:55 am
Post
by willyw » Sat Jun 09, 2012 4:44 pm
Experiment with this:
Code: Select all
# June 9, 2012
# http://forum.egghelp.org/viewtopic.php?t=19001
#
# set how often file check runs - in minutes
set sched 1
# set path and filename of stats file
set statsfile "scripts/added/testing/Stats.txt"
# set channels in which to announce
set announcechans "#chan1 #chan2"
##
timer $sched readthefile
proc readthefile { } {
global sched statsfile announcechans
if {![file exists $statsfile]} {
timer $sched readthefile
return 0
}
# reference: http://forum.egghelp.org/viewtopic.php?t=6885
set fname $statsfile
set fp [open $fname "r"]
set data [read -nonewline $fp]
close $fp
set lines [split $data "\n"]
if {[lindex $lines 0] == ""} {
timer $sched readthefile
return 0
}
foreach channel $announcechans {
if {[botonchan $channel]} {
putserv "privmsg $channel :[lindex $lines 0]"
}
}
file delete $statsfile
timer $sched readthefile
}
bind evnt - prerehash cleartimers
proc cleartimers {prerehash} {
foreach timerlisted [timers] {
if {[lindex $timerlisted 1] == "readthefile" } {
killtimer [lindex $timerlisted 2]
}
}
}
Tested briefly, and it seemed to do what you described. I hope this helps.
Football
Master
Posts: 205 Joined: Fri Dec 26, 2008 3:08 pm
Location: Quakenet, #Football
Post
by Football » Sun Jun 10, 2012 2:38 am
Hey willyw,
Thanks for the code
However it seems that the script reads the first line from Stats.txt and then erases the whole txt, which isn't good.
* Also wondering if you can please add a public command for adding lines to Stats.txt please
Idling at #Football, Quakenet.
willyw
Revered One
Posts: 1203 Joined: Thu Jan 15, 2009 12:55 am
Post
by willyw » Sun Jun 10, 2012 3:23 am
Football wrote:
However it seems that the script reads the first line from Stats.txt and then erases the whole txt, which isn't good.
[...]
Football wrote:
... it will write the first line in the file to channel(s) set in the .tcl file and erase that file .
?
That's how I read your request.
Football
Master
Posts: 205 Joined: Fri Dec 26, 2008 3:08 pm
Location: Quakenet, #Football
Post
by Football » Sun Jun 10, 2012 3:30 am
Yeah you're right, I apologize, its my fault.. I was thinking about something and wrote something else... The script is meant to read each X minutes the next line in the file. Each time it reads a line, it erases that line from the file..
Idling at #Football, Quakenet.
willyw
Revered One
Posts: 1203 Joined: Thu Jan 15, 2009 12:55 am
Post
by willyw » Sun Jun 10, 2012 10:46 am
Football wrote: Yeah you're right, I apologize, its my fault.. I was thinking about something and wrote something else...
The script is meant to read each X minutes the next line in the file. Each time it reads a line, it erases that line from the file..
Try this:
Code: Select all
# June 10, 2012
# http://forum.egghelp.org/viewtopic.php?t=19001
#######
# Usage: To add a line, do: !addline <text of line here> in channel.
# To see all lines, do: !sayfile
# set how often file check runs - in minutes
set sched 1
# set path and filename of stats file
set statsfile "scripts/added/testing/Stats.txt"
# set channels in which to announce
set announcechans "#channel1 #channel2"
bind pub - "!addline" addline
bind evnt - prerehash cleartimers
bind pub - "!sayfile" sayfile
timer $sched readthefile
proc readthefile { } {
global sched statsfile announcechans
if {![file exists $statsfile]} {
timer $sched readthefile
return 0
}
# reference: http://forum.egghelp.org/viewtopic.php?t=6885
set fname $statsfile
set fp [open $fname "r"]
set data [read -nonewline $fp]
close $fp
set lines [split $data "\n"]
if {[lindex $lines 0] == ""} {
timer $sched readthefile
return 0
}
foreach channel $announcechans {
if {[botonchan $channel]} {
putserv "privmsg $channel :[lindex $lines 0]"
}
}
set fname $statsfile
set fp [open $fname "r"]
set data [read -nonewline $fp]
close $fp
set lines [split $data "\n"]
# reference: http://forum.egghelp.org/viewtopic.php?t=6885
set line_to_delete 0
set lines [lreplace $lines $line_to_delete $line_to_delete]
set fp [open $fname "w"]
puts $fp [join $lines "\n"]
close $fp
timer $sched readthefile
}
###
proc cleartimers {prerehash} {
foreach timerlisted [timers] {
if {[lindex $timerlisted 1] == "readthefile" } {
killtimer [lindex $timerlisted 2]
}
}
}
###
proc addline {nick uhost handle chan text} {
global statsfile
if {$text == ""} {
return 0
}
# reference: http://forum.egghelp.org/viewtopic.php?t=6885
set line_to_add $text
set fname $statsfile
set fp [open $fname "a"]
puts $fp $line_to_add
close $fp
}
###
proc sayfile {nick uhost handle chan text} {
global statsfile
if {![file exists $statsfile]} {
putserv "privmsg $chan :File does not exist"
return 0
}
set fname $statsfile
set fp [open $fname "r"]
set data [read -nonewline $fp]
close $fp
set lines [split $data "\n"]
putserv "privmsg $chan :$lines"
}
Football
Master
Posts: 205 Joined: Fri Dec 26, 2008 3:08 pm
Location: Quakenet, #Football
Post
by Football » Sun Jun 10, 2012 11:24 am
Hey willyw,
You got it right... both times..
Thanks a lot, I apologize for earlier.
Idling at #Football, Quakenet.
willyw
Revered One
Posts: 1203 Joined: Thu Jan 15, 2009 12:55 am
Post
by willyw » Sun Jun 10, 2012 11:30 am
Football wrote: Hey willyw,
You got it right... both times..
Thanks a lot,
You're welcome
I apologize for earlier.
It's ok. np