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.

write and delete file on trigger

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
t
televi
Voice
Posts: 4
Joined: Fri Aug 13, 2010 2:09 pm

write and delete file on trigger

Post by televi »

Hi, I am looking for a script that would write in a file on a given trigger..and deletes it after an hour

something like:
user> !trigger hello
bot> nick, message written.
....bot writes nick:message in a file saved on the shell. Once the hour is donee the bot deletes that line in the file.


It sounds simple and I hope its simple :)

Any help is much appreciated!
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

Post by Luminous »

Code: Select all

bind pub - !trigger trigger
proc trigger {nick host hand chan text} {
 set mfile "messages.txt"
    set msg [join [lindex [split $text] 0 end]]
    if {![file exists $mfile]} {
        close [open $mfile w]
    }
    set fs [open $mfile]
    set msgs [read -nonewline $fs]
  close $fs
    if {![llength $msgs]} {
        set fs [open $mfile w]
    } else {
        set fs [open $mfile a]
    }
    puts $fs "$nick:$msg"
 close $fs
    putserv "PRIVMSG $chan :$nick, added message: $nick:$msg."
    utimer 60 [list delete_file $mfile $nick $msg]
}

proc delete_file {mfile nick msg} {
    set fs [open $mfile]
    set msgs [split [read -nonewline $fs] \n]
   close $fs
    set line [lsearch -exact $msgs "$nick:$msg"]
    set lines [lreplace $msgs $line $line]
    set fs [open $mfile w]
    puts $fs [join $lines "\n"]
 close $fs
   putserv "PRIVMSG $chan :Message $nick:$msg has been deleted."
}

Messages will be written to ~/eggdrop/messages.txt. This seems incomplete to me though... this will write the data, but nothing is really being done with it, other than deleting...
Last edited by Luminous on Fri Aug 13, 2010 3:09 pm, edited 1 time in total.
t
televi
Voice
Posts: 4
Joined: Fri Aug 13, 2010 2:09 pm

thanx

Post by televi »

thanx for the quick reply!

However, the bot does create the file messages.txt but does not write anything it in. I also do not see any errors/problems in partyline
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

Post by Luminous »

Try again mate, I fixed a few things. You probably loaded it before I edited. :S
t
televi
Voice
Posts: 4
Joined: Fri Aug 13, 2010 2:09 pm

thaaannkkk you!

Post by televi »

Oh that I did! :)

Thanx a lot man!!! you the best!!
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

Post by Luminous »

Your welcome. :) And okay... I don't have a lot of time to work on it atm, but I'll see what I can do about that.
t
televi
Voice
Posts: 4
Joined: Fri Aug 13, 2010 2:09 pm

no worries

Post by televi »

dont worry this is all i needed! :) everything works fine, thanx a lot
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

Post by Luminous »

Your very welcome. :) I didn't mean to make it sound like a hassle, I always enjoy scripting. I went ahead and used a familiar method, using the comment field to store a keyword to check for when allowing or disallowing messages. I am sure there is a better way to do this though. Maybe another scripter can provide a different/better way. :)

Code: Select all

bind pub - !trigger trigger
proc trigger {nick host hand chan text} {
 set mfile "messages.txt"
 set comment [getuser $hand comment]
  if {$comment eq "msg"} {
    putserv "NOTICE $nick :Sorry, you are only allowed to create one message per hour."
 return
  }
   setuser $hand comment "msg"
    set msg [join [lindex [split $text] 0 end]]
    if {![file exists $mfile]} {
        close [open $mfile w]
    }
    set fs [open $mfile]
    set msgs [read -nonewline $fs]
  close $fs
    if {![llength $msgs]} {
        set fs [open $mfile w]
    } else {
        set fs [open $mfile a]
    }
    puts $fs "$nick:$msg"
 close $fs
    putserv "PRIVMSG $chan :$nick, added message: $nick:$msg."
    utimer 60 [list delete_msg $mfile $nick $hand $msg $chan]
}

proc delete_msg {mfile nick hand msg chan} {
    set fs [open $mfile]
    set msgs [split [read -nonewline $fs] \n]
   close $fs
    set line [lsearch -exact $msgs "$nick:$msg"]
    set lines [lreplace $msgs $line $line]
    set fs [open $mfile w]
    puts $fs [join $lines "\n"]
 close $fs
   setuser $hand comment ""
   putserv "PRIVMSG $chan :Message \"$nick:$msg\" has been deleted."
}
I also fixed the error that probably occured with this line before: putserv "PRIVMSG $chan :Message \"$nick:$msg\" has been deleted."

Forgot to pass the chan arg, whoops.
Post Reply