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.

help with this script

Help for those learning Tcl or writing their own scripts.
Post Reply
Z
Zohaib
Voice
Posts: 22
Joined: Wed May 18, 2011 3:30 am

help with this script

Post by Zohaib »

hello,
I need help wit his script, it will read a vhost from list and say it on channel and then remove it from list too so it dont give the same reply again. I am able to made this to read random vhosts from list but how do I make it to delete that vhost from list so it is not repeated again. thanks in advance

Code: Select all

set vhostlist {

"vhost1.balabla.balabala"
"vhost2.balabla.balabala"
"vhost3.balabla.balabala"
"vhost4.balabla.balabala"
"vhost5.balabla.balabala"
"vhost6.balabla.balabala"
}

bind pub _ !vhost pub_vhost

proc pub_vhost {nick host hand channel args} {
putserv "PRIVMSG $channel : [randvhost $args]"
}




proc randvhost {nick} {
       global vhostlist
       return [lindex $vhostlist [expr int(rand()*[llength $vhostlist])]]
}
<Zohaib>
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Try this...

Code: Select all

set vhostlist { 
"vhost1.balabla.balabala" 
"vhost2.balabla.balabala" 
"vhost3.balabla.balabala" 
"vhost4.balabla.balabala" 
"vhost5.balabla.balabala" 
"vhost6.balabla.balabala" 
} 

bind pub _ !vhost pub_vhost 

proc pub_vhost {nick host hand channel text} { 
  putserv "PRIVMSG $channel : [randvhost $text]" 
} 

proc randvhost {nick} { 
  global vhostlist 

  # save the index number of the chosen list item #
  set ret [lindex $vhostlist [set idx [rand [llength $vhostlist]]]] 

  # remove the chosen item from the list #
  set vhostlist [lreplace $vhostlist $idx $idx]

  return $ret
}
Note: rehashing/restarting the bot will start again with all items in the provided list.
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
Z
Zohaib
Voice
Posts: 22
Joined: Wed May 18, 2011 3:30 am

Post by Zohaib »

thanks SpiK^^ ok I try to modify it by Saving it in a vhost.txt file, then it will not start again after rehash/restart right?
<Zohaib>
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Yes, you could put the vhosts in a text file, read them and pick one, then rewrite the file without the chosen vhost....
That would make the script remember where it was with the provided list.

When you have some of that wrote, post it back here for review:)

Goodluck,
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
Z
Zohaib
Voice
Posts: 22
Joined: Wed May 18, 2011 3:30 am

Post by Zohaib »

Code: Select all


bind pub _ !vhost pub_vhost 

proc pub_vhost {nick host hand channel text} { 
  putserv "PRIVMSG $channel : [randvhost $text]" 
} 

proc randvhost {nick} { 
  set vhostlist "/home/fun/eggdrop/scripts/vhosts.cfg" 
  set f [open $vhostlist r ] 
  set data [read -nonewline $f]
  set lines [split $data "\n"] 
  set randline [lindex $lines [set idx [rand [llength $lines]]]]
  set lines [lreplace $lines $idx $idx] 
  set f [open $f "w"] 
  puts $f [join $lines "\n"] 
  close $f
  return $randline
}
it reads from the line but it is not removing it. please tell me where i am doing it wrong
<Zohaib>
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Very close, but no cigar:)

1) You opened the file for reading only, which is fine, read the contents to a variable and close the file.

2) You can't do [open $f "w"], $f is just the channel reference to the file we opened earlier, not a route/file.name

You should try something more like this...

Code: Select all

bind pub _ !vhost pub_vhost

proc pub_vhost {nick host hand channel text} {
  putserv "PRIVMSG $channel : [randvhost $text]"
}

proc randvhost {nick} {
  set vhostlist "/home/fun/eggdrop/scripts/vhosts.cfg"

  # open the vhost file, read it to a var, and close the file #
  set f [open $vhostlist r]
  set data [read -nonewline $f]
  close $f

  # split $data to a list, choose one to show, and remove it from list #
  set lines [split $data "\n"]
  set randline [lindex $lines [set idx [rand [llength $lines]]]]
  set lines [lreplace $lines $idx $idx]

  # open vhost file for writing, write the file, and close the file #
  set f [open $vhostlist w]
  puts $f [join $lines "\n"]
  close $f

  return $randline
}

SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
Z
Zohaib
Voice
Posts: 22
Joined: Wed May 18, 2011 3:30 am

Post by Zohaib »

I see. Thanks SpiKe^^ wasn't possible for me without your help. Thanks again :)
<Zohaib>
Post Reply