egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Change Topic based on a text file every 60 seconds?

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
DJCharlie
Voice


Joined: 06 May 2009
Posts: 37

PostPosted: Sat May 09, 2009 10:59 pm    Post subject: Change Topic based on a text file every 60 seconds? Reply with quote

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:

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:

<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.
Back to top
View user's profile Send private message Visit poster's website
speechles
Revered One


Joined: 26 Aug 2006
Posts: 1398
Location: emerald triangle, california (coastal redwoods)

PostPosted: Sun May 10, 2009 12:02 am    Post subject: Reply with quote

Code:
# 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.
_________________
speechles' eggdrop tcl archive
Back to top
View user's profile Send private message
DJCharlie
Voice


Joined: 06 May 2009
Posts: 37

PostPosted: Sun May 10, 2009 12:19 am    Post subject: Reply with quote

Quote:
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.
Back to top
View user's profile Send private message Visit poster's website
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sun May 10, 2009 9:42 am    Post subject: Reply with quote

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:
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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
DJCharlie
Voice


Joined: 06 May 2009
Posts: 37

PostPosted: Sun May 10, 2009 11:38 am    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message Visit poster's website
DJCharlie
Voice


Joined: 06 May 2009
Posts: 37

PostPosted: Thu May 28, 2009 12:28 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message Visit poster's website
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Thu May 28, 2009 9:32 am    Post subject: Reply with quote

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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
DJCharlie
Voice


Joined: 06 May 2009
Posts: 37

PostPosted: Thu May 28, 2009 9:56 am    Post subject: Reply with quote

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. Smile Exact same setup, just a different server and botnet-name.
Back to top
View user's profile Send private message Visit poster's website
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Thu May 28, 2009 10:00 am    Post subject: Reply with quote

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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
DJCharlie
Voice


Joined: 06 May 2009
Posts: 37

PostPosted: Thu May 28, 2009 10:20 am    Post subject: Reply with quote

That did it! Thanks!
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber