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.

relay.tcl

Support & discussion of released scripts, and announcements of new releases.
Post Reply
F
Football
Master
Posts: 205
Joined: Fri Dec 26, 2008 3:08 pm
Location: Quakenet, #Football

relay.tcl

Post by Football »

Hey, I`m using relay.tcl, it relays words that are written in a text file from one channel another, the problem is that if you have two words in the text file that match in the same setence, it will relay the message more then once.

Lets say our text file includes the words "Hello there"
and the sentence is "Hello there Tony"
It will relay the sentence twice (as many times as it matches the text file)

Can anyone fix it so it won't relay it more then once please?

Thank you.

Code: Select all

# Set here, the channel you want the bot to listen for messages on                        

set mchan "#PremierLeague" 

# Set here, the channel you want the messages to be relayed to.                            

set rchan "#EPL" 

# Add a word for relay 
bind pub n|o !addword addword 

# View a list of your relays 
bind pub o|o !relays relays 

# Clear all relays from wordsEPL.txt 
bind pub o|o !relayclr msgclear 

# This is what searches for and relays lines 
bind pubm * * relay_to_chan 

# Delete a relay word from file 
bind pub n|o !delword delword 

proc addword {nick host hand chan text} { 
 global mchan 
    if {[string equal -nocase $chan $mchan]} { 
 set mfs "wordsEPL.txt" 
        set word [join [lindex [split $text] 0]] 
        if {![file exists $mfs]} { 
            close [open $mfs w] 
        } 
        set fs [open $mfs] 
        set data [read -nonewline $fs] 
  close $fs 
        if {![string length $data]} { 
            set fs [open $mfs w] 
        } else { 
            set fs [open $mfs a] 
        } 
 puts $fs "$word" 
  close $fs 
   putserv "NOTICE $nick :Relay added: $word" 
    } 
 return 
} 

proc relays {nick host hand chan text} { 
    set fs [open "wordsEPL.txt"] 
    set data [read -nonewline $fs] 
  close $fs 
    if {![string length $data]} { 
  putserv "NOTICE $nick :I don't currently have any messages to relay." 
 return 0 
    } 
  putserv "NOTICE $nick :My list of relay messages is as follows:" 
    set fs [open "wordsEPL.txt"] 
  seek $fs 0 start 
    while {[gets $fs line] >= 0} { 
  puthelp "NOTICE $nick :$line" 
    } 
 close $fs 
} 

proc msgclear {nick host hand chan text} { 
    close [open "wordsEPL.txt" w] 
} 

proc relay_to_chan {nick host hand chan text} { 
  global mchan rchan 
        if {[string equal -nocase $chan $mchan]} { 
 set fs [open "wordsEPL.txt"] 
   seek $fs 0 start 
     while {[gets $fs line] >= 0} { 
         if {[regexp -nocase ".*$line.*" [join [lrange [split $text] 0 end]] match]} { 
   putserv "PRIVMSG $rchan :$match" 
         } 
   } 
 close $fs 
 return 
        } 
 return 
} 

proc delword {nick host hand chan text} { 
 global mchan 
    if {[string equal -nocase $chan $mchan]} { 
 set word [lindex [split $text] 0] 
 set fs [open "wordsEPL.txt"] 
 set words [split [read -nonewline $fs] \n] 
   close $fs 
 set match [lsearch -exact $words $word] 
 set fs [open "wordsEPL.txt" w] 
 set newwords [lreplace $words $match $match] 
   puts $fs [join $newwords "\n"] 
   close $fs 
    putserv "NOTICE $nick :Relay deleted: $word." 
    } 
   return 
} 
Idling at #Football, Quakenet.
User avatar
username
Op
Posts: 196
Joined: Thu Oct 06, 2005 9:20 am
Location: Russian Federation, Podolsk
Contact:

Post by username »

Try to change

Code: Select all

set fs [open "wordsEPL.txt"] 
seek $fs 0 start 
while {[gets $fs line] >= 0} { 
if {[regexp -nocase ".*$line.*" [join [lrange [split $text] 0 end]] match]} { 
putserv "PRIVMSG $rchan :$match" 
}
to

Code: Select all

foreach line [split [read [set fid [open "wordsEPL.txt" "r"]]] "\n"] {
    if {[string match -nocase "*line*" $text]} {
        putserv "PRIVMSG $rchan :$text" 
    }
}
close $fid
Архив TCL скриптов для ботов Eggdrop/Windrop:
http://egghelp.ru/
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

I'm afraid that change will not sort the issue;

I'd suggest you add a break to stop the loop once you've found a match:

Code: Select all

set fs [open "wordsEPL.txt"]
seek $fs 0 start
while {[gets $fs line] >= 0} {
  if {[regexp -nocase ".*$line.*" $text match]} {
    putserv "PRIVMSG $rchan :$match"
    break
  }
}
close $fs
PS. I took the liberty of removing some pointless list operations (split, lrange all, join). DS
NML_375
F
Football
Master
Posts: 205
Joined: Fri Dec 26, 2008 3:08 pm
Location: Quakenet, #Football

Post by Football »

hey nml375 and username.

Thanks so much for the reply.

nml375 - that worked well, though it seems to have caused a new problem:

[19:04:50] <@DontPanic> !relays
[19:04:07] <BarraBrava> [16:04:14] Tcl error [relays]: can't read "data": no such variable
Idling at #Football, Quakenet.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Could you post your new version of the relays proc?
NML_375
F
Football
Master
Posts: 205
Joined: Fri Dec 26, 2008 3:08 pm
Location: Quakenet, #Football

Post by Football »

Sure

Code: Select all

# Set here, the channel you want the bot to listen for messages on
set mchan "#PremierLeague" 

# Set here, the channel you want the messages to be relayed to.
set rchan "#EPL" 

# Add a word for relay 
bind pub n|o !addword addword 

# View a list of your relays 
bind pub o|o !relays relays 

# Clear all relays from words.txt 
bind pub o|o !relayclr msgclear 

# This is what searches for and relays lines 
bind pubm * * relay_to_chan 

# Delete a relay word from file 
bind pub n|o !delword delword 

proc addword {nick host hand chan text} { 
 global mchan 
    if {[string equal -nocase $chan $mchan]} { 
 set mfs "wordsEPL.txt" 
        set word [join [lindex [split $text] 0]] 
        if {![file exists $mfs]} { 
            close [open $mfs w] 
        } 
        set fs [open $mfs] 
        set data [read -nonewline $fs] 
  close $fs 
        if {![string length $data]} { 
            set fs [open $mfs w] 
        } else { 
            set fs [open $mfs a] 
        } 
 puts $fs "$word" 
  close $fs 
   putserv "NOTICE $nick :Relay added: $word" 
    } 
 return 
} 

proc relays {nick host hand chan text} { 
    set fs [open "wordsEPL.txt"]
set fs [open "wordsEPL.txt"]
seek $fs 0 start
while {[gets $fs line] >= 0} {
  if {[regexp -nocase ".*$line.*" $text match]} {
    putserv "PRIVMSG $rchan :$match"
    break
  }
}
close $fs
    if {![string length $data]} { 
  putserv "NOTICE $nick :I don't currently have any messages to relay." 
 return 0 
    } 
  putserv "NOTICE $nick :My list of relay messages is as follows:" 
   set fs [open "wordsEPL.txt"]
seek $fs 0 start
while {[gets $fs line] >= 0} {
  if {[regexp -nocase ".*$line.*" $text match]} {
    putserv "PRIVMSG $rchan :$match"
    break
  }
}
close $fs
} 

proc msgclear {nick host hand chan text} { 
    close [open "wordsEPL.txt" w] 
} 

proc relay_to_chan {nick host hand chan text} { 
  global mchan rchan 
        if {[string equal -nocase $chan $mchan]} { 
 set fs [open "wordsEPL.txt"] 
   set fs [open "wordsEPL.txt"]
seek $fs 0 start
while {[gets $fs line] >= 0} {
  if {[regexp -nocase ".*$line.*" $text match]} {
    putserv "PRIVMSG $rchan :$match"
    break
  }
}
close $fs
return 
        } 
 return 
} 

proc delword {nick host hand chan text} { 
 global mchan 
    if {[string equal -nocase $chan $mchan]} { 
 set word [lindex [split $text] 0] 
 set fs [open "wordsEPL.txt"] 
 set words [split [read -nonewline $fs] \n] 
   close $fs 
 set match [lsearch -exact $words $word] 
 set fs [open "wordsEPL.txt" w] 
 set newwords [lreplace $words $match $match] 
   puts $fs [join $newwords "\n"] 
   close $fs 
    putserv "NOTICE $nick :Relay deleted: $word." 
    } 
   return 
} 
Idling at #Football, Quakenet.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

That is quite far from the original proc. The suggested fix should be applied to the relay_to_chan proc, not the other ones.
NML_375
F
Football
Master
Posts: 205
Joined: Fri Dec 26, 2008 3:08 pm
Location: Quakenet, #Football

Post by Football »

oh, errr

might writing the proper script in full?
Idling at #Football, Quakenet.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Code: Select all

# Set here, the channel you want the bot to listen for messages on                       
set mchan "#PremierLeague"

# Set here, the channel you want the messages to be relayed to.                           
set rchan "#EPL"

# Add a word for relay
bind pub n|o !addword addword

# View a list of your relays
bind pub o|o !relays relays

# Clear all relays from wordsEPL.txt
bind pub o|o !relayclr msgclear

# This is what searches for and relays lines
bind pubm * * relay_to_chan

# Delete a relay word from file
bind pub n|o !delword delword

proc addword {nick host hand chan text} {
  global mchan
  if {[string equal -nocase $chan $mchan]} {
    set mfs "wordsEPL.txt"
    set word [join [lindex [split $text] 0]]
    if {![file exists $mfs]} {
      close [open $mfs w]
    }
    set fs [open $mfs]
    set data [read -nonewline $fs]
    close $fs
    if {![string length $data]} {
      set fs [open $mfs w]
    } else {
      set fs [open $mfs a]
    }
    puts $fs "$word"
    close $fs
    putserv "NOTICE $nick :Relay added: $word"
  }
  return
}

proc relays {nick host hand chan text} {
  set fs [open "wordsEPL.txt"]
  set data [read -nonewline $fs]
  close $fs
  if {![string length $data]} {
    putserv "NOTICE $nick :I don't currently have any messages to relay."
    return 0
  }
  putserv "NOTICE $nick :My list of relay messages is as follows:"
  set fs [open "wordsEPL.txt"]
  seek $fs 0 start
  while {[gets $fs line] >= 0} {
    puthelp "NOTICE $nick :$line"
  }
  close $fs
}

proc msgclear {nick host hand chan text} {
  close [open "wordsEPL.txt" w]
}

proc relay_to_chan {nick host hand chan text} {
  global mchan rchan
  if {[string equal -nocase $chan $mchan]} {
    set fs [open "wordsEPL.txt"]
    seek $fs 0 start
    while {[gets $fs line] >= 0} {
      if {[regexp -nocase ".*$line.*" $text match]} {
        putserv "PRIVMSG $rchan :$match"
        break
      }
    }
    close $fs
    return
  }
  return
}

proc delword {nick host hand chan text} {
  global mchan
  if {[string equal -nocase $chan $mchan]} {
    set word [lindex [split $text] 0]
    set fs [open "wordsEPL.txt"]
    set words [split [read -nonewline $fs] \n]
    close $fs
    set match [lsearch -exact $words $word]
    set fs [open "wordsEPL.txt" w]
    set newwords [lreplace $words $match $match]
    puts $fs [join $newwords "\n"]
    close $fs
    putserv "NOTICE $nick :Relay deleted: $word."
  }
  return
}
Adjusted indenting, and added the changes in my earlier post. Rest is as it was initially.
NML_375
F
Football
Master
Posts: 205
Joined: Fri Dec 26, 2008 3:08 pm
Location: Quakenet, #Football

Post by Football »

splendid! thanks for all the help.
Idling at #Football, Quakenet.
User avatar
username
Op
Posts: 196
Joined: Thu Oct 06, 2005 9:20 am
Location: Russian Federation, Podolsk
Contact:

Post by username »

Sorry, I forgot about

Code: Select all

return 0
after

Code: Select all

putserv "PRIVMSG $rchan :$text"
Архив TCL скриптов для ботов Eggdrop/Windrop:
http://egghelp.ru/
Post Reply