egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Dynamic binds...

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
FallFromGrace
Voice


Joined: 28 Jul 2008
Posts: 17

PostPosted: Wed Jan 26, 2011 6:21 pm    Post subject: Dynamic binds... Reply with quote

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

There is no problem if i do this:
Code:
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:
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
Back to top
View user's profile Send private message
speechles
Revered One


Joined: 26 Aug 2006
Posts: 1398
Location: emerald triangle, california (coastal redwoods)

PostPosted: Wed Jan 26, 2011 6:35 pm    Post subject: Re: Dynamic binds... Reply with quote

FallFromGrace wrote:
Quote:
proc frist {nick uhost hand chan text} {

To start off, you do not have a "first" procedure. It's actually frist. Wink

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:
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:
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.
_________________
speechles' eggdrop tcl archive
Back to top
View user's profile Send private message
FallFromGrace
Voice


Joined: 28 Jul 2008
Posts: 17

PostPosted: Wed Jan 26, 2011 8:03 pm    Post subject: Reply with quote

"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.
Back to top
View user's profile Send private message
speechles
Revered One


Joined: 26 Aug 2006
Posts: 1398
Location: emerald triangle, california (coastal redwoods)

PostPosted: Wed Jan 26, 2011 9:09 pm    Post subject: Reply with quote

Code:
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. Wink
_________________
speechles' eggdrop tcl archive
Back to top
View user's profile Send private message
FallFromGrace
Voice


Joined: 28 Jul 2008
Posts: 17

PostPosted: Thu Jan 27, 2011 6:22 am    Post subject: Reply with quote

i have tried to do such thing:

Code:
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?
Back to top
View user's profile Send private message
speechles
Revered One


Joined: 26 Aug 2006
Posts: 1398
Location: emerald triangle, california (coastal redwoods)

PostPosted: Thu Jan 27, 2011 6:41 am    Post subject: Reply with quote

FallFromGrace wrote:
i have tried to do such thing:

Code:
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:
# 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"
}

_________________
speechles' eggdrop tcl archive
Back to top
View user's profile Send private message
FallFromGrace
Voice


Joined: 28 Jul 2008
Posts: 17

PostPosted: Thu Jan 27, 2011 9:12 am    Post subject: Reply with quote

it's not a best solution, but i'll try it, thanks
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber