| View previous topic :: View next topic |
| Author |
Message |
seeb Voice
Joined: 14 Aug 2008 Posts: 6
|
Posted: Thu Aug 14, 2008 5:12 pm Post subject: Tcl script with constant database check |
|
|
hi all,
I just installed my first eggdrop bot. I never used tcl and it's some time ago that I programmed something in C.
I want to write a small script that checks my mysql database every minute and prints new infos to the channel. is that possible? does anything like this already exist? i found a tcl script which writes data into the DB, but how can I repeat such actions? Do I need a module or is a tcl script enough? |
|
| Back to top |
|
 |
seeb Voice
Joined: 14 Aug 2008 Posts: 6
|
Posted: Thu Aug 14, 2008 5:49 pm Post subject: |
|
|
Well, I found some parts and mixed them together. What do you think of this? How can I start it? How does my bot to know use this script? I hope I'll find out before wasting your time. And where can I get the libmysqltcl.so?
| Code: | # MySQL hostname
set db(host) "localhost"
# MySQL user
set db(id) "user"
# MySQL password
set db(pass) "pass"
# MySQL database
set db(dbase) "database"
# load the mysqltcl interface
load PATH/TO/libmysqltcl.so
bind time - "* * * * *" news_post
set db_handle [mysqlconnect -host $db(host) -user $db(id) -password $db(pass) -db $db(dbase)]
set news_version "1.0"
setudef flag newsengine
# --------------------------------------------------------------------
# news_post
# jede Minute die aktuellen Nachrichten ausspucken
# --------------------------------------------------------------------
proc news_post { } {
global db_handle news_noflags
set sql "SELECT text FROM geschichte ORDER BY geschichte_id desc limit 1"
#putloglev d * "NewsEngine: executing $sql"
set result [mysqlquery $db_handle $sql]
if {[set row [mysqlnext $result]] != ""} {
set text [lindex $row 0]
puthelp "PRIVMSG #elygor : $text"
}
else {
puthelp "PRIVMSG $channel :Es ist nichts passiert!"
}
mysqlendquery $result
}
| [/quote][/code]
Last edited by seeb on Thu Aug 14, 2008 5:50 pm; edited 1 time in total |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Aug 14, 2008 5:50 pm Post subject: |
|
|
Tcl has no native support for mysql. There is, however, a package (mysqltcl) available for mysql connectivity. This would be loaded as a tcl package (or tcl module, not eggdrop module), and provides a complete API to the tcl environment. There is thus no need to develop your project as a module just for the mysql-support.
Most, if not all, that takes place within eggdrop is event-driven, so you'll most likely want some hook to trigger your check on a minutely basis. The time binding should do the trick (see doc/tcl-commands.doc for syntax and description). Roughly however, every minute it checks the current time against a given mask. If it matches, the associated code is evaluated.
I can't think of any specific script that does what you ask for at the moment, although they probably do exist. Since you are new to tcl, I'd suggest starting with a few simple projects just to get accustomed to tcl and it's variable- and namespaces, aswell as eggdrop's extensions. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
seeb Voice
Joined: 14 Aug 2008 Posts: 6
|
Posted: Thu Aug 14, 2008 5:51 pm Post subject: |
|
|
| thx for your reply, I guess we wrote simultaniously |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Aug 14, 2008 5:56 pm Post subject: |
|
|
Yup.
As for your posted code, I cannot see any direct flaws in it.
One remark however:
| Code: | | load PATH/TO/libmysqltcl.so |
This is not the preferred way of loading mysqltcl, instead, assuming it has been properly installed, use something like this:
| Code: | | package require mysqltcl |
| seeb wrote: | | And where can I get the libmysqltcl.so? |
Link is in my first post, but I'll repeat it here aswell:
http://www.xdobry.de/mysqltcl/ _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
seeb Voice
Joined: 14 Aug 2008 Posts: 6
|
Posted: Thu Aug 14, 2008 6:01 pm Post subject: |
|
|
| Well, I installed the lib and I changed the tcl script? How can I start it? |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Aug 14, 2008 6:05 pm Post subject: |
|
|
add a source command to your config-file, and restart your eggdrop:
| Code: | | source scripts/yourscript.tcl |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
seeb Voice
Joined: 14 Aug 2008 Posts: 6
|
Posted: Thu Aug 14, 2008 6:13 pm Post subject: |
|
|
i did so, but nothing happens. i also added
bind pub "-|-" !news news_post
but when i say !news in channel or private chat nothing happens, too. how can i debug whats going wrong? |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Aug 14, 2008 6:21 pm Post subject: |
|
|
Best way is usually to connect to the dcc-chat partyline, as you can setup your console-settings to include tcl errors and other interesting information.
There is however, one flaw in the script that I overlooked. The proc (function) named news_post has an empty argument list, yet the time binding will try to call it with 5 parameters (minute, hour, day, month, and year). Thus there will be a mismatch... The same will be true for your pub-binding, which will call the function with 5 parameters (nick, host, handle, channel, text).
| Code: | #change this:
proc news_post { } {
#into this:
proc news_post {minute hour day month year} { |
Although you will get away with it in this script, it is generally a bad idea to use the same function with multiple bindings of different kind, as different kinds of bindings provide different information as parameters, and in many cases, not the same number of parameters. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
seeb Voice
Joined: 14 Aug 2008 Posts: 6
|
Posted: Fri Aug 15, 2008 1:59 am Post subject: |
|
|
yeah it works thx a lot :)
here the script fpr those who want to try it, too
| Code: |
MySQL hostname
set db(host) "localhost"
# MySQL user
set db(id) "user"
# MySQL password
set db(pass) "pass"
# MySQL database
set db(dbase) "dbase"
# load the mysqltcl interface
package require mysqltcl
bind time - "* * * * *" news_post
set db_handle [mysqlconnect -host $db(host) -user $db(id) -password $db(pass) -db $db(dbase)]
set news_version "1.0"
setudef flag newsengine
# --------------------------------------------------------------------
# news_post
# jede Minute die aktuellen Nachrichten ausspucken
# --------------------------------------------------------------------
proc news_post {minute hour day month year} {
global db_handle news_noflags
set sql "SELECT text FROM geschichte ORDER BY geschichte_id desc limit 1"
#putloglev d * "NewsEngine: executing $sql"
set result [mysqlquery $db_handle $sql]
if {[set row [mysqlnext $result]] != ""} {
set text [lindex $row 0]
puthelp "PRIVMSG #elygor : $text"
}
else {
puthelp "PRIVMSG $channel :Es ist nichts passiert!"
}
mysqlendquery $result
}
|
|
|
| Back to top |
|
 |
|