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.

Request : Simple Action Reply Script

Old posts that have not been replied to for several years.
Locked
c
cupid-

Request : Simple Action Reply Script

Post by cupid- »

can someone help me code a simple tcl script that responds randomly to actions please?

example:

* user slaps botnick.
* botnick slaps user with a [random item]

example 2:

* user slaps botnick.
botnick : [random respond]


Thanks in advance!
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Make your list of replies a tcl list. There are two ways to do this...

Code: Select all

set slaps {
    "I love you, $nick!"
    "\001ACTION slaps $nick back\001"
}
or

Code: Select all

set slaps [list
    "I love you, \$nick!"
    "\001ACTION slaps \$nick back\001"
]
Notice the difference...the braces ({}) add one layer of "protection" from the parser so special chars inside them (except braces) don't need to be escaped. In the first example, the escape sequence \001 will stay the way you see it, while in the second one they'll get parsed and made into the ascii char that ctcp's are enclosed in when the list is created. (You could prevent this by adding another backslash to escape the one you've got... if you really feel like it for some reason :P)

The $'s in the second example need to be escaped to avoid variable substitution at that point. $nick is not the nick of our victim because this list is created in the 'global namespace' before the proc using the messages is even triggered.

Now for the proc (that would best be triggered by a ctcp bind)...

Inside the proc body you'll have to import the variable to be able to use it (by using 'global slaps') or address it directly using a fully qualified namespace path (::slaps). Then use 'lindex' combined with 'rand' or 'expr rand()' and 'llength' to pick a random message. The message at this point is just a string, so the $nick part will not be replaced by the current 'nick' variable's content unless we tell tcl to parse the message for us. 'subst' is a good way to do this.

That's about all you need to know
...have fun.

PS: I never slapped botnick...that's a lie!
c
cupid-

Post by cupid- »

whoops! haha. my bad :P
c
cupid-

Post by cupid- »

i'm like 5hrs new to tcl scripting. can someone please correct my mistake?

Code: Select all


set slaps {
    "I love you, $nick!"
    "\001ACTION slaps $nick back\001"
}

bind CTCP - ACTION slaps
proc slaps {nick host handle chan keyword text} {
global slaps
global botnick
if {[lindex $text 2] == "slaps"} {
if {[lindex $text 1] == $botnick} {
putserv "PRIVMSG $chan : [lindex $slaps [rand [llength $slaps]]] ."
}
}
}

User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

[lindex $text 0] would be "slaps", but you need to split the text before using list commands (lindex) on it to avoid problems with speacial chars like [, { and "...
set text [split $text]
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

or even from:

Code: Select all

if {[lindex $text 2] == "slaps"} { 
to:

Code: Select all

if {[strlwr [lindex [split $text] 2]] == "slaps"} { 
Once the game is over, the king and the pawn go back in the same box.
c
cupid-

Post by cupid- »

i read what user adviced and i tried to script it and this was what i came out with. Yes, it works fine now..it responds on slaps...but it shows $nick instead of the user's nickname...advice?

Code: Select all


set slaps { 
    "I love you, $nick!" 
    "\001ACTION slaps $nick back\001" 
} 

bind CTCP - ACTION action:slaps
proc action:slaps {nick host handle chan keyword text} {
global slaps
global botnick
if { [string tolower [lindex $text 0]] == "slaps" } {
if { [string tolower [lindex $text 1]] == [string tolower $botnick]} {
putchan $chan "[lindex $slaps [rand [llength $slaps]]]"
}
}
}

User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

try this:

Code: Select all

set slaps { 
    "I love you, $nick!" 
    "\001ACTION slaps $nick back\001" 
} 

bind CTCP - ACTION action:slaps 
proc action:slaps {nick host handle chan keyword text} { 
  global slaps botnick 
  if { [string tolower [lindex $text 0]] == "slaps" } { 
    if { [string tolower [lindex $text 1]] == [string tolower $botnick]} {
      putchan $chan "[string map "\$nick $nick" [lindex $slaps [rand [llength $slaps]]]]"
    } 
  } 
}
Elen sila lúmenn' omentielvo
c
cupid-

Post by cupid- »

thank you very much Papillon!

it finally works the way i want it to!

*cheers*
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

From my first reply:
user wrote:...The message at this point is just a string, so the $nick part will not be replaced by the current 'nick' variable's content unless we tell tcl to parse the message for us. 'subst' is a good way to do this.
I suggested this method because it's more flexible than using static keyword->value translations, but if the nick is the only variable you'll ever use in the messages the string map solution is fine :) (subst would allow commands in the message to be performed at runtime too btw)

If you stick with the string map you should consider replacing the $ with another char to avoid having to escape it all the time (eg %nick)
Locked