09-20-2018, 04:40 +0200
Search result (5)
All posts by: Turbo
AuthorPost
Topic: mySQL Database Connect   Forum: workscript announcements      Goto post
Turbo  03-03-2013, 18:43 | mySQL Database Connect MSN  Homepage
Moderator
Member since 06/2007
5 Posts
Location: Netherlands
  1. _____           _             ____
#|_   _|   _ _ __| |__   ___   |  _ \  _____   __
  1. | || | | | '__| '_ \ / _ \  | | | |/ _ \ \ / /
  2. | || |_| | |  | |_) | (_) | | |_| |  __/\ V /
  3. |_| \__,_|_|  |_.__/ \___/  |____/ \___| \_/
#
  1. -------------------------------------------------------------------
  2. | Author:    Björn 'Turbo' B.                             |
  3. -------------------------------------------------------------------
#
  1. -------------------------------------------------------------------
  2. | Require:    package mysqltcl                              |
  3. |         If you don`t have the package than download it at:  |
  4. |            (http://www.xdobry.de/mysqltcl/#downloads)        |
  5. -------------------------------------------------------------------
#
  1. -------------------------------------------------------------------
  2. |         Copyright © 2008 Turbo - All Rights Reserved.        |
  3. -------------------------------------------------------------------


namespace eval ::db {


    # - Mysql Configuration

    variable database ""
    variable username ""
    variable password ""

    ## Hostname can be used as empty or you fill in a hostname, such as localhost.
    variable hostname "localhost"

    # - Mysql Module Check

    if {[catch {package require mysqltcl} error ]} {
        putlog "Could not load mysqltcl, if you don't have it, download it at (http://www.xdobry.de/mysqltcl/#downloads)"
        return
    } else {
        package require mysqltcl
        putlog "Package Mysqltcl Version '[::mysql::baseinfo clientversion]' Loaded."
    }

    # - Bindings

    bind msg nm connect [namespace current]::connect
    bind msg nm disconnect [namespace current]::disconnect
    bind msg nm status [namespace current]::status

    ## End Of Configuration

    ## Don`t touch anything beneath this (only if you know what you are doing)

    proc connect {nick host hand text} {
        global mysqlhandle
        variable database
        variable username
        variable password
        variable hostname
        if {$database == "" || $username == "" || $password == ""} {
            putserv "NOTICE $nick :(mySQL) Please check your mySQL configuration."
        } elseif {[string length $password] < 6} {
            putserv "NOTICE $nick :(mySQL) Password unsafe, make sure your password has more than 6 chars."
        } elseif {[info exists mysqlhandle] && [::mysql::state $mysqlhandle] == "IN_USE"} {
            putserv "NOTICE $nick :(mySQL) Already connected to a database."
        } else {
            putserv "NOTICE $nick :(mySQL) Connecting to server:"
            if {[catch { set mysqlhandle [::mysql::connect -user $username -db $database -host $hostname -password $password] } error]} {
                putserv "NOTICE $nick :(mySQL) Error:"
                foreach mysqlerror [split $error \n] {
                    putserv "NOTICE $nick :(mySQL) $mysqlerror"
                }
                putserv "NOTICE $nick :(mySQL) End of error."
            } else {
                putserv "NOTICE $nick :(mySQL) Connected to $database"
                putserv "NOTICE $nick :(mySQL) Handle: $mysqlhandle"
            }
            timer 300 [namespace current]::reconnect
        }
    }

    proc disconnect {nick host hand text} {
        global mysqlhandle
        if {![info exists mysqlhandle]} {
            putserv "NOTICE $nick :(mySQL) No mysql handle found, maybe you need to connect first."
        } elseif {[::mysql::state $mysqlhandle] == "NOT_A_HANDLE"} {
            putserv "NOTICE $nick :(mySQL) There is no connection found."
        } elseif {[::mysql::ping $mysqlhandle] == "0"} {
            putserv "NOTICE $nick :(mySQL) Connecting found but mysql server has gone, please connect again."
        } else {
            ::mysql::close $mysqlhandle
            unset mysqlhandle
            putserv "NOTICE $nick :(mySQL) Disconnected from the database."
            foreach mysqltimer [timers] {
                if {[string match *reconnect* $mysqltimer]} {
                    killtimer [lindex [split [join $mysqltimer] ] 2]
                }
            }
        }
    }

    proc status {nick host hand text} {
        global mysqlhandle
        if {![info exists mysqlhandle]} {
            putserv "NOTICE $nick :(mySQL) No mysql handle, connect first."
        } elseif {[::mysql::state $mysqlhandle] == "NOT_A_HANDLE"} {
            putserv "NOTICE $nick :(mySQL) No status possible when not connected."
        } else {
            putserv "NOTICE $nick :(mySQL) State of $mysqlhandle: [::mysql::state $mysqlhandle]"
            putserv "NOTICE $nick :(mySQL) Name of the database: [::mysql::info $mysqlhandle dbname]"
            putserv "NOTICE $nick :(mySQL) Serverversion: [::mysql::info $mysqlhandle serverversion]"
            putserv "NOTICE $nick :(mySQL) Hostname: [::mysql::info $mysqlhandle host]"
            putserv "NOTICE $nick :(mySQL) Tables: [::mysql::info $mysqlhandle tables]"

        }
    }

    proc reconnect {} {
        global mysqlhandle
        variable database
        variable username
        variable password
        variable hostname
        ::mysql::close $mysqlhandle
        if {[catch { set mysqlhandle [::mysql::connect -user $username -db $database -host $hostname -password $password] } error]} {
            foreach mysqlerror [split $error \n] {
                putlog "(mySQL) $mysqlerror"
            }
        } else {
            putlog "Reconnected to: $database"
        }
        timer 300 [namespace current]::reconnect
    }

}

putlog "DBconnect.tcl Is Coded By 'Björn 'Turbo' B.'"
Topic: [EGG] Slot Script (Playing slot on your channel with friends or just alone)   Forum: workscript announcements      Goto post
Turbo  10-27-2009, 17:12 MSN  Homepage
Moderator
Member since 06/2007
5 Posts
Location: Netherlands
    # _____           _             ____
    #|_   _|   _ _ __| |__   ___   |  _ \  _____   __
    #  | || | | | '__| '_ \ / _ \  | | | |/ _ \ \ / /
    #  | || |_| | |  | |_) | (_) | | |_| |  __/\ V /
    #  |_| \__,_|_|  |_.__/ \___/  |____/ \___| \_/
    #
    # -------------------------------------------------------------------
    # | Author:    Björn 'Turbo' B.                             |
    # | Translated To German By: André 'Blaeuling' T.            |
    # | Scripting-Helper: Sören 'Virtu' B.                    |
    # -------------------------------------------------------------------
    #
    # -------------------------------------------------------------------
    # | Require:    dbconnect.tcl                                 |
    # -------------------------------------------------------------------
    #
    # -------------------------------------------------------------------
    # |         Copyright © 2010 Turbo - All Rights Reserved.           |
    # -------------------------------------------------------------------
    #
    # Slot machine to play on every channel the bot is on.
    # With player & channel stats.
    #
    # Note: This script is using mysqltcl
    #
    # Commands are available below
    #
    # .slot        .stats
    # .mymoney    .jackpot
    # .myrank    .top5players
    # .mystats    .version
    # .bank
    #
    # Please check always the version, you might be using an older version.
    #
    # Version/Date check list.
    #    1.0 - June 13 2008        2.7 - Januari 18 2013
    #    1.1 - Januari 11 2009        2.8 - Februari 19 2013
    #    1.2 - Februari 1 2009
    #    1.3 - May 17 2009
    #    1.4 - July 6 2009
    #    1.5 - July 18 2009
    #    1.6 - August 25 2009
    #    1.7 - Oktober 19 2009
    #    1.8 - December 3 2009
    #    1.9 - December 21 2009
    #    2.0 - Februari 10 2010
    #    2.1 - April 4 2010
    #    2.2 - April 26 2010
    #    2.3 - June 06 2010
    #    2.4 - December 20 2012
    #    2.5 - Januari 6 2013
    #    2.6 - Januari 10 2013


namespace eval ::slot_de {

    # - Database Connection Script Check

    if {![file exists scripts/dbconnect.tcl]} { putlog "Für slot.de-tcl brauchst du dbconnect.tcl"; return }

    # - Configuration

    # - Variables

    variable version "2.8 German"
    variable author "Created by Turbo & Translated to german by: Blaeuling"

    variable slots {
        "Glocke"
        "Herz"
        "7"
        "Nix"
        "Orange"
        "Bar"
        "Jackpot"
        "Kirsche"
        "Herz"
        "Zitrone"
        "Limette"
        "7"
        "Apfel"
        "Glocke"
        "Banane"
    }

    # - End Of Configuration

    # - Bindings

    bind pub -|- .slot [namespace current]::slot
    bind pub -|- .jackpot [namespace current]::jackpot
    bind pub -|- .top5players [namespace current]::top5players
    bind pub -|- .mymoney [namespace current]::mymoney
    bind pub -|- .myrank [namespace current]::myrank
    bind pub -|- .mystats [namespace current]::mystats
    bind pub -|- .bank [namespace current]::bank
    bind pub -|- .stats [namespace current]::stats
    bind pub -|- .version [namespace current]::showversion

    # - End Of Bindings

    ## Ab hier Finger vom Code solang du nicht weißt was du tust!


    ## Slot Game

    proc slot {nick host hand chan text} {
        global waittime mysqlhandle
        if {![connected]} { putserv "privmsg $chan :\002(\017Slot\002)\017 Keine Verbindung zur Datenbank. Bitte wende dich an einen Admin."; return }
        set hostname "*!$host"
        if {[info exists ::waittime($hostname)]} { return }
        ::mysql::sel $mysqlhandle "CREATE TABLE IF NOT EXISTS `slot_accounts` ( `player` varchar(32) not null, `hostname` varchar(100) not null, `since` varchar(20) not null, `won` int(11) not null, `lose` int(11) not null, `money` int(100) not null, `lastplay` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP )"
        ::mysql::sel $mysqlhandle "CREATE TABLE IF NOT EXISTS `slot_jackpot` ( `money` int(30) not null, `lastwon` varchar(50) not null, `time` int(20) not null, `channel` varchar(50) not null )"
        ::mysql::sel $mysqlhandle "CREATE TABLE IF NOT EXISTS `slot_stats` ( `channel` varchar(50) not null, `played` int(30) not null )"
        if {[::mysql::sel $mysqlhandle "SELECT money FROM `slot_jackpot`" -flatlist] == ""} {
                ::mysql::sel $mysqlhandle "INSERT INTO slot_jackpot VALUES ( '0' , 'None' , '0' , '#' )"
        }
        if {[::mysql::sel $mysqlhandle "SELECT `played` FROM `slot_stats` WHERE `channel` = '[::mysql::escape $chan]'" -flatlist] == ""} {
                ::mysql::sel $mysqlhandle "INSERT INTO slot_stats VALUES ( '[::mysql::escape $chan]' , '0' )"
        }
        if {[isadded $hostname] == "0"} {
            if {[validplayer $nick] == "1"} {
                putserv "NOTICE $nick :\002(\017Slot\002)\017 Dieser Nickname ist bereits vergeben. Bitte wende dich an einen Admin"
                return
            } else {
                ::mysql::sel $mysqlhandle "INSERT INTO slot_accounts VALUES ( '[::mysql::escape $nick]' , '[::mysql::escape $hostname]' , '[strftime {%m/%d/%y}]' , '0' , '0' , '500' , now() )"
            }
        }
        set stats [join [::mysql::sel $mysqlhandle "SELECT `played` FROM `slot_stats` WHERE `channel` = '[::mysql::escape $chan]'" -flatlist]]
        set money [join [::mysql::sel $mysqlhandle "SELECT `money` FROM `slot_accounts` WHERE `hostname` = '[::mysql::escape $hostname]'" -flatlist]]
        if {$money < 30} {
            putserv "NOTICE $nick :\002(\017Slot\002)\017 Du hast nicht genug Geld um slot zu spielen. Tippe '.bank' ein."
            return
        }
        variable slots
        set wheel1 [lindex $slots [rand [llength $slots]]]
        set wheel2 [lindex $slots [rand [llength $slots]]]
        set wheel3 [lindex $slots [rand [llength $slots]]]
        if {$wheel1 == "Nix" && $wheel2 == "Nix" && $wheel3 == "Nix"} {
            set payouts "0"
        }
        if {$wheel1 == "Glocke" && $wheel2 == "Glocke" || $wheel2 == "Glocke" && $wheel3 == "Glocke" || $wheel1 == "Glocke" && $wheel3 == "Glocke"} {
            set payouts "30"
        }
        if {$wheel1 == "Banane" && $wheel2 == "Banane" || $wheel2 == "Banane" && $wheel3 == "Banane" || $wheel1 == "Banane" && $wheel3 == "Banane"} {
            set payouts "30"
        }
        if {$wheel1 == "Kirsche" && $wheel2 == "Kirsche" || $wheel2 == "Kirsche" && $wheel3 == "Kirsche" || $wheel1 == "Kirsche" && $wheel3 == "Kirsche"} {
            set payouts "30"
        }
        if {$wheel1 == "Orange" && $wheel2 == "Orange" || $wheel2 == "Orange" && $wheel3 == "Orange" || $wheel1 == "Orange" && $wheel3 == "Orange"} {
            set payouts "30"
        }
        if {$wheel1 == "Zitrone" && $wheel2 == "Zitrone" || $wheel2 == "Zitrone" && $wheel3 == "Zitrone" || $wheel1 == "Zitrone" && $wheel3 == "Zitrone"} {
            set payouts "30"
        }
        if {$wheel1 == "Limette" && $wheel2 == "Limette" || $wheel2 == "Limette" && $wheel3 == "Limette" || $wheel1 == "Limette" && $wheel3 == "Limette"} {
            set payouts "30"
        }
        if {$wheel1 == "Apfel" && $wheel2 == "Apfel" || $wheel2 == "Apfel" && $wheel3 == "Apfel" || $wheel1 == "Apfel" && $wheel3 == "Apfel"} {
            set payouts "30"
        }
        if {$wheel1 == "Herz" && $wheel2 == "Herz" || $wheel2 == "Herz" && $wheel3 == "Herz" || $wheel1 == "Herz" && $wheel3 == "Herz"} {
            set payouts "40"
        }
        if {$wheel1 == "$wheel2" && $wheel2 == "$wheel3"} {
            set payouts "50"
        }
        if {$wheel1 == "Bar" && $wheel2 == "Bar" || $wheel2 == "Bar" && $wheel3 == "Bar" || $wheel3 == "Bar" && $wheel1 == "Bar" } {
            set payouts "60"
        }
        if {$wheel1 == "Bar" && $wheel2 == "Bar" && $wheel3 == "Bar"} {
            set payouts "70"
        }
        if {$wheel1 == "7" && $wheel2 == "7" || $wheel2 == "7" && $wheel3 == "7" || $wheel3 == "7" && $wheel1 == "7" } {
            set payouts "80"
        }
        if {$wheel1 == "7" && $wheel2 == "7" && $wheel3 == "7"} {
            set payouts "90"
        }
        if {$wheel1 == "Jackpot" && $wheel2 == "Jackpot" || $wheel2 == "Jackpot" && $wheel3 == "Jackpot" || $wheel1 == "Jackpot" && $wheel3 == "Jackpot"} {
            set payouts "100"
        }
        if {$wheel1 == "Jackpot" && $wheel2 == "Jackpot" && $wheel3 == "Jackpot"} {
            set payouts "Jackpot"
            set wintime [unixtime]
            set winchan "$chan"
        }
        ::mysql::sel $mysqlhandle "UPDATE `slot_stats` SET played = '[expr $stats + 1]' WHERE `channel` = '[::mysql::escape $chan]'"
        set jackpot [join [::mysql::sel $mysqlhandle "SELECT `money` FROM `slot_jackpot`" -flatlist]]
        set won [join [::mysql::sel $mysqlhandle "SELECT `won` FROM `slot_accounts` WHERE `hostname` = '[::mysql::escape $hostname]'" -flatlist]]
        set loss [join [::mysql::sel $mysqlhandle "SELECT `lose` FROM `slot_accounts` WHERE `hostname` = '[::mysql::escape $hostname]'" -flatlist]]
        set waittime($hostname) "1"
        if {[info exists payouts] && $payouts == "Jackpot"} {
            set won [join [::mysql::sel $mysqlhandle "SELECT `won` FROM `slot_accounts` WHERE `hostname` = '[::mysql::escape $hostname]'" -flatlist]]
            set totalmoney [expr {$money + $jackpot}]
            putserv "privmsg $chan :[Slot\][ \002$nick:\002 ($wheel1) - ($wheel2) - ($wheel3) \][ \037Dein Geld:\037 $[commify $totalmoney] \][ \037Gewinn:\037 \002Jackpot\002 \][ \037Jackpot:\037 \$[commify $jackpot] \][Slot\]"
            ::mysql::sel $mysqlhandle "UPDATE `slot_accounts` SET money = '$totalmoney', won = '[expr $won + 1]' WHERE `hostname` = '[::mysql::escape $hostname]'"
            ::mysql::sel $mysqlhandle "UPDATE `slot_jackpot` SET lastwon = '[::mysql::escape $nick]', money = '0', time = '$wintime', channel = '$winchan' WHERE `money` = '$jackpot'"
        } elseif {[info exists payouts]} {
            set won [join [::mysql::sel $mysqlhandle "SELECT `won` FROM `slot_accounts` WHERE `hostname` = '[::mysql::escape $hostname]'" -flatlist]]
            set totalmoney [expr {$money + $payouts}]
            putserv "privmsg $chan :[Slot\][ \002$nick:\002 ($wheel1) - ($wheel2) - ($wheel3) \][ \037Dein Geld:\037 $[commify $totalmoney] \][ \037Gewinn:\037 $$payouts \][ \037Jackpot:\037 \$[commify $jackpot] \][Slot\]"
            ::mysql::sel $mysqlhandle "UPDATE `slot_accounts` SET money = '$totalmoney', won = '[expr $won + 1]' WHERE `hostname` = '[::mysql::escape $hostname]'"
        } else {
            set loss [join [::mysql::sel $mysqlhandle "SELECT `lose` FROM `slot_accounts` WHERE `hostname` = '[::mysql::escape $hostname]'" -flatlist]]
            set lossmoney "15"
            set totalmoney [expr {$money - $lossmoney}]
            set totaljackpot [expr {$jackpot + $lossmoney}]
            putserv "privmsg $chan :[Slot\][ \002$nick:\002 ($wheel1) - ($wheel2) - ($wheel3) \][ \037Dein Geld:\037 $[commify $totalmoney] \][ \037Verlust:\037 $$lossmoney \][ \037Jackpot:\037 $[commify $totaljackpot] \][Slot\]"
            ::mysql::sel $mysqlhandle "UPDATE `slot_accounts` SET money = '$totalmoney', lose = '[expr $loss + 1]' WHERE `hostname` = '[::mysql::escape $hostname]'"
            ::mysql::sel $mysqlhandle "UPDATE `slot_jackpot` SET money = '$totaljackpot' WHERE `money` = '$jackpot'"
        }
        utimer 15 [list unset ::waittime($hostname)]
    }

    ## Jackpot

    proc jackpot {nick host hand chan text} {
        global mysqlhandle
        if {![connected]} { putserv "privmsg $chan :\002(\017Slot\002)\017 Keine Verbindung zur Datenbank. Bitte wende dich an einen Admin."; return }
        set hostname "*!$host"
        if {[isadded $hostname] == "0"} {
            putserv "NOTICE $nick :\002(\017Slot\002)\017 Tut mir leid $nick, aber du musst dich erst anmelden."
            return
        }
        set jackpot [join [::mysql::sel $mysqlhandle "SELECT money,lastwon,time,channel FROM `slot_jackpot`" -flatlist]]
        if {[join [lindex [split $jackpot] 1]] == "None"} {
            putserv "privmsg $chan :[Slot\][ Jackpot: $[commify [join [lindex [split $jackpot] 0]]] \][Slot\]"
        } else {
            putserv "privmsg $chan :[Slot\][ Jackpot: $[commify [join [lindex [split $jackpot] 0]]] \][ Zuletzt gewonnen von: [join [lindex [split $jackpot] 1]] \][ am: [clock format [join [lindex [split $jackpot] 2]] -format "%d.%m.%y %H:%M:%S"] \][ in: [join [lindex [split $jackpot] 3]] \][Slot\]"
        }
    }

    ## Top5 Players

    proc top5players {nick host hand chan text} {
        global mysqlhandle
        if {![connected]} { putserv "privmsg $chan :\002(\017Slot\002)\017 Keine Verbindung zur Datenbank. Bitte wende dich an einen Admin."; return }
        set hostname "*!$host"
        if {[isadded $hostname] == "0"} {
            putserv "NOTICE $nick :\002(\017Slot\002)\017 Tut mir leid $nick, aber du musst dich erst anmelden."
            return
        }
        set top5players {}
        set top5list {}
        set top5players [::mysql::sel $mysqlhandle "SELECT player,money FROM `slot_accounts` ORDER BY `money` DESC" -list]
        if {$top5players == ""} {
            putserv "privmsg $chan :[Slot\][ Keiner bisher. \][Slot\]"
            return
        } else {
            for {set place 1} {$place <= 5 && $place <= [llength $top5players]} {incr place} {
                lappend top5list "#$place. \037[lindex $top5players [expr {$place-1}] 0]\037 (\002[commify [lindex $top5players [expr {$place-1}] 1]]\002)"
            }
            putserv "privmsg $chan :[Slot\][ [join $top5list] \][Slot\]"
        }
    }

    ## Player Mymoney

    proc mymoney {nick host hand chan text} {
        global mysqlhandle
        if {![connected]} { putserv "privmsg $chan :\002(\017Slot\002)\017 Keine Verbindung zur Datenbank. Bitte wende dich an einen Admin."; return }
        set hostname "*!$host"
        if {[isadded $hostname] == "0"} {
            putserv "NOTICE $nick :\002(\017Slot\002)\017 Tut mir leid $nick, aber du musst dich erst anmelden."
            return
        }
        set money [join [::mysql::sel $mysqlhandle "SELECT money FROM `slot_accounts` WHERE `hostname` = '[::mysql::escape $hostname]'" -flatlist]]
        putserv "privmsg $chan :[Slot\][ \002$nick:\002 Deine Knete: $[commify $money] \][Slot\]"
    }

    ## Player Myrank

    proc myrank {nick host hand chan text} {
        global mysqlhandle
        if {![connected]} { putserv "privmsg $chan :\002(\017Slot\002)\017 Keine Verbindung zur Datenbank. Bitte wende dich an einen Admin."; return }
        set hostname "*!$host"
        if {[isadded $hostname] == "0"} {
            putserv "NOTICE $nick :\002(\017Slot\002)\017 Tut mir leid $nick, aber du musst dich erst anmelden."
            return
        }
        set listplayers [::mysql::sel $mysqlhandle "SELECT player FROM `slot_accounts`" -flatlist]
        set rankplayers [::mysql::sel $mysqlhandle "SELECT hostname FROM `slot_accounts` ORDER BY `money` DESC" -flatlist]
        set rankplace "[lsearch -exact $rankplayers $hostname]"
        incr rankplace
        putserv "privmsg $chan :[Slot\][ \002$nick:\002 Du stehst auf platz: $rankplace \][Slot\]"
    }

    ## Player Mystats

    proc mystats {nick host hand chan text} {
        global mysqlhandle
        if {![connected]} { putserv "privmsg $chan :\002(\017Slot\002)\017 Keine Verbindung zur Datenbank. Bitte wende dich an einen Admin."; return }
        set hostname "*!$host"
        if {[isadded $hostname] == "0"} {
            putserv "NOTICE $nick :\002(\017Slot\002)\017 Tut mir leid $nick, aber du musst dich erst anmelden."
            return
        }
        set info [join [::mysql::sel $mysqlhandle "SELECT won,lose FROM `slot_accounts` WHERE `hostname` = '[::mysql::escape $hostname]'" -flatlist]]
        putserv "privmsg $chan :[Slot\][ \002$nick:\002 Gesamt: [expr [lindex [split $info] 0]+[lindex [split $info] 1]] \][ Gewonnen: [lindex [split $info] 0] \][ Verloren: [lindex [split $info] 1] \][Slot\]"
    }

    ## Player Bank

    proc bank {nick host hand chan text} {
        global mysqlhandle
        if {![connected]} { putserv "privmsg $chan :\002(\017Slot\002)\017 Keine Verbindung zur Datenbank. Bitte wende dich an einen Admin."; return }
        set hostname "*!$host"
        if {[isadded $hostname] == "0"} {
            putserv "NOTICE $nick :\002(\017Slot\002)\017 Tut mir leid $nick, aber du musst dich erst anmelden."
            return
        }
        if {[join [::mysql::sel $mysqlhandle "SELECT `money` FROM `slot_accounts` WHERE `hostname` = '[::mysql::escape $hostname]'" -flatlist]] > 30} {
            putserv "NOTICE $nick :\002(\017Slot\002)\017 Du hast noch genug Geld. Du brauchst noch kein neues."
        } else {
            set money [join [::mysql::sel $mysqlhandle "SELECT `money` FROM `slot_accounts` WHERE `hostname` = '[::mysql::escape $hostname]'" -flatlist]]
            ::mysql::sel $mysqlhandle "UPDATE `slot_accounts` SET money = '[expr $money + 100]' WHERE `hostname` = '[::mysql::escape $hostname]'"
            putserv "privmsg $chan :[Slot\][ \002$nick:\002 Tada! Du hast nun neues Geld und kannst wieder spielen! Viel Spaß. \][Slot\]"
        }
    }

    ## Channel Stats

    proc stats {nick host hand chan text} {
        global mysqlhandle
        if {![connected]} { putserv "privmsg $chan :\002(\017Slot\002)\017 Keine Verbindung zur Datenbank. Bitte wende dich an einen Admin."; return }
        set hostname "*!$host"
        if {[isadded $hostname] == "0"} {
            putserv "NOTICE $nick :\002(\017Slot\002)\017 Tut mir leid $nick, aber du musst dich erst anmelden."
            return
        }
        set played [join [::mysql::sel $mysqlhandle "SELECT `played` FROM `slot_stats` WHERE `channel` = '[::mysql::escape $chan]'" -flatlist]]
        if {$played == ""} {
            putserv "privmsg $chan :[Slot\][ Zur Zeit gibt es keine channel stats. \][Slot\]"
        } else {
            putserv "privmsg $chan :[Slot\][ Es wurde schon '\002$played\002' mal gespielt in $chan \][Slot\]"
        }
    }

    ## Show Version

    proc showversion {nick host hand chan text} {
        variable version
        variable author
        putserv "privmsg $chan :[Slot\][ Aktuelle Version: $version \][ $author \][Slot\]"
    }

    ## Is Player Added

    proc isadded {hostname} {
        if {[::mysql::sel $::mysqlhandle "SELECT `player` FROM `slot_accounts` WHERE `hostname` = '[::mysql::escape $hostname]'" -flatlist] == ""} {
            return 0
        } else {
            return 1
        }
    }

    ## Valid Player Nick

    proc validplayer {nick} {
        set valid "0"
        foreach playersnames [::mysql::sel $::mysqlhandle "SELECT `player` FROM `slot_accounts`" -flatlist] {
            if {[string equal -nocase $nick "$playersnames"]} { set valid  "1" } }
        if {$valid != "0"} {
            return 1
        } else {
            return 0
        }
    }

    ## Checking if connected to database

    proc connected {} {
        global mysqlhandle
        if {![info exists mysqlhandle] || [::mysql::state $mysqlhandle] == "NOT_A_HANDLE" || [::mysql::ping $mysqlhandle] == "0"} {
            return 0
        } else {
            return 1
        }
    }

    ## Commify Money

    proc commify money {regsub -all {\d(?=(\d{3})+($|\.))} $money {\0,}}


}

putlog "Slot Machine $slot_de::version Is Scripted By 'Björn 'Turbo' B.'"
This post was edited 6 times, last on 03-03-2013, 18:35 by Turbo
Topic: [EGG/TCL] Example on how to work with mysql (various libraries used)   Forum: #tclcode snippets      Goto post
Turbo  07-29-2007, 18:30 | mysqlmod (+ eggdrop) MSN  Homepage
Moderator
Member since 06/2007
5 Posts
Location: Netherlands
[Eggdrop-TCL]
This works with mysqlmod (http://barkerjr.net/irc/eggdrop/modules/)
                                                                                                                              
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
namespace eval database {

    # -- Mysql Info

    variable dbhost "127.0.0.1"
    variable dbuser "root"
    variable dbpass "secretpass"
    variable dbname "userlist"
    variable dbconnect ""

    # Leave dbconnect empty!

    # --- Module

    loadmodule mysql

    bind evnt - "init-server" [namespace current]::dbconnect

}

Connecting to the database
                                                                                                                              
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
proc database::dbconnect init-server {
    putlog "Connecting to MySQL Server:"
    if {[catch { set database::dbconnect [mysql_connect $database::dbname $database::dbhost $database::dbuser $database::dbpass] } error]} {
        putlog "MySQL Error:"
        foreach x [split $error \n] {
            putlog "$x"
        }
        putlog "End of error."
    } else {
        putlog "Connected to MySQL."
        putlog "Database: $database::dbname"
        mysql_query "CREATE TABLE IF NOT EXISTS `userlist` ( `nickname` varchar(100) NOT NULL, `hostname` varchar(100) NOT NULL )"

    }
}

Check if user is present in the database
                                                                                                                              
1
2
3
4
5
6
7
8
proc isadded {nick} {
    set nick_ident *!*@[lindex [split [getchanhost $nick] "@"] 1]
    if {[mysql_query "SELECT `nickname` FROM `userlist` WHERE `hostname` = '[mysql_escape $nick_ident]'"] == ""} {
        return "0"
    } else {
        return "1"
    }
}

returns 0 if the user was not added
returns 1 if the user was successfully added.



Insert user into the table
                                                                                                                              
1
2
3
4
5
6
7
8
9
proc addnick {nick} {
    set nick_ident *!*@[lindex [split [getchanhost $nick] "@"] 1]
    if {[isadded $nick] == "1"} {
        return "allready added"
    } else {
        mysql_query "INSERT INTO `userlist` ( nickname , hostname  ) VALUES ( '[mysql_escape $nick]' , '[mysql_escape $nick_ident]'  )"
        return "added you in the tabel"
    }
}

Getting hostname from nickname, who is added in the tabel. returns 0 if he is not added
                                                                                                                              
1
2
3
4
5
6
7
8
proc hostname {nick} {
    set nick_ident *!*@[lindex [split [getchanhost $nick] "@"] 1]
    if {[set hostname1 [mysql_query "SELECT `hostname` FROM `userlist` WHERE `hostname` = '[mysql_escape $nick_ident]'"]] == ""} {
        return "0"
    } else {
        return "[join $hostname1]"
    }
}

Delete a user from a table
                                                                                                                              
1
2
3
4
5
6
7
8
9
proc delnick {nick} {
    set nick_ident *!*@[lindex [split [getchanhost $nick] "@"] 1]
    if {[isadded $nick] == "0"} {
        return "didn`t found $nick in the database"
    } else {
        mysql_query "DELETE FROM `userlist` WHERE `nickname` = '[mysql_escape $nick]' AND `hostname` = '[mysql_escape $nick_ident]';"
        return "Deleted you from database"
    }
}

now you see how to connect to a database & add/del/check from a table and creating a table
This post was edited 10 times, last on 06-04-2008, 01:14 by Turbo
Topic: [EGG] request   Forum: workscript announcements      Goto post
Turbo  06-18-2007, 00:02 MSN  Homepage
Moderator
Member since 06/2007
5 Posts
Location: Netherlands
  1. Author: Björn 'Turbo' B. 
  2.                  
  3. Copyright © 2009 Turbo-Dev.org - All Rights Reserved.
#
  1. Version/Date check list.
#    1.0 - June 18 2007
#    1.1 - December 22 2009
#    1.2 - Januari 21 2010

namespace eval ::request_system {

    # - Config

    ## Set here the trigger you wanna use.
    variable reqtrigger "."

    ## Please leave this.
    variable reqversion "1.2"

    ## Set here the channel (where users can use the request command).
    variable reqchannel "#choose-your-channel"

    ## Set here the channel where all msgs report goes to.
    variable reportchannel "#choose-your-channel"

    ## Set here the require users that a requested channel needs.
    variable require "25"

    # - File Check

    if {![file exists request]} { file mkdir request }

    # - Binds

    bind pub -|- ${reqtrigger}request [namespace current]::start
    bind pub m|m ${reqtrigger}suspend [namespace current]::suspend
    bind pub m|m ${reqtrigger}unsuspend [namespace current]::unsuspend
    bind pub m|m ${reqtrigger}info [namespace current]::suspended
    bind pub m|m ${reqtrigger}remove [namespace current]::removebot
    bind need -|- * [namespace current]::need:check

    # - End Of Bindings

    # - End Of Config

    ## Don`t touch anything below this (only if you know what you are doing)


    proc start {nick host hand chan text} {
        global request
        variable reqchannel
        variable reportchannel
        variable require
        set requestchannel [lindex [split $text] 0]
        if {$reqchannel == "" || $reportchannel == "" || $require == ""} {
            putlog "(Request) Please check your Configuration."
            return 0
        } elseif {![string equal -nocase $chan "$reqchannel"]} {
            putserv "PRIVMSG $chan :(Request) You can only request on channel: $reqchannel"
            return 0
        } elseif {[info exists request(proceeding)]} {
            putserv "PRIVMSG $chan :(Request) There is already a request proceeding, please wait and try again."
            return 0       
        } elseif {$requestchannel == ""} {
            putserv "PRIVMSG $chan :(Request) $::lastbind <#channel>"
            return 0
        } elseif {[string index $requestchannel 0] != "#"} {
            putserv "PRIVMSG $chan :(Request) ($requestchannel) is no such channel name."
            return 0
        } elseif {[string tolower $requestchannel] == "[string tolower $reqchannel]"} {
            putserv "PRIVMSG $chan :(Request) ($requestchannel) is no request channel."
            return 0
        } elseif {[issuspended $requestchannel] == "1"} {
            putserv "PRIVMSG $chan :(Request) ($requestchannel) has been suspended."
            return 0
        } elseif {[validchan $requestchannel] == "1"} {
            putserv "PRIVMSG $chan :(Request) Already on channel: ($requestchannel)"
            return 0
        } else {
            putquick "PRIVMSG $reportchannel :(Request) Status: Proceeding - Channel: $requestchannel - Nickname: $nick"
            channel add $requestchannel
            set request(proceeding) "1"
            set request(chan) "$requestchannel"
            set request(nick) "$nick"
            set request(required) "$require"
        }
        bind raw - 315 [namespace current]::end
        proc end {from key text} {
            global request
            variable reportchannel
            if {![info exists request(proceeding)]} { return }
            set users "[llength [chanlist $request(chan)]]"
            if {$users < "$request(required)"} {
                channel remove $request(chan)
                set request(reason) "Not enough users."
                putquick "privmsg $request(nick) :(Request) Failed: Not enough users on $request(chan) you need $request(required) users, and currently have $users"
                putquick "PRIVMSG $reportchannel :(Request) Channel: $request(chan) - Nickname: $request(nick) - Accepted: No - Reason: $request(reason)"
                unset request(reason)
            } elseif {![onchan $request(nick) $request(chan)]} {
                channel remove $request(chan)
                set request(reason) "User not channel."
                putquick "privmsg $request(nick) :(Request) Failed: Didn`t found you on $request(chan)"
                putquick "PRIVMSG $reportchannel :(Request) Channel: $request(chan) - Nickname: $request(nick) - Accepted: No - Reason: $request(reason)"
                unset request(reason)
            } elseif {![isop $request(nick) $request(chan)]} {
                channel remove $request(chan)
                set request(reason) "User not opped on channel."
                putquick "privmsg $request(nick) :(Request) Failed: You don`t have op on $request(chan)"
                putquick "PRIVMSG $reportchannel :(Request) Channel: $request(chan) - Nickname: $request(nick) - Accepted: No - Reason: $request(reason)"
                unset request(reason)
            } elseif {![onchan Q $request(chan)]} {
                channel remove $request(chan)
                set request(reason) "Q not on channel"
                putquick "privmsg $request(nick) :(Request) Failed: Didn`t found Q on $request(chan)"
                putquick "PRIVMSG $reportchannel :(Request) Channel: $request(chan) - Nickname: $request(nick) - Accepted: No - Reason: $request(reason)"
                unset request(reason)
            } else {
                putquick "PRIVMSG $request(nick) :(Request) $request(nick) Your request is accepted, to remove me from $request(chan) you can ask bot owner: $::owner"
                putquick "PRIVMSG $reportchannel :(Request) Channel: $request(chan) - Nickname: $request(nick) - Accepted: Yes"
                channel remove $request(chan)
            }
            unset request(chan)
            unset request(nick)
            unset request(required)
            unset request(proceeding)
            unbind raw - 315 [namespace current]::end
        }
    }

    proc issuspended {chan} {
        if {![file exists request/suspended.txt]} { return 0 }
        set file [open request/suspended.txt "r+"]
        set found 0
        while {![eof $file]} {
            set info [gets $file]
            if {[string equal -nocase "[lindex [split $info] 0]" $chan]} { set found "1" }
        }
        close $file
        if {$found == "1"} {
            return 1
        } else {
            return 0
        }
    }

    proc removebot {nick host hand chan text} {
        variable reqchannel
        variable reportchannel
        set removechannel [lindex [split $text] 0]
        if {[string equal -nocase $removechannel "$reqchannel"]} {
            putserv "PRIVMSG $chan :(Request) ($removechannel) not allowed."
            return 0
        } elseif {$removechannel == ""} {
            putserv "PRIVMSG $chan :(Request) $::lastbind <#channel>"
            return 0
        } elseif {[validchan $removechannel] == "0"} {
            putserv "PRIVMSG $chan :(Request) ($removechannel) is not a valid channel."
            return 0           
        } else {
            channel remove $removechannel
            putquick "PRIVMSG $reportchannel :(Request) Removed channel: $removechannel."
        }
    }

    proc need:check {chan type} {
        global request
        variable reportchannel
        if {![info exists request(proceeding)]} { return }
        if {![string equal -nocase $chan "$request(chan)"]} { return }
        switch -exact -- [string tolower [lindex [split $type] 0]] {
            "unban" {
                channel remove $request(chan)
                set request(reason) "Couldn`t check the channel because i found myself banned."
                putquick "PRIVMSG $reportchannel :(Request) Channel: $request(chan) - Nickname: $request(nick) - Accepted: No - Reason: $request(reason)"
                unset request(reason)
                unset request(chan)
                unset request(nick)
                unset request(required)
                unset request(proceeding)
            }
            "invite" {
                channel remove $request(chan)
                set request(reason) "Couldn`t check the channel because the channel is set to +i (invite only)."
                putquick "PRIVMSG $reportchannel :(Request) Channel: $request(chan) - Nickname: $request(nick) - Accepted: No - Reason: $request(reason)"
                unset request(reason)
                unset request(chan)
                unset request(nick)
                unset request(required)
                unset request(proceeding)
            }
            "limit" {
                channel remove $request(chan)
                set request(reason) "Couldn`t check the channel because the channel is set to +l (limit)."
                putquick "PRIVMSG $reportchannel :(Request) Channel: $request(chan) - Nickname: $request(nick) - Accepted: No - Reason: $request(reason)"
                unset request(reason)
                unset request(chan)
                unset request(nick)
                unset request(required)
                unset request(proceeding)
            }
            "key" {
                channel remove $request(chan)
                set request(reason) "Couldn`t check the channel because the channel is set to +k (key)."
                putquick "PRIVMSG $reportchannel :(Request) Channel: $request(chan) - Nickname: $request(nick) - Accepted: No - Reason: $request(reason)"
                unset request(reason)
                unset request(chan)
                unset request(nick)
                unset request(required)
                unset request(proceeding)
            }
        }
        unbind raw - 315 [namespace current]::end
    }

    proc suspend {nick host hand chan text} {
        if {![file exists request/suspended.txt]} { set file [open request/suspended.txt w+]; close $file }
        if {[lindex [split $text] 0] == "" || [join [lrange [split $text] 1 end]] == "" || [string index [lindex [split $text] 0] 0] != "#"} {
            putquick "PRIVMSG $chan :(Request) $::lastbind <#channel> <reason>"
        } else {
            set file [open request/suspended.txt "r+"]
            set found 0
            set suspendchan [lindex [split $text] 0]
            set suspendreason [join [lrange [split $text] 1 end]]
            while {![eof $file]} {
                set info [gets $file]
                if {[string equal -nocase "[lindex [split $info] 0]" $suspendchan]} { incr found 1 }
            }
            if {$found != "0"} {
                putquick "PRIVMSG $chan :(Request) Already suspended."
            } else {
                puts $file "$suspendchan $nick [strftime {%m/%d/%y}] $suspendreason"
                putquick "PRIVMSG $chan :(Request) Suspended."
            }
            close $file
        }
    }
    proc unsuspend {nick host hand chan text} {
        if {![file exists request/suspended.txt]} {
            putquick "PRIVMSG $chan :(Request) Couldn`t find the suspended channels list."
        } elseif {[lindex [split $text] 0] == "" || [string index [lindex [split $text] 0] 0] != "#"} {
            putquick "PRIVMSG $chan :(Request) $::lastbind <#channel>"
        } else {
            set file [open request/suspended.txt "r"]
            set found 0
            set list {}
            set suspendchan [lindex [split $text] 0]
            while {![eof $file]} {
                set info [gets $file]
                if {[eof $file]} { break }
                if {[string equal -nocase "[lindex [split $info] 0]" $suspendchan]} {
                    incr found 1
                } else {
                lappend list [join [split $info]] }
            }
            close $file
            if {$found == "0"} {
                putquick "PRIVMSG $chan :(Request) $suspendchan is not suspended."
            } else {
                set file [open request/suspended.txt "w+"]
                foreach {info} $list {
                puts $file $info }
                close $file
                putquick "PRIVMSG $chan :(Request) $suspendchan has been removed from suspended list."
            }
        }
    }       
    proc suspended {nick host hand chan text} {
        if {![file exists request/suspended.txt]} {
            putquick "PRIVMSG $chan :(Request) Couldn`t find the suspended channels list."
        } elseif {[lindex [split $text] 0] == "" || [string index [lindex [split $text] 0] 0] != "#"} {
            putquick "PRIVMSG $chan :(Request) $::lastbind <#channel>"
        } else {
            set file [open request/suspended.txt r]
            set buffer [split [read $file] \n]
            set suspendchan [lindex [split $text] 0]
            foreach info $buffer {
                if {[string equal -nocase "[lindex [split $info] 0]" $suspendchan]} {
                    putquick "PRIVMSG $chan :(Request) \'[lindex [split $info] 0]\' has been suspended by ([lindex [split $info] 1]) on [lindex [split $info] 2] with reason: [join [lrange [split $info] 3 end]]"
                    return
                }
            }
            putquick "PRIVMSG $chan :(Request) No info found about $suspendchan, not suspended."
            close $file
        }
    }

}

putlog "Request System ${request_system::reqversion} Is Scripted By 'Björn 'Turbo' B.'"
This post was edited 1 times, last on 02-02-2010, 01:16 by Turbo
Topic: [EGG] Safe load script (errors in script wont crash the bot)   Forum: #tclcode snippets      Goto post
Turbo  06-17-2007, 15:31 | Load script MSN  Homepage
Moderator
Member since 06/2007
5 Posts
Location: Netherlands
                                                                                                                              
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
set lflags "n"
set lscriptsdir "scripts"
set ltrigger "!"

bind pub $lflags ${ltrigger}load load:file


proc load:file {nick host hand chan text} {
    global lscriptsdir  ltrigger
    set file [lindex [split $text] 0]
    if {$file == ""} {
        putserv "PRIVMSG $nick :-(Load)- ${ltrigger}load <scriptname.tcl> -(Info)-"
        return 0
    } elseif {![file exists [file join $lscriptsdir $file]]} {
        putserv "PRIVMSG $nick :-(Load)- Sorry $file doesn`t exists -(Info)-"
        return 0
    } else {
        set kbsize [expr {[file size [file join $lscriptsdir/$file]] / 1024.0}]
        if {[catch {uplevel "source [file join $lscriptsdir $file]"} error]} {
            putserv "PRIVMSG $nick :-(Load)- Script: $file Size: $kbsize Kb Status: Error -(Info)-"
            putserv "PRIVMSG $nick :-(Load)- $error -(Info)-"
        } else {
            putserv "PRIVMSG $nick :-(Load)- Script: $file - Size: $kbsize Kb - Status: OK -(Info)-"
        }
    }
}


You need to set the lscriptsdir to your own dir
You can also change the lflags to whatever you want

Example: your trigger is "!"

!load tcldebug.tcl

when no error found he load the script and rehash

when error found he gives you the error and he don`t load

when he load the scripts and no error found it will be like

Script: tcldebug.tcl - Size: 3 kb - Status: No Error
This post was edited 7 times, last on 08-24-2007, 03:54 by Turbo
Go to forum
Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen | Time: 256.2 msec, CPU time: 130.9 msec, 46 Database queries in 153.6 msec | 13.6 kB (104.4 kB) | Timezone: +0200