Can you explain it a bit differently?Football wrote:Hey,
I need a script that will store and display lines in a file by order and by a timer through private/public commands.
Would be nice if you could also configure the timer and view the whole file on request.
PLEASE PLEASE help.
Code: Select all
# August 3, 2010
# http://forum.egghelp.org/viewtopic.php?p=93728#93728
########################################################
#The public commands would be:
#!enable
#!disable
#!settimer
#!addmsg
#!delmsg
#!listmsg
#!timeron
#!timeroff
#!status
#!setline
#!enable - will enable the script in the current channel
#!disable - will disable the script in the current channel
#!status - will notify you whether the script is enabled or disabled
#!addmsg <message-here> and the bot will add it to his list
#!listmsg <number> - will list one message, from line #<number>
#!listmsg - will list all the messages that were added to the bot
#!delmsg <message-number> - will erase that specific message
#!settimer <number of minutes> - Every X minutes the bot will display the messages by order; #1 and then X minutes later #2 and so on
#!settimer will display the current setting.
#!timeroff Stops the timer for message display (timer is ON, when bot starts )
#!timeron Starts the timer for message display (without having to restart the bot)
#!setline <number> Manually set the line number of the next displayed line
#!setline will display current setting
####################################
##### Config
# Set the path and file name of the file that holds messages
set message_file "scripts/message_file.txt"
# Set the default starting timer minutes.
# This is the time that will be used after a restart, until a !settimer command is used to change it.
set timer_minutes 2
##### End Config
########### script begins here
bind pub - "!addmsg" addmsg
bind pub - "!listmsg" listmsg
bind pub - "!delmsg" delmsg
bind pub - "!settimer" settimer
bind pub - "!enable" enable
bind pub - "!disable" disable
bind pub - "!status" status
bind pub - "!timeroff" timer_off
bind pub - "!timeron" timer_on
bind pub - "!setline" setline
setudef flag announcer
set line_counter 0
## If message file does not exist, create it as empty file
if {![file exist $message_file]} {
close [open $message_file w]
}
## Check to see if script is loaded already, and if not, then start timer.
## The idea is to NOT start additional timers, after a rehash
if {![info exists timer_announcer_loaded]} {
set timer_announcer_loaded 1
timer $timer_minutes run_announce
}
## Sends one message to channel, every XX minutes, incrementing through stored messages.
proc run_announce {} {
global message_file timer_minutes line_counter
foreach chan [channels] {
if {([lsearch -exact [channel info $chan] {+announcer}] != -1)} {
set phile [open $message_file "r"]
set data [read -nonewline $phile]
close $phile
#Check to see if the line number we are looking for, is greater than the actual number of lines in the message file
#and if so, instead have it go to the last line
if {[llength [split $data \n]]<=$line_counter} {
set line_counter [expr [llength [split $data \n]] - 1]
}
#Display the line
if {"[lindex [split $data \n] $line_counter]"!=""} {
putserv "privmsg $chan : [join [lindex [split $data \n] $line_counter]] "
}
}
}
incr line_counter
if {[llength [split $data \n]]<=$line_counter} {
set line_counter 0
}
# re-starts the timer, to run this proc again
timer $timer_minutes run_announce
}
## Allows line counter to be manually set
proc setline {nick uhost handle chan text} {
global line_counter
if {[string is integer -strict [join [lindex [split $text] 0]]]} {
set line_counter [expr [lindex [split $text] 0] -1]
putserv "notice $nick :Next line displayed will be line [expr $line_counter +1]"
} else {
putserv "notice $nick :Next line displayed will be line [expr $line_counter +1]"
}
}
##Allows timer delay to be changed via a pub command
proc settimer {nick uhost handle chan text} {
global timer_minutes
if {([lsearch -exact [channel info $chan] {+announcer}] != -1)} {
if {"[lindex [split $text] 0]"==""} {
putserv "notice $nick :Timer is set to $timer_minutes minutes"
} elseif {"[lindex [split $text] 0]"<="0"} {
putserv "notice $nick :No change. Timer must be set to a positive integer"
} else {
set timer_minutes "[lindex [split $text] 0]"
putserv "notice $nick :Timer will be set to $timer_minutes minutes"
}
}
}
##Allows timer to be turned off via a pub command
proc timer_off {nick uhost handle chan text} {
if {([lsearch -exact [channel info $chan] {+announcer}] != -1)} {
foreach thetimer [timers] {
if {[string match -nocase "run_announce*" [lindex $thetimer 1]]} {
# got a match
killtimer [lindex $thetimer 2]
putserv "notice $nick :Timed Announcer is off"
# killed the timerID which is the 3rd element of $thetimer.
break
# stopped since no need for further checks.
}
}
# credit: http://forum.egghelp.org/viewtopic.php?p=53896#53896
}
}
## Allows timer to be turned on via a pub command
proc timer_on {nick uhost handle chan text} {
if {([lsearch -exact [channel info $chan] {+announcer}] != -1)} {
if {"[timerexists run_announce]"==""} {
run_announce
putserv "notice $nick :Timed Announcer is on"
}
}
}
## Used by other procedure, to find out if a timer already exists
proc timerexists {command} {
foreach i [timers] {
if {![string compare $command [lindex $i 1]]} then {
return [lindex $i 2]
}
}
return
#credit: from alltools.tcl
}
## Allows messages to be added via a pub command
proc addmsg {nick uhost handle chan text} {
global message_file
if {([lsearch -exact [channel info $chan] {+announcer}] != -1)} {
if {"$text"==""} {
putserv "notice $nick :Syntax: !addmsg <message here..... >"
return 0
}
set phile [open $message_file "r"]
set first_line [gets $phile]
close $phile
if {"$first_line"==""} {
set phile [open $message_file "w"]
puts $phile "[lrange [split $text] 0 end]"
close $phile
} else {
set phile [open $message_file "a"]
puts $phile "[lrange [split $text] 0 end]"
close $phile
}
putserv "notice $nick :Added: \"[join [lrange [split $text] 0 end]]\" "
}
}
## Will display all messages, in response to a pub command
proc listmsg {nick uhost handle chan text} {
global message_file line_counter
if {([lsearch -exact [channel info $chan] {+announcer}] != -1)} {
#Display one message via notice, only if a number is given with !listmsg - if no number given, then list all messages
if {[string is integer -strict [join [lindex [split $text] 0]]]} {
set phile [open $message_file "r"]
set data [read -nonewline $phile]
close $phile
if {[llength [split $data \n]]>=[lindex [split $text] 0]} {
putserv "notice $nick :[join [lindex [split $data \n] [expr [lindex [split $text] 0]-1]]]"
} else {
putserv "notice $nick :No such line"
}
return 0
}
set phile [open $message_file "r"]
set first_line [gets $phile]
close $phile
if {"$first_line"==""} {
putserv "notice $nick : I got nothing"
return 0
}
set lnum 0
set phile [open $message_file "r"]
while {[gets $phile line] >= 0} {
putserv "notice $nick :[incr lnum]: [join $line]"
}
close $phile
#credit: Luminous
if {[string is integer -strict [join [lindex [split $text] 0]]]} {
set phile [open $message_file "r"]
set data [read -nonewline $phile]
close $phile
putserv "privmsg $chan : [join [lindex [split $data \n] $line_counter]] "
}
}
}
## Allows a message to be deleted (by message number, see !listmsg) via a pub command
proc delmsg {nick uhost handle chan text} {
global message_file
if {([lsearch -exact [channel info $chan] {+announcer}] != -1)} {
if {"$text"==""} {
putserv "notice $nick :Syntax: !delmsg <number>"
putserv "notice $nick :Use !listmsg to get line numbers"
return 0
}
set del_line_num [expr [lindex [split $text] 0] - 1 ]
if {$del_line_num < 0} {
putserv "notice $nick :No can do. Must be 1 or greater."
return 0
}
set phile [open $message_file r]
set data [read -nonewline $phile]
close $phile
set lines [split $data "\n"]
if {[llength $lines] <= $del_line_num} {
putserv "notice $nick :No such line number"
return 0
}
putserv "notice $nick :Deleting: [join [lindex $lines $del_line_num]]"
putserv "notice $nick : (use !listmsg again now, as you may have changed the line numbers!)"
set lines [lreplace $lines $del_line_num $del_line_num]
set phile [open $message_file "w"]
puts $phile [join $lines "\n"]
close $phile
}
}
## Will enable timed messages announcements in the channel where the pub command is sent
proc enable {nick uhost handle chan text} {
channel set $chan +announcer
if {([lsearch -exact [channel info $chan] {+announcer}] != -1)} {
putserv "notice $nick :Timed Announcer enabled on $chan"
}
}
## Will disable timed message announcements in the channel where the pub command is sent
proc disable {nick uhost handle chan text} {
if {([lsearch -exact [channel info $chan] {+announcer}] != -1)} {
channel set $chan -announcer
}
if {([lsearch -exact [channel info $chan] {-announcer}] != -1)} {
putserv "notice $nick :Timed Announcer disabled on $chan"
}
}
## Send the status (enabled or disabled) to the channel where the pub command is given
proc status {nick uhost handle chan text} {
set time_left 0
foreach thetimer [timers] {
if {[string match -nocase "run_announce*" [lindex $thetimer 1]]} {
set time_left [lindex $thetimer 0]
}
}
if {([lsearch -exact [channel info $chan] {+announcer}] != -1)} {
putserv "notice $nick :Announcer is enabled in $chan."
if {$time_left} {putserv "notice $nick :Timer is running with $time_left minutes or less to go"}
if !{$time_left} {putserv "notice $nick :Timer is not running"}
} else {
putserv "notice $nick :Timed Announcer is disabled in $chan"
if {$time_left} {putserv "notice $nick :Timer is running with $time_left or less to go"}
if !{$time_left} {putserv "notice $nick :Timer is not running"}
}
}
putlog "Loaded Timed Announcer :) See: http://forum.egghelp.org/viewtopic.php?p=93728#93728 "
## Much credit goes to posters on the forum at http://forum.egghelp.org/
## Bits and pieces, and ideas used above - were borrowed from there.
## To anyone not specifically mentioned, I apologize.
## Any errors, broken code, poor code, or other strange things, are not theirs. They are mine.
You are welcome.Football wrote:Hey willyw! Thank you for the kind help!
It doesn't?I have a few suggestions though.
A. The script doesn't support multiple channels...
I saw:B. Would be nice if I could use the commands both on public or in private
and went with that.Football wrote: ...I need a script that can be controlled via public commands...
I'd said that you could easily edit the binds. Perhaps that didn't register on you.an example for private commands:
!enable #Football
!addmsg #Football This is an auto msg
!listmsg #Football
And so on..
C. Would be nice if you add a bind that will allow only +o to use the commands.
Code: Select all
bind pub - "!addmsg" addmsg
Code: Select all
bind pub o "!addmsg" addmsg
nah...Hope this isn't too rude... thank you both!
What I mean is, that since it uses only one file, it will use the same messages set for channel A for channel B aswell and not seperate messages.It doesn't?
Have you tried it on more than one?
I tested on only two....
Use the commands you requested : !enable and !disable
Yeah, you're totally right and thats my fault, this is only a suggestion to improve it, thought it could get a bit floody & the whole room will have to put up with the configuration, when it doesn't really need to see whats going on.. would be great if you could add it though!I saw:
Football wrote:
...I need a script that can be controlled via public commands...
and went with that.
I will have to look at it again, and see how much might be involved, to get it to work with either. ... no promises though.
One of the reasons I`m so thankful for your help is because A. I know it takes a fair amount of time & B. Because I can't script at all, thats why I`m trying to reach for the most help I can from you..I'd said that you could easily edit the binds. Perhaps that didn't register on you.
Yeah I did, everything works great, I`ve checked it out and its beyond my requests, only that addition of op binds & private binds aswell could make it just too sweet!but I am unclear on something: Have you loaded it up, and tried it?
All the commands?
I ask, because there are couple commands in there that you did not request, and couple that you did, but with modifications.
Just wanted to be sure you'd found that stuff, too.
Oh.Football wrote:What I mean is, that since it uses only one file, it will use the same messages set for channel A for channel B aswell and not seperate messages.It doesn't?
Have you tried it on more than one?
I tested on only two....
Use the commands you requested : !enable and !disable
hehe... well... put your toe in the water!....
Because I can't script at all,
Code: Select all
bind pub - "!addmsg" addmsg
bind pub - "!listmsg" listmsg
bind pub - "!delmsg" delmsg
bind pub - "!settimer" settimer
bind pub - "!enable" enable
bind pub - "!disable" disable
bind pub - "!status" status
bind pub - "!timeroff" timer_off
bind pub - "!timeron" timer_on
bind pub - "!setline" setline
Code: Select all
bind pub o "!addmsg" addmsg
bind pub o "!listmsg" listmsg
bind pub o "!delmsg" delmsg
bind pub o "!settimer" settimer
bind pub o "!enable" enable
bind pub o "!disable" disable
bind pub o "!status" status
bind pub o "!timeroff" timer_off
bind pub o "!timeron" timer_on
bind pub o "!setline" setline
Great!Yeah I did, everything works great, ...but I am unclear on something: Have you loaded it up, and tried it?
All the commands?
I ask, because there are couple commands in there that you did not request, and couple that you did, but with modifications.
Just wanted to be sure you'd found that stuff, too.