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.

Reading from a file

Old posts that have not been replied to for several years.
Locked
J
Jag
Halfop
Posts: 90
Joined: Fri Sep 19, 2003 10:06 am

Reading from a file

Post by Jag »

i need a script that read every 3 mins line from a file and send it to me in a privmsg.
something like this:
timer 3 "privmsg jag :line 1"
and after 3 mins it will do:
timer 3 "privmsg jag :line 2"
and after 3 mins it will do:
timer 3 "privmsg jag :line 3"
and after 3 mins it will do:
timer 3 "privmsg jag :line 4"
somebody can help me with that please?
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Where are you stuck and what happens when you reach the end of the file?

EDIT: I was bored...
Usage is pretty much explained by the variable names except for 'pos' which is the byte position when we last read from the file (not used when you start a new "slowmsg")

This proc doesn't care about nick changes, quitting etc., so if you need that, you've got some coding to do :) It doesn't care if you call it multiple times with the same nick/file as arguments either ...which might become annoying if you .rehash alot and have a call to this proc that is executed every time :P

Code: Select all

proc slowmsg {nick file {pos 0}} {
	set f [open $file]
	seek $f $pos
	if {[gets $f line]>-1} {
		putserv "PRIVMSG $nick :$line"
		timer 3 [list slowmsg $nick $file [tell $f]]
	} {
		putserv "PRIVMSG $nick :What am I supposed to do now? (EOF)"
	}
	close $f
}
Have you ever read "The Manual"?
J
Jag
Halfop
Posts: 90
Joined: Fri Sep 19, 2003 10:06 am

Post by Jag »

Thanks a lot, but.... how can i do that when it get to the end of the file, it will go back to the start? :)

EDIT: can you build it in "bind time" please?
10x
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Jag wrote:Thanks a lot, but.... how can i do that when it get to the end of the file, it will go back to the start? :)
Start a new timer with 0 as the last parameter or call the proc from within itself (make sure your file is not empty if you do this :P) instead of doing that putlog.
Jag wrote:EDIT: can you build it in "bind time" please?
10x
I _can_ but you don't provide enough info and I'm not always bored. Do you need it to work with multiple users/files? How are you planning to use this? Why did you change your mind about using 'timer'? Here's something to get you started:

Code: Select all

bind time - * time_demo
proc time_demo {args} {
	if {![expr {([clock seconds]/60)%3}]} {
		# do stuff
	}
}
Have you ever read "The Manual"?
J
Jag
Halfop
Posts: 90
Joined: Fri Sep 19, 2003 10:06 am

Post by Jag »

Thanks, i tried this:

Code: Select all

proc slowmsg {file {pos 0}} { 
  set f [open $file] 
  seek $f $pos 
  if {[gets $f line]>-1} { 
    putserv "privmsg #Jag $line"
    timer 3 [list slowmsg $file [tell $f]] 
    } { 
  } 
  close $f 
}

slowmsg "news.txt"
And it's works fine.
But, how can i do that when it get to the end of the file, it will go back to the start (go again from the first line to the end of the file) ?
And when i do a lot of '.rehash' it starts a lot of timers because the:
slowmsg "news.txt"
How can i stop that?

Thanks again for the help user :P
User avatar
arcane
Master
Posts: 280
Joined: Thu Jan 30, 2003 9:18 am
Location: Germany
Contact:

Post by arcane »

Code: Select all

set mytimer [timer 3 bla...]
and

Code: Select all

catch { killtimer $mytimer }
in front of your proc
aVote page back online!
Check out the most popular voting script for eggdrop bots.

Join the metal tavern!
J
Jag
Halfop
Posts: 90
Joined: Fri Sep 19, 2003 10:06 am

Post by Jag »

Thanks, it helps.
But, what about the:
Jag wrote: But, how can i do that when it get to the end of the file, it will go back to the start (go again from the first line to the end of the file) ?
?
10x :P
B
Black Byte

Post by Black Byte »

I'm not quite sure - as I'm quite new - but I would try it as:

Code: Select all

proc slowmsg {file {pos 0}} {
  set f [open $file]
  seek $f $pos
  if {[gets $f line]>-1} {
    putserv "privmsg #Jag $line"
     timer 3 [list slowmsg $file [tell $f]]
    } else {
     slowmsg $file
  }
  close $f
} 
Probally closing the file should be called before restarting the proc.

u could also start the timer directly - skipping one read. :) like:

Code: Select all

} else {
     timer 3 [list slowmsg $file]
}
(not sure about the list)
J
Jag
Halfop
Posts: 90
Joined: Fri Sep 19, 2003 10:06 am

Post by Jag »

It works!
Thanks man :)
Locked