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.

varibles help

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
G
Gemster
Halfop
Posts: 51
Joined: Mon Oct 04, 2010 5:43 pm

varibles help

Post by Gemster »

Hi, im very new to tcl scripting .

Anyways i have made a little test to see how varibles would work but i keep running into ptoblems and have searched all sorts of docs and tryed always to make this happen but always fail.

Code: Select all

bind pub - ".ctest" ctest 
proc ctest {nick host hand channel text} {

if {$text == on} {
set temptest "on"
putmsg ###1 "ctest is now $temptest"
}

if {$text == off} {
set temptest "off"
putmsg ###1 "ctest is now $temptest"
}

if {$text == check} {
putmsg ###1 "ctest is currently $temptest"
}
}
The first 2 work fine but the "check" does not :/

Thanks
Gemster
w
willyw
Revered One
Posts: 1203
Joined: Thu Jan 15, 2009 12:55 am

Re: varibles help

Post by willyw »

There is nothing to set temptest after the if command that looks for check.


Next, try enclosing in double quotes.
Example:
if {$text == "check"}

In another thread of yours, ( http://forum.egghelp.org/viewtopic.php? ... inet#96036 )

I mentioned this site:http://suninet.the-demon.de/

I suspect that you haven't been through that whole thing yet.
:)
Here:
http://suninet.the-demon.de/071.htm
it specifically says to enclose in double quotes.

There are times that you don't need to do so. I'm not the one to explain that. Nor have I spent time researching it.
And I can't tell you why it seems to work ok in the first two if statements, with
on
and
off.
Maybe a guru can jump in here and explain that.


I did experiment with your little script, both with and without double quotes around the word "check".
Try it for yourself. Both ways. Be logged in to the partyline of your bot when you do, and watch for error messages. You'll see.

By the way, I think that experimenting like you are doing is great!
Keep it up.
G
Gemster
Halfop
Posts: 51
Joined: Mon Oct 04, 2010 5:43 pm

Post by Gemster »

Hi willyw
There is nothing to set temptest after the if command that looks for check.
The first 2 sets the varible and the check just displays it in the chan, no need. For example it displays if something is on/off.

I suspect that you haven't been through that whole thing yet.

Here:
http://suninet.the-demon.de/071.htm
it specifically says to enclose in double quotes.
I have read that site and useing it for reference. it states that its

Code: Select all

 set varname "something here"
in quotes like i have.

For if statements it says that anything inside of quotes is counted as plain text. Also i took the if statement examples from there too.

Anyways i tryed what you posted and yet the first 2 works but when it comes to the "check" it throws something like "no such varible "temptest".

Thanks
Gemster
G
Gemster
Halfop
Posts: 51
Joined: Mon Oct 04, 2010 5:43 pm

Post by Gemster »

ok tryed this

Code: Select all

bind pub - ".ctest" ctest 
proc ctest {nick host hand channel text} {

if {$text == "on"} {
set temptest "on"
putmsg ###1 "ctest is now $temptest"
}

if {$text == "off"} {
set temptest "off"
putmsg ###1 "ctest is now $temptest"
}


if {$text == "check"} {
putmsg ###1 "ctest is currently $temptest"
}
}
Then in the channel:
[07:31] <Gemster> .ctest on
[07:31] <Stoner> ctest is now on
[07:31] <Gemster> .ctest off
[07:31] <Stoner> ctest is now off
[07:32] <Gemster> .ctest check

then in the eggdrops partyline:
Tcl error [ctest]: can't read "temptest": no such variable

:/ still dont work.

Thanks
Gemster
User avatar
TCL_no_TK
Owner
Posts: 509
Joined: Fri Aug 25, 2006 7:05 pm
Location: England, Yorkshire

Post by TCL_no_TK »

global

Code: Select all

proc ctest {nick host hand channel text} { 
 global temptest
w
willyw
Revered One
Posts: 1203
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

Gemster wrote:
There is nothing to set temptest after the if command that looks for check.
The first 2 sets the varible and the check just displays it in the chan, no need. For example it displays if something is on/off.
Ah. Ok.
I didn't understand what your intention was.
I suspect that you haven't been through that whole thing yet.

Here:
http://suninet.the-demon.de/071.htm
it specifically says to enclose in double quotes.
I have read that site and useing it for reference.
I hope you found it a good place to start.
it states that its

Code: Select all

 set varname "something here"
in quotes like i have.

For if statements it says that anything inside of quotes is counted as plain text. Also i took the if statement examples from there too.
Well... it has been quite a while since I myself went through that site, top to bottom.
Perhaps I was skimming too quickly... but on this page, http://suninet.the-demon.de/071.htm
I see:
"This can be two strings, .... or some text (enclosed in quotes like also told in Chapter 3.3) "

Anyways i tryed what you posted and yet the first 2 works but when it comes to the "check" it throws something like "no such varible "temptest".

And TCL_no_TK has illustrated the solution.

The explantion is here: http://suninet.the-demon.de/055.htm

I hope this helps.

Hang in there... keep experimenting. :)
G
Gemster
Halfop
Posts: 51
Joined: Mon Oct 04, 2010 5:43 pm

Post by Gemster »

TCL_no_TK wrote:global

Code: Select all

proc ctest {nick host hand channel text} { 
 global temptest
Thanks TCL_no_TK, that done the trick.

Yes willyw, http://suninet.the-demon.de/ is a great site, i love it :D.

Im working my way down it and if i see something that i like i try to code with it.

So far ive made quite a good script with commands that i have found and got working :D

Heres an example to show how much ive learned so far for if statments and global/local vars:

Code: Select all

#Tssync 
bind pub - ".tssync" tssync
proc tssync {nick host hand channel text} {
global tss
if {[onchan $nick #opers] == 1 && [isop $nick #opers] == 1} {
if {$text == "on"} {
set tss "on"
putserv "PRIVMSG #opers :TSSync turned \0034\002ON\002\003 by \00310$nick\003."
        bind cron - "0 * * * *"  tssync_doit
}
if {$text == "off"} {
set tss "off"
putserv "PRIVMSG #opers :TSSync turned \0034\002OFF\002\003 by \00310$nick\003."
        unbind cron - "0 * * * *"  tssync_doit
}
if {$text == "check"} {
putmsg #opers "Tssync is currently $tss"
}
if {$text != "on" && $text != "off" && $text != "check"} {
putquick "NOTICE $nick : syntax is .tssync on/off/check"
}
 } else {
  putmsg #opers "Sorry \00310$nick\003, you dont have access to use that command"
}
}
proc tssync_doit {min hour day month weekday} { 
        putserv "PRIVMSG OperServ :tssync"
}
Its not that good i know and there will be plenty of room in that little code for improvements but as im just starting to get the hang of it im quite proud of that little code :D

Thanks
Gemster
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

You should 'split' the $text and take what you need from it with 'lindex'

Code: Select all

set text [lindex [split $text] 0]
if {[onchan $nick #opers] == 1 && [isop $nick #opers] == 1} {
there's no need to check bouth, stick with just the isop as it wil return 1 if that user is on the channel and has op, else will return 0.
Also, here's another tip:

Code: Select all

if {$something} {
  will do this part if $something is equal with 1
}
if {!$something} {
  will do this part if $something is equal with 0
}
if {$text == "on"} {
You should start using either [string tolower $text] or [string match -nocase $text "on"] (same thing for "off" and "check") as "ON" isn't the same thing as 'on' and the script won't work
I would replace:

Code: Select all

if {$text != "on" && $text != "off" && $text != "check"} {
  putquick "NOTICE $nick : syntax is .tssync on/off/check"
} 
with something easier to change:

Code: Select all

set commands [list "on" "off" "check"]
if {![lsearch -exact $commands $text]} {
  putquick "NOTICE $nick : syntax is .tssync on/off/check"
}
OR use 'switch -nocase -- $text' like i did on the other topic that would take care of other inputs for you.

Code: Select all

switch -nocase -- [lindex [split $text] 0] {
  "on" {
    # on stuff here
  }
  "off" {
    # off stuff here
  }
  "check" {
    # check stuff here
  }
  default {
    # any other command goes here
  }
}
Move the 'if' check that sends this message:

Code: Select all

putmsg #opers "Sorry \00310$nick\003, you dont have access to use that command"
at top and return after like this:

Code: Select all

if {![isop $nick "#opers"]} { 
  putserv "PRIVMSG #opers :Sorry \00310$nick\003, you dont have access to use that command" 
  return
}
Here's my version of your code with 'switch':

Code: Select all

bind pub - ".tssync" pub:tssync

proc pub:tssync {nick uhost hand chan text} {
  global tss
  if {![isop $nick "#opers"]} {
    putserv "PRIVMSG $chan :Sorry \00310$nick\003, you dont have access to use that command" 
    return
  }
  switch -nocase -- [lindex [split $text] 0] {
    "on" {
      set tss "on"
      putserv "PRIVMSG $chan :TSSync turned \0034\002ON\002\003 by \00310$nick\003."
      bind cron - "* */1 * * *" cron:tssync
    }
    "off" {
      set tss "off"
      putserv "PRIVMSG $chan :TSSync turned \0034\002OFF\002\003 by \00310$nick\003."
      unbind cron - "* */1 * * *" cron:tssync
    }
    "check" {
      putserv "PRIVMSG $chan :TSSync is currently $tss"
    }
    default {
      putserv "NOTICE $nick : syntax is .tssync on/off/check"
    }
  }
}

proc cron:tssync {min hour day month weekday} {
  putserv "PRIVMSG OperServ :tssync"
}
Edit: Fixed minor typos and added the missing bind. :oops:
Last edited by caesar on Thu Feb 24, 2011 3:08 pm, edited 5 times in total.
Once the game is over, the king and the pawn go back in the same box.
w
willyw
Revered One
Posts: 1203
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

Take time to read this and bookmark it :
http://www.peterre.info/characters.html

Bookmark it, because you'll want to return to it, and re-read it too. :)
How to write eggdrop scripts that won't choke on special characters

by "Peterre"
G
Gemster
Halfop
Posts: 51
Joined: Mon Oct 04, 2010 5:43 pm

Post by Gemster »

caesar, your code does not work :/

When i do ".tssync on" i get this error:
Tcl error [tssync]: invalid command name "tssync"
Thanks
Gemster
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Oh snap. </facepalm>

You get that error because you are using your old bind instead of the one I should have included in my previous post. :roll:

Before the:

Code: Select all

proc pub:tssync {nick uhost hand chan text} { 
add:

Code: Select all

bind pub * .tssync pub:tssync
and remove other code. I've made this change in my previous post too.

Edit: Here's a enhanced version, one that is bug free (do a .tssync on or off twice and see what happens)

Code: Select all

bind pub - ".tssync" pub:tssync

proc pub:tssync {nick host hand chan text} {
  if {![string match -nocase $chan "#opers"]} return 
  if {![isop $nick #opers]} {
    putserv "PRIVMSG $chan :Sorry \00310$nick\003, you dont have access to use that command"
    return
  }
  switch -nocase -- [lindex [split $text] 0] {
    "on" {
      if {![bind:check "cron:tssync"]} {
        bind cron - "* */1 * * *" cron:tssync
        putserv "PRIVMSG $chan :TSSync turned \0039\002ON\002\003 by \00310$nick\003."
        } else {
        putserv "PRIVMSG $chan :TSSync is already \0039\002OFF\002\003."
      }
    }
    "off" {
      if {[bind:check "cron:tssync"]} {
        unbind cron - "* */1 * * *" cron:tssync
        putserv "PRIVMSG $chan :TSSync turned \0034\002OFF\002\003 by \00310$nick\003."
        } else {
        putserv "PRIVMSG $chan :TSSync is already \0034\002OFF\002\003."
      }
    }
    "check" {
      if {[bind:check "cron:tssync"]} {
        set status "\0039\002ON\002\003"
        } else {
        set status "\0034\002OFF\002\003"
      }
      putserv "PRIVMSG $chan :TSSync is currently $status"
    }
    default {
      putserv "NOTICE $nick :Correct syntax is: \002.tssync on/off/check\002"
    }
  }
}

proc bind:check {name} {
  set status 0
  foreach {type flags command hits bind} [join [binds]] {
    if {[string equal -nocase $bind $name]} {
      set status 1
      break
    }
  }
  return $status
}

proc cron:tssync {min hour day month weekday} {
  putserv "PRIVMSG OperServ :tssync"
}
I took the liberty to make it work only in #opers channel. You should remove the 'if {![string match -nocase $chan "#opers"]} return' line if don't want this behavior.
Once the game is over, the king and the pawn go back in the same box.
G
Gemster
Halfop
Posts: 51
Joined: Mon Oct 04, 2010 5:43 pm

Post by Gemster »

Wow caesar lol,

Slow down :D

Im still learning rember. Anyways let me get the first code posted working then ill move onto your last version, By looking at the code i see it checks the var when u do on/off.

Anyways befor i test that code and learn that id like to learn the first 1 and get that sorted befor i move on :D

well, the first version i done what u sed but i still get errors:

Tcl error [pub:tssync]: bad option "-nocase": must be -exact, -glob, -regexp, or --

Thanks
Gemster
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

I think you have an older version of TCL, cos in version 8.5.8 that I have the 'nocase' option exist. Anyway, replace the:

Code: Select all

switch -nocase -- [lindex [split $text] 0] {
with:

Code: Select all

switch -- [string tolower [lindex [split $text] 0]] {
and should work just fine.

In the second code with the 'bind:check' proc I do a check if the bind exists or not, cos if it exists and you want to create it again, or delete something that doesn't exist will give you an error. I just covered all the possible errors. :)
Once the game is over, the king and the pawn go back in the same box.
G
Gemster
Halfop
Posts: 51
Joined: Mon Oct 04, 2010 5:43 pm

Post by Gemster »

Thanks caesar,

It now works but i found another problem :/

"* */1 * * *" makes it do it every 1 min not once an hour.

Thanks
Gemster
w
willyw
Revered One
Posts: 1203
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

Gemster wrote: ...
"* */1 * * *" makes it do it every 1 min not once an hour.
Google for
crontab
and find some examples/help on how to use crontab.

All you are looking for, is the syntax.
You'll find other specific info on crontab too.

From tcl-commands.doc:
All cron operators are supported. Please refer to the crontab
manual for their meanings.

Here's a few:
http://linux.about.com/od/commands/l/bl ... rontab.htm
http://ss64.com/bash/crontab.html
http://adminschoice.com/crontab-quick-reference

(Every time I use it, I have to go look it up too )


I hope this helps.
Post Reply