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.

Timespent Script

Support & discussion of released scripts, and announcements of new releases.
Post Reply
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Timespent Script

Post by Madalin »

For any ideas for improving the code
For any bugs or questions please post here

Code: Select all

#### ++++ Author: MadaliN <madalinmen28@yahoo.com> 
### +++ Website: www.Ascenture.ro
## +++ TCL Name: Timespent 
# +++ Version: 1.0
#
# Commands: 
#   !timespent activate         (local or global owner) 
#   !timespent deactivate         (local or global owner) 
#   !timespent <$nickname> <$chan>      (local or global owner) 
# 

#### 
# +++ Created: 1/26/2013 
#### 

bind TIME - * egghelp:timespent 

bind PUB - !timespent timespent:pub 

set announce(time) "1" ;#setting is in hours 

setudef flag timespent 

proc egghelp:timespent {min hour day month year} { 
   global timespent announce noa 

   set list "" 
   foreach chan [channels] { 
      if {[channel get $chan timespent]} { 
         foreach u [chanlist $chan] { 

            set host [lindex [split [getchanhost $u $chan] @] 1] 

            if {![info exists timespent($chan,$host)]} { 
               set timespent($chan,$host) "60 $u" 
               timespent:save 
            } else { 
               set timespent($chan,$host) "[expr [lindex [split $timespent($chan,$host)] 0] + 60] $u" 
               timespent:save 

               if {$announce(time) != ""} { 
                  if {[lindex [split $timespent($chan,$host)] 0] > [expr $announce(time) * 3600]} { 
                     if {![info exists noa($chan,$host)]} { 
                        set noa($chan,$host) "[unixtime]" 
                        timespent:save 

                        lappend list "\00303[lindex [split $timespent($chan,$host)] 1]\003" 
                     } 
                  } 
               } 
            } 
         } 
         if {$list != ""} { 
            putserv "PRIVMSG $chan :Users who spent more than \00312$announce(time)\003 hour on \00304$chan\003 are: [join $list "\002,\002 "]"
         } 
      } 
   } 
} 

proc timespent:pub {nick uhost hand chan arg} { 
   global timespent 

   set host [lindex [split [getchanhost [lindex [split $arg] 0] $chan] @] 1] 

   switch -exact -- [lindex [split $arg] 0] { 
      activate { 
         if {[matchattr $hand n] || [matchattr $hand |N $chan]} { 
            channel set $chan +timespent 

            putserv "PRIVMSG $chan :\002$nick\002 - TIMESPENT script \00312activated\003 succesfully" 
         } 
      } 
      deactivate { 
         if {[matchattr $hand n] || [matchattr $hand |N $chan]} { 
            channel set $chan -timespent 

            putserv "PRIVMSG $chan :\002$nick\002 - TIMESPENT script \00304deactivated\003 succesfully" 
         } 
      } 
      default { 
         if {[matchattr $hand n] || [matchattr $hand |N $chan]} { 

            if {[string match -nocase "#*" [lindex [split $arg] 1]]} { 
               if {![info exists timespent([lindex [split $arg] 1],$host)]} { 
                  putserv "PRIVMSG $chan :\002$nick\002 - Nickname \00303[lindex [split $arg] 0]\003 has no info on \00312[lindex [split $arg] 1]"
               } else { 
                  putserv "PRIVMSG $chan :\002$nick\002 - Nickname \00303[lindex [split $arg] 0]\003 idled on \00312[lindex [split $arg] 1]\003 for \00303[duration [lindex [split $timespent([lindex [split $arg] 1],$host)] 0]]" 
               } 
            } else { 
               if {![info exists timespent($chan,$host)]} { 
                  putserv "PRIVMSG $chan :\002$nick\002 - Nickname \00303[lindex [split $arg] 0]\003 has no info on this channel" 
               } else { 
                  putserv "PRIVMSG $chan :\002$nick\002 - Nickname \00303[lindex [split $arg] 0]\003 idled on \00312$chan\003 for \00303[duration [lindex [split $timespent($chan,$host)] 0]]" 
               } 
            } 
         } 
      } 
   } 
} 

proc timespent:save {} { 
   global timespent 

   set ofile [open timespent w] 
   puts $ofile "array set timespent [list [array get timespent]]" 
   close $ofile 
} 

catch {source timespent} 

putlog "+++ Succesfully loaded: \00312Timpespent TCL Script" 
Last edited by Madalin on Tue Aug 06, 2013 7:19 am, edited 1 time in total.
x
x0x
Op
Posts: 140
Joined: Tue Feb 10, 2009 6:42 am

Post by x0x »

Does it keep track of people even after they part/quit/split?
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

It only keeps track if the user is seen in the channel
x
x0x
Op
Posts: 140
Joined: Tue Feb 10, 2009 6:42 am

Post by x0x »

Would it be possible to write to a file and keep a record of a nicknames and their total time? So count when they are on and continue counting when they return after a quit or netsplit?
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

This is how the script works now.
The eggdrop is counting (minute by minute) if the user is on the channel and writes the users with their idle time in a file (so it wont be lost)
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Shouldn't you should exclude the bot from adding itself to the list? :)

The easiest way to do this would be with replacing:

Code: Select all

foreach u [chanlist $chan] { 
with this:

Code: Select all

set userlist [lreplace [chanlist $chan] 0 0]
foreach u $userlist {
The time bind you got it's triggered every minute so it's a waste of resources when you could easily integrate the 1 hour check directly in the bind:

Code: Select all

bind time {?0 *} gghelp:timespent
You have the timespent:save function called in like 3 places in the egghelp:timespent proc, why not have it just once?

In the timespent:pub proc why do you use matchattr to check user flags when you can bind the pub command directly in the bind line?

Code: Select all

bind PUB n|N !timespent timespent:pub
or whatever..

I see you using [split $arg] for like 7 times in timespent:pub. Why don't you split it just once and use it from there on with just $arg?

If you want to keep track of since the user joined the channel, why don't you just use getchanjoin instead? The time bind would be triggered every 1 hour (or whatever) and get the info from there and announce the list. Seems more easier than keeping a list in memory, a list that has to be cleaned once the bot itself would quit, part, split, die, whatever.

Here's what I meant by that:

Code: Select all

bind time {?0 *} announce:timespent

proc announce:timespent {min hour day month year} {
	foreach chan [channels] {
      if {![channel get $chan timespent]} continue
		set userlist [lreplace [chanlist $chan] 0 0]
		foreach user $userlist {
			set timestamp [getchanjoin $user $chan]
			if {[expr [unixtime] - $timestamp] < 3600} continue
			lappend list "$user $timestamp"
		}		
		set list [lsort -integer -index 1 $list]
		foreach ele $list {
			lappend users [lindex [split $ele] 0]
		}
		if {![info exists $users]} continue
		puthelp "PRIVMSG $chan :Users who spent more than 1 hour on $chan are:"
		while {[llength $users] != 0} {
			puthelp "PRIVMSG $chan :[join [lrange $users 0 14] ", "]"
			set users [lrange $users 15 end]
		}
	}
}
I've tested it in my head but not on an eggdrop. :roll:
Once the game is over, the king and the pawn go back in the same box.
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

That is only the first version i know it can be improved but why improve something if its not constantly used. Maybe for sure thats the reason i didnt updated the script.

And exclude the bot from the list i would have done something like this

Code: Select all

if {![isbotnick $var]} { .. }
and for the n|N flags at the !timespent command i didnt used the flags there because i used switch in that proc and i have more than 1 command in there so i could easy add a command for another flag and the list can continue
Post Reply