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.

Dynamic binds...

Help for those learning Tcl or writing their own scripts.
Post Reply
F
FallFromGrace
Voice
Posts: 17
Joined: Mon Jul 28, 2008 8:52 am

Dynamic binds...

Post by FallFromGrace »

I have a commands stored in mysql database. I want to bind them dynamically.

There is no problem if i do this:

Code: Select all

set rs [mysqlsel $sql "SELECT command, private FROM commands " -flatlist]

# f.e. rs = "first 1 second 1 third 1"

# my commands starts with a dot
# priv is a switch (0 = channel only, 1 = both, 2 = private only)
foreach {cmd priv} $rs {
		if {$priv != 2} {
			bind pub - ".$cmd" $cmd
		} elseif {$priv != 0} {
			bind msg - ".$cmd" priv:$cmd
		}
}

proc first {nick uhost hand chan text} {
  if {$text == ""} {
    putlog "first"
 } else {
    putlog "first, text = $text"
 }

}

proc second {nick uhost hand chan text} {
  if {$text == ""} {
    putlog "second"
 } else {
    putlog "second, text = $text"
 }
}

proc third {nick uhost hand chan text} {
  if {$text == ""} {
    putlog "third"
 } else {
    putlog "third, text = $text"
 }
}

proc priv:first {nick uhost hand text} {
  if {$text == ""} {
    putlog "first"
 } else {
    putlog "first, text = $text"
 }
}

proc priv:second {nick uhost hand text} {
  if {$text == ""} {
    putlog "second"
 } else {
    putlog "second, text = $text"
 }
}

proc priv:third {nick uhost hand text} {
  if {$text == ""} {
    putlog "third"
 } else {
    putlog "third, text = $text"
 }
}
But i have read a thread about binding private and channel commands to the same proc: http://forum.egghelp.org/viewtopic.php?t=11796

And i want to make it work:

Code: Select all

foreach {cmd priv} $rs {
		if {$priv != 2} {
			bind pub - ".$cmd" {$cmd $_pub1 $_pub2 $_pub3 $_pub4 $_pub5; #}
		} elseif {$priv != 0} {
			bind msg - ".$cmd" {$cmd $_msg1 $_msg2 $_msg3 "" $_msg4; #}
		}
}

proc first {nick uhost hand chan text} {
  if {$text == ""} {
    putlog "first"
 } else {
    putlog "first, text = $text"
 }
}

proc second {nick uhost hand chan text} {
  if {$text == ""} {
    putlog "second"
 } else {
    putlog "second, text = $text"
 }
}

proc third {nick uhost hand chan text} {
  if {$text == ""} {
    putlog "third"
 } else {
    putlog "third, text = $text"
 }
}
Bot binds ".first", ".second" and ".third" commands to a last bind (proc third), and i only see "third" as an universal answer for an all of these commands.

I think, there is something wrong with my syntax.
Last edited by FallFromGrace on Thu Jan 27, 2011 6:04 am, edited 3 times in total.
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Re: Dynamic binds...

Post by speechles »

FallFromGrace wrote:
proc frist {nick uhost hand chan text} {
To start off, you do not have a "first" procedure. It's actually frist. ;)

And secondly, why don't you do it this way?
This makes it easier for pub and msg to use the same procedure. Based on the amount of args passed the procedure can reform it's arguments to make commands easier. If it's a private message $chan will become their $nick. This makes it easier to script events as you only need to remember 1 place to send replies, $chan. Rather than guessing whether it was a pub or msg bind passed to each procedure. This way removes the guessing.

Code: Select all

foreach {cmd priv} $rs {
      if {$priv < 2} {
         bind pub - ".$cmd" $cmd
      } elseif {$priv > 0} {
         bind msg - ".$cmd" $cmd
      }
}

proc first {nick uhost hand chan {text ""}} {
  # no text = msg, rearrange arguments
  if {![string length $text]} { set text $chan ; set chan $nick }
  putlog "first"
}

proc second {nick uhost hand chan {text ""}} {
  # no text = msg, rearrange arguments
  if {![string length $text]} { set text $chan ; set chan $nick }
  putlog "second"
}

proc third {nick uhost hand chan {text ""}} {
  # no text = msg, rearrange arguments
  if {![string length $text]} { set text $chan ; set chan $nick }
  putlog "third"
}
Your code corrected would look like this, #comments denote the fixes.

Code: Select all

foreach {cmd priv} $rs {
      if {$priv != 2} {
         bind pub - ".$cmd" {$cmd $_pub1 $_pub2 $_pub3 $_pub4 $_pub5; #}
      } elseif {$priv != 0} {
         # pass the nickname as the channel, makes messaging easier
         bind msg - ".$cmd" {$cmd $_msg1 $_msg2 $_msg3 $_msg1 $_msg4; #}
      }
}

# corrected mispelling of first
proc first {nick uhost hand chan text} {
  putlog "first"
}

proc second {nick uhost hand chan text} {
  putlog "second"
}

proc third {nick uhost hand chan text} {
  putlog "third"
}
Either should work equally the same.
F
FallFromGrace
Voice
Posts: 17
Joined: Mon Jul 28, 2008 8:52 am

Post by FallFromGrace »

"proc first" is a sample, i have another names for procedures =)

I can't use your way to check $text == "", some of my commands should work both with text and without it.

Any way, "bind pub - ".$cmd" {$cmd $_pub1 $_pub2 $_pub3 $_pub4 $_pub5; #}" - won't work. As I said, it will only bind the last proc.

I think it's a special block of code, and I can't just write "$cmd" there.
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

Code: Select all

foreach {cmd priv} $rs {
      if {$priv < 2} {
         bind pub - ".$cmd" [list "$cmd" pub]
      } elseif {$priv > 0} {
         bind msg - ".$cmd" [list "$cmd" msg]
      }
}

proc first {type nick uhost hand chan {text ""}} {
  # type = msg, rearrange arguments
  if {[string equal "msg" $type]} { set text $chan ; set chan $nick }
  putlog "first"
}

proc second {type nick uhost hand chan {text ""}} {
  # type = msg, rearrange arguments
  if {[string equal "msg" $type]} { set text $chan ; set chan $nick }
  putlog "second"
}

proc third {type nick uhost hand chan {text ""}} {
  # type = msg, rearrange arguments
  if {[string equal "msg" $type]} { set text $chan ; set chan $nick }
  putlog "third"
}
This will work. Since some of your commands also have empty text field. Let's simply add a "type" to each bind that signifies to the procedures which has invoked it. Simple. ;)
F
FallFromGrace
Voice
Posts: 17
Joined: Mon Jul 28, 2008 8:52 am

Post by FallFromGrace »

i have tried to do such thing:

Code: Select all

set x "first"
bind pub - ".$x" {$x $_pub1 $_pub2 $_pub3 $_pub4 $_pub5; #}

set x "second"
bind pub - ".$x" {$x $_pub1 $_pub2 $_pub3 $_pub4 $_pub5; #}

set x "third"
bind pub - ".$x" {$x $_pub1 $_pub2 $_pub3 $_pub4 $_pub5; #}
But it doesnt work too! It binds all of these commands to ".third". How can i fix it?
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

FallFromGrace wrote:i have tried to do such thing:

Code: Select all

set x "first"
bind pub - ".$x" {$x $_pub1 $_pub2 $_pub3 $_pub4 $_pub5; #}

set x "second"
bind pub - ".$x" {$x $_pub1 $_pub2 $_pub3 $_pub4 $_pub5; #}

set x "third"
bind pub - ".$x" {$x $_pub1 $_pub2 $_pub3 $_pub4 $_pub5; #}
But it doesnt work too! It binds all of these commands to ".third". How can i fix it?
How is that anything like I showed you. Those { } braces suppress execution of substitution. Until the time the bind is triggered and processed, at that point the last value of $x will be used. This is why only the last entry for you works. Stop using that bind...{ } method. Use like I have below:

Code: Select all

# init rs as a list for testing
set [list "first" 1 "second" 2 "third" 3]

foreach {cmd priv} $rs {
      if {$priv < 2} {
         bind pub - ".$cmd" [list "$cmd" pub]
      } elseif {$priv > 0} {
         bind msg - ".$cmd" [list "$cmd" msg]
      }
}

proc first {type nick uhost hand chan {text ""}} {
  # type = msg, rearrange arguments
  if {[string equal "msg" $type]} { set text $chan ; set chan $nick }
  putlog "first"
}

proc second {type nick uhost hand chan {text ""}} {
  # type = msg, rearrange arguments
  if {[string equal "msg" $type]} { set text $chan ; set chan $nick }
  putlog "second"
}

proc third {type nick uhost hand chan {text ""}} {
  # type = msg, rearrange arguments
  if {[string equal "msg" $type]} { set text $chan ; set chan $nick }
  putlog "third"
}
F
FallFromGrace
Voice
Posts: 17
Joined: Mon Jul 28, 2008 8:52 am

Post by FallFromGrace »

it's not a best solution, but i'll try it, thanks
Post Reply