| View previous topic :: View next topic |
| Author |
Message |
jeffo Voice
Joined: 02 Mar 2011 Posts: 9
|
Posted: Wed Mar 02, 2011 9:41 pm Post subject: 1st tcl script - text event trigger - help needed |
|
|
hi all,
as u've seen in the topic it's my first tcl one (did mirc scripts already).
here's my code so far:
| Code: |
bind pubm o|* "!game" game
proc game {nick uhost hand chan rest}
{
if {[string equal -nocase "!game" $text]} {random_int}
proc random_int { } {
global myrand
set myrand [expr int(rand() * 7)]
return $myrand
}
{
#if {$myrand == 0} {set $myrand i+1} //<-needed??????????
if {$myrand == 1} {putserv "PRIVMSG #channel:$nick plays 1!"}
elseif {$myrand == 2} {putserv "PRIVMSG #channel:$nick plays 2!"}
elseif {$myrand == 3} {putserv "PRIVMSG #channel:$nick plays 3!"}
elseif {$myrand == 4} {putserv "PRIVMSG #channel:$nick plays 4!"}
elseif {$myrand == 5} {putserv "PRIVMSG #channel:$nick plays 5!"}
elseif {$myrand == 6} {putserv "PRIVMSG #channel:$nick plays 6!"}
elseif {$myrand == 7} {putserv "PRIVMSG #channel:$nick plays 7!"}
}
}
|
as u can see, it shall trigger an "!game" from ops and randomnize the output in a public message.
as it is my first tcl, i'm not sure if i got the syntax right, probably/most likely not. so help a newbie here pls.
thx in advance |
|
| Back to top |
|
 |
thommey Halfop
Joined: 01 Apr 2008 Posts: 73
|
Posted: Wed Mar 02, 2011 9:48 pm Post subject: |
|
|
- bind pubm doesn't work that way, reread the documentation about it.. the mask is matched against "#chan text", not against "text"
- $text isn't set in your proc, you called the last variable $rest
- defining a proc in a proc is almost always wrong, you wouldn't do that in mircscript either, are you sure you know mircscript?
- no need to "global myrand", a local variable is sufficient and better style if you store the return value of random_int
- your if control structure seems to be weird, it generates the id only if !game is equal to $text but the rest is evaluated in any case
- why not just .. "PRIVMSG #channel :$nick plays $myrand" instead of that spaghetticode?
- you violate fundamental Tcl syntax with your elseif statements and your bracing style (brace = {})
In the end, my advice is to read a tcl tutorial, here's a list:
Tcl tutorials:
Eggdrop specific:
I took the liberty of posting your "Tcl script" on a pastebin that has a static Tcl syntax checker and here you can see the result of that (the errors above the code): http://paste.tclhelp.net/?id=8p2 |
|
| Back to top |
|
 |
jeffo Voice
Joined: 02 Mar 2011 Posts: 9
|
Posted: Wed Mar 02, 2011 10:28 pm Post subject: |
|
|
yea, will do. ty, was trying to get my mirc script into tcl and as i've said i've never worked with tcl scripts, so u are right. but calling it "spaghetticode" is veeeery nice of u  |
|
| Back to top |
|
 |
jeffo Voice
Joined: 02 Mar 2011 Posts: 9
|
Posted: Wed Mar 02, 2011 10:42 pm Post subject: |
|
|
so...
| Code: |
bind pubm o|* "!game" game
proc game {nick host hand chan text}
{
if {[string equal -nocase "#channel:!game" $text]} {random_int}
#or add set var text here and return $text or can i leave it at all??
}
proc random_int { }
{
set myrand [expr int(rand() * 7)]
return $myrand
}
{
#if {$myrand == 0} {set $myrand i+1} //<-needed??????????
if {$myrand == 1} {putserv "PRIVMSG #channel:$nick plays TXT1!"}
elseif {$myrand == 2} {putserv "PRIVMSG #channel:$nick plays TXT2!"}
elseif {$myrand == 3} {putserv "PRIVMSG #channel:$nick plays TXT3!"}
elseif {$myrand == 4} {putserv "PRIVMSG #channel:$nick plays TXT4!"}
elseif {$myrand == 5} {putserv "PRIVMSG #channel:$nick plays TXT5!"}
elseif {$myrand == 6} {putserv "PRIVMSG #channel:$nick plays TXT6!"}
elseif {$myrand == 7} {putserv "PRIVMSG #channel:$nick plays TXT7!"}
#endif ???
}
|
...better?
and the if one is due to the output, it has to be a txt-msg instead of the no. out of the variable
and regarding the first error, i've read:
BIND [type] [Flags] [Event] [NameofProc]
edit:
| thommey wrote: | | I took the liberty of posting your "Tcl script" on a pastebin that has a static Tcl syntax checker and here you can see the result of that (the errors above the code): http://paste.tclhelp.net/?id=8p2 |
i don't get this static tcl checker -> leaving most of the braces, which i thought were needed from what i've read, removes some errors. guess i have to think over the syntax once again |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Thu Mar 03, 2011 3:07 am Post subject: |
|
|
If you want to trigger a command upon !game then you will need 'bind pub' not 'bind pubm'. By using 'pub' instead the:
| Code: |
if {[string equal -nocase "#channel:!game" $text]} {random_int}
|
can be removed. Instead of a bunch of 'if' and 'elseif' you should use switch. In order to get a random number from 1 to 7 (both included) not from 0 to 6
you need to use [expr [rand 7] +1] to have. Also, the correct definition of a proc is:
| Code: |
proc myproc {variables} {
# my stuff
}
|
Notice the { and }.
Anyway, here's a 'pub' code:
| Code: |
bind pub o|* !game pub:game
proc pub:game {nick uhost hand chan text} {
switch -- [expr [rand 7] +1] {
1 {
putserv "PRIVMSG #channel :$nick plays TXT1!"
}
2 {
putserv "PRIVMSG #channel :$nick plays TXT2!"
}
3 {
putserv "PRIVMSG #channel :$nick plays TXT3!"
}
4 {
putserv "PRIVMSG #channel :$nick plays TXT4!"
}
5 {
putserv "PRIVMSG #channel :$nick plays TXT5!"
}
6 {
putserv "PRIVMSG #channel :$nick plays TXT6!"
}
7 {
putserv "PRIVMSG #channel :$nick plays TXT7!"
}
}
}
|
_________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
jeffo Voice
Joined: 02 Mar 2011 Posts: 9
|
Posted: Thu Mar 03, 2011 2:22 pm Post subject: |
|
|
ty, will try it this way then. i thought the "rand()" is calling the function, but [rand 7] looks much more handy aswell as the "switch" cmd..
kudos |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Thu Mar 03, 2011 3:27 pm Post subject: |
|
|
While in other coding languages it's rand() in TCL it's [rand]. Also, notice the [ and ] that translate in to calling the function of whatever you got inside. _________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
arfer Master

Joined: 26 Nov 2004 Posts: 436 Location: Manchester, UK
|
Posted: Fri Mar 04, 2011 10:19 am Post subject: |
|
|
In core Tcl it is the mathamatical expression rand(), used together with the command expr to return a random floating point value between 0 and 1.
The command [rand <limit>] only exists in Eggdrop Tcl to return a random integer between 0 and <limit>-1
Hence if you are scripting for Eggdrop, either is allowed. _________________ I must have had nothing to do |
|
| Back to top |
|
 |
jeffo Voice
Joined: 02 Mar 2011 Posts: 9
|
Posted: Fri Mar 11, 2011 4:34 pm Post subject: |
|
|
just another question for you pro's, would be cool if you could help me there. is it possible to set the trigger so that it will function (in this case) to not only "!game", but also to look in whole sentences like "why dont you type !game while we're at it"
i've tried:
bind pub -|- * !game game
bind pub -|- "* !game" game
bind pub -|- *!game* game
bind pub -|- "*!game*" game
bind pubm -|- "* !game" game
bind pubm -|- "% !game" game
and so on, but nothing works...
i.e. "*!game*" will trigger on *!game* only but not on "is this a !game" |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Mar 11, 2011 4:38 pm Post subject: |
|
|
Wildcards such as * and % only works with the matching version of the binding (such as pubm for public channel chat and msgm for private messages). The proper binding would in this case be as follows:
| Code: | | bind pubm -|- "*!game*" game |
Edit: Fixed minor typo _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
jeffo Voice
Joined: 02 Mar 2011 Posts: 9
|
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Mar 11, 2011 6:05 pm Post subject: |
|
|
I can't see why the binding I proposed would interfere with bender_quotes, as they don't overlap.
If you were to apply the same kind of binding to the bender_quotes script however, you would have to rewrite the pub_bender proc as "arg" would contain the whole line of text (including the !game trigger), rather than the text following the first word.
With the simple scripts proposed in this thread, that wouldn't be an issue, since they don't care of the content of "text", though. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
jeffo Voice
Joined: 02 Mar 2011 Posts: 9
|
Posted: Fri Mar 11, 2011 7:42 pm Post subject: |
|
|
if i do pubm on it, it'll do
<..> <trigger>
-...- enter a number pitiful human! (notice)
the whole time, and only that
yea, trying to learn on the trigger thing, different script tho |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Mar 11, 2011 7:48 pm Post subject: |
|
|
So you were indeed applying the same kind of binding to the pub_bender proc. Thus, you'll either have to re-write pub_bender, or write a wrapper, to dig out the number from the whole line of text.
The simplest would probably be to write a wrapper, that actually don't bother with digging for the number, but use the random-line operation:
| Code: | bind pubm -|- "*!trigger" pubm_bender
proc pubm_bender {nick host handle channel text} {
pub_bender $nick $host $handle $channel ""
}
|
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
jeffo Voice
Joined: 02 Mar 2011 Posts: 9
|
Posted: Fri Mar 11, 2011 7:57 pm Post subject: |
|
|
| ty, gonna try that |
|
| Back to top |
|
 |
|