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.

Change Topic based on a text file every 60 seconds?

Help for those learning Tcl or writing their own scripts.
Post Reply
D
DJCharlie
Voice
Posts: 37
Joined: Wed May 06, 2009 10:45 am
Contact:

Change Topic based on a text file every 60 seconds?

Post by DJCharlie »

Okay, let me preface this by saying that I'm totally new to eggdrops in general, and scripting tcl in particular.

Now then.. I'm trying to write a script that every 60 seconds reads 2 text files, and if the contents of those files are equal, do nothing. BUT, if they're different, incorporate the line from the first file into the channel topic.

Here's what I've got so far:

Code: Select all

proc checktopic {} {

  set cdjf [open "/usr/local/autodj/tmp/lastdj" r]
  while {[eof $cdjf] !=1} {
    set cdj [gets $cdjf]
  }
  close $cdjf

  set tdjf [open "/usr/local/autodj/tmp/topicdj" r]
  while {[eof $tdjf] !=1} {
    set tdj [gets $tdjf]
  }
  close $tdjf

  if {$cdj == $tdj} {
    putlog "Same DJ, no change - $cdj"
    break
  } else {
    putserv "TOPIC #KJSR :KJSR.net - The Voice of Jello Shooters - $cdj"
    set tdjf [open "/usr/local/autodj/tmp/topicdj" w]
    puts $tdjf $cdj
    close $tdjf
    utimer 60 [list checktopic]
  }
}

utimer 60 [list checktopic]
And here's what it's outputting:

Code: Select all

<JohnnyFever> [22:49] Same DJ, no change -
<JohnnyFever> [22:49] Tcl error in script for 'timer1494':
<JohnnyFever> [22:49] invoked "break" outside of a loop
It never runs again until I rehash.

And what's worse, is the contents of the two files are different, so it SHOULD be changing the topic.

Can anyone tell me where I screwed up? Thanks in advance.
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

Code: Select all

# interval in minutes at which to refresh topic
variable dj_refresh 1

proc checktopic { } {

  set cdjf [open "/usr/local/autodj/tmp/lastdj" r]
  set cdj [read -nonewline $cdjf]
  close $cdjf

  set tdjf [open "/usr/local/autodj/tmp/topicdj" r]
  set tdj [read -nonewline $tdjf]
  close $tdjf

  if {[string match $tdj $cdj]} {
    putlog "Same DJ, no change - $cdj"
  } else {
    putserv "TOPIC #KJSR :KJSR.net - The Voice of Jello Shooters - $cdj"
    set tdjf [open "/usr/local/autodj/tmp/topicdj" w]
    puts $tdjf $cdj
    close $tdjf
    timer $::dj_refresh [list checktopic]
  }
}

timer $dj_refresh [list checktopic] 
it's better in this case to use timer rather than utimer to track 60 seconds/1 minute. Utimer will check for matches each second that passes, so even if it isn't triggered it still evaluates this every second. Timer runs against minutes, and theoretically saves your bot 59 evaluations every minute.
D
DJCharlie
Voice
Posts: 37
Joined: Wed May 06, 2009 10:45 am
Contact:

Post by DJCharlie »

it's better in this case to use timer rather than utimer to track 60 seconds/1 minute. Utimer will check for matches each second that passes, so even if it isn't triggered it still evaluates this every second. Timer runs against minutes, and theoretically saves your bot 59 evaluations every minute.
Well, it's almost working. Except it's triggering every minute and changing the topic twice.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

I'd suggest you do not use "string match" here, but rather "string compare" or "string equal". "match" should only be used when pattern matching is needed, not literary matching.

The load impact of using utimer instead of timer would be minimal. For best performance and reliability, I'd rather recommend the time binding.
Also, you both forgot to restart the timer in the case of both files matching.

Code: Select all

bind time - * checktopic
proc checktopic {args} {
  set cdjf [open "/usr/local/autodj/tmp/lastdj" r]
  set cdj [read -nonewline $cdjf]
  close $cdjf

  set tdjf [open "/usr/local/autodj/tmp/topicdj" r]
  set tdj [read -nonewline $tdjf]
  close $tdjf

  if {[string equal $tdj $cdj]} {
    putlog "Same DJ, no change - $cdj"
  } else {
    putserv "TOPIC #KJSR :KJSR.net - The Voice of Jello Shooters - $cdj"
    file copy --force "/usr/local/autodj/tmp/lastdj" "/usr/local/autodj/tmp/topicdj"
  }
}
Also took the liberty of simply copying the lastdj file to topicdj file, rather than the "cumbersome" file operations of rewriting it's content.

Finally, since you're using timers, make sure you don't have several timers running at the same time, as this will cause your behaviour of "changing topic twice"
NML_375
D
DJCharlie
Voice
Posts: 37
Joined: Wed May 06, 2009 10:45 am
Contact:

Post by DJCharlie »

nml375 wrote:I'd suggest you do not use "string match" here, but rather "string compare" or "string equal". "match" should only be used when pattern matching is needed, not literary matching.
It works! Thanks!
D
DJCharlie
Voice
Posts: 37
Joined: Wed May 06, 2009 10:45 am
Contact:

Post by DJCharlie »

Well, the script is still working great, except for one minor little problem that just popped up today...

On DALnet (our "home" server), the script runs fine (on bot J1). But on EFnet (on bot J2), it doesn't run period. No error message, no nothing. The bots are linked correctly, and relaying fine. Other scripts run fine as well.

Any ideas?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Well, the code is not server-dependant, which suggests an issue with J2 or it's system/account.

Are both bots running on the same system/account? Are they using the very same topicdj/lastdj files?
NML_375
D
DJCharlie
Voice
Posts: 37
Joined: Wed May 06, 2009 10:45 am
Contact:

Post by DJCharlie »

nml375 wrote:Well, the code is not server-dependant, which suggests an issue with J2 or it's system/account.

Are both bots running on the same system/account? Are they using the very same topicdj/lastdj files?
Yes, and yes. :) Exact same setup, just a different server and botnet-name.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Then what is happening is that the first eggdrop sees the new dj, changes topic, and copies the file... hence the second eggie won't see the new dj.

You'd either have to use different files for each eggie (topicdj, the lastdj may still remain shared), or you'll have to keep the $tdj in each eggies memory, rather than re-reading it from the file once every minute.
NML_375
D
DJCharlie
Voice
Posts: 37
Joined: Wed May 06, 2009 10:45 am
Contact:

Post by DJCharlie »

That did it! Thanks!
Post Reply