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.

Modifying decision.tcl

Help for those learning Tcl or writing their own scripts.
Post Reply
d
droune
Voice
Posts: 2
Joined: Tue May 20, 2008 5:16 pm
Location: Finland

Modifying decision.tcl

Post by droune »

Hello.

I found this great script which can make some decisions for me. It works like this:

<user> [ ] option 1 [ ] option 2 [ ] option 3
<bot> [x] option 1 [ ] option 2 [ ] option 3

The idea is great but I'd like to modify it a bit. The best way to avoid channel flooding is to set the user input on private messages and the actual output on the channel. Example:

User sends a private message to bot: "[ ] option 1 [ ] option 2 [ ] option 3"
Bot chooses randomly an option and prints the output on #channel:
<bot> user on private: [ ] option 1 [x] option 2 [ ] option 3

So I modified the code like this (Note: I haven't touched the actual text manipulating. I've only changed the first four lines and the puthelp-line):

Code: Select all

set channl #channel
bind msg - \[ proc:decision
proc proc:decision {nick host hand arguments} {
global channl

   set arguments [split $arguments]
   set count 1
   set klammer_count 0
   if {([lindex $arguments 0] == "]") || ([string first "]" [lindex $arguments 0]] == 0)} {
      incr klammer_count
   } else {
      return
   }
   while {$count != [llength $arguments]} {
      if {[lindex $arguments $count] == "\["} {
         set tmp $count
         incr tmp
         if {([lindex $arguments $tmp] == "]") || ([string first "]" [lindex $arguments $tmp]] == 0)} {
            incr klammer_count
         }
      }
      incr count
   }
   set count 0
   set myrand [rand $klammer_count]
   set klammer_count 0
   if {$myrand == 0} {
      set output "\[x"
      while {$count != [llength $arguments]} {
         set output "$output[lindex $arguments $count] "
         incr count
      }
   } else {
      set output "\["
      while {$count != [llength $arguments]} {
         if {[lindex $arguments $count] == "\["} {
            set tmp $count
            incr tmp
            if {([lindex $arguments $tmp] == "]") || ([string first "]" [lindex $arguments $tmp]] == 0)} {
               incr klammer_count
            }
         }
         if {$klammer_count == $myrand} {
            if {([lindex $arguments $count] == "]") || ([string first "]" [lindex $arguments $count]] == 0)} {
               set output "[string range $output 0 end]x"
            } else {
               set output "$output "
            }
         } else {
            set output "$output "
         }
         set output "$output[lindex $arguments $count]"
         incr count
      }

   }
   puthelp "PRIVMSG $channl :$nick on private: '$output'"
}
But the code doesn't seem to work. At first I thought the problem was in the bind part as I'm using MSG, but it treats everything identically as PUB.

So could someone help me? I'm really running out of ideas. Thanks in advance.

P.S. My first post :)
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

your problem most likely is in the very first line

Code: Select all

set channl #channel 
tcl interpret everything following # on a line as a comment, so you need to do

Code: Select all

set channl "#channel" 
for $channl not to be an empty variable
Elen sila lúmenn' omentielvo
d
droune
Voice
Posts: 2
Joined: Tue May 20, 2008 5:16 pm
Location: Finland

Post by droune »

Papillon wrote:your problem most likely is in the very first line

Code: Select all

set channl #channel 
tcl interpret everything following # on a line as a comment, so you need to do

Code: Select all

set channl "#channel" 
for $channl not to be an empty variable
Ahh, so that's the problem. It works now. Thanks a lot :)
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

I got really bored, so redesigned it abit. Don't shoot me if any errors occur as I am unable to test it on an eggdrop and my tcl abilities might be a bit rusty

Code: Select all

set channl "#chaneelyouwant"
bind msg - \[ making_choices
proc making_choices {nick host hand arg} {
	set arg [fixthearg "$arg"]
	set opts [regexp -all {\[\]} $arg]
	if {[lindex [split $arg] 0] == {\[\]} || $opts == 1} { return }
	set inx [lsearch -all [split $arg] {\[\]}]
	set ran [rand $opts]
	set showlist [list ""]
	for {set st 0} {$st <= [expr $opts -1]} {incr st} {
		set putin ""
		if {$st == [expr $opts -1]} { 
			set inx_str [join [lrange [split $arg] [expr [lindex $inx $st] +1] end]]
		} else {
			set inx_str [join [lrange [split $arg] [expr [lindex $inx $st] +1] [expr [lindex $inx [expr $st +1]] -1]]]
		}
		if {$inx_str == {}} { continue }
		if {$st == $ran} { set putin x }
		lappend showlist [join "\[$putin\] $inx_str"]
	}
	puthelp "PRIVMSG $::channl :[join $showlist]"
}

proc fixthearg {arg} {
	regsub -all {\[\]} "\[$arg" " \[\] " arg
	regsub -all {  } $arg { } arg
	return "$arg"
}
It should be able to handle abit more faulty inputs... hopefully :D
Elen sila lúmenn' omentielvo
Post Reply