Version 11 of tcl-dicebot

Updated 2007-02-02 12:03:14

so that debugging/checking can be done easily, I will provide a working sample of what

 gets $::fd line ;#-- read a line from socket

would have in it when I want this bot to trigger its actions.

 :[email protected] PRIVMSG #help :1d20 this is the message portion of the raw data

heres the IRC bot.

 set ::server irc.sorcery.net
 set ::chan   #omen-heart
 set ::me     "dicebot_[expr round([expr rand() * 10000])]"

 proc random {sides} {
        ##############################################
        #generates random numbers given 'sides' sided#
        #dice              works                     #         
        # Sides=number of sides to a dice            #
        ##############################################
        ##############################################
        return [expr round([expr rand() * $sides])]
 }


 proc roller {name number sides} {
 #name=person who called the function (get from irc)
 #number=Number of times to roll the dice
        =Number of times to roll the dice
        #########################################################
        #calls random proc 'number times and returns the results#
        #########################################################

        set incriment 0
        set rolled "$name rolled"
        set total 0
        while {$incriment != $number} {
                append rolled " "
                set temp [random $sides]
                append rolled $temp
                incr incriment
                set total [expr $temp+$total]           #########
        }
        append rolled " total= $total"
        return $rolled
 }

 proc matcher {input} {
        ######################################################################################################
        #proc searches for a string matching (1d20, ect) and returns it if it exists, otherwise, it returns 0#
        #                                       works!                                                       #
        #         rolls using this dicebot must have the rolling info seperated by a comma                   #
        ######################################################################################################
         if {[regexp {[0-9]*[dD][0-9]+} $input]} {
 #searches for patterns matching (number)d(or)D(number), if found the if resolves as true
                split $input " "
 #plits rolls at the spaces
                return [set input [lindex $input 0]]
        }
 return 0
 }

 #proc roll_formater {input} {
        #set input [string tolower $input]
        #set input [split $input ""]
        #set size [llength $input]
        #set d_point [lsearch -exact $input "d"]
        #set control 0
        #set number ""
        #set sides ""
        #while {$control <= $d_point} {
                #lappend number [lindex $input $control] 
                #incr control
        #}
        #if {$control == $d_point} {
                #incr control
        #}
        #while {$control <= $size} {
                #lappend sides [lindex $input $control] 
                #incr control
        #}

 #set number [lreplace $number [expr [llength $number] - 1] [expr [llength $number] - 1] "" ]
 #set number [join $number]
 #set sides [join $sides]
 #set result [list $number $sides]
 #return $result
 #}

 #}

 ###########################################################################
 #preceding function splits the output of matcher (x*d/Dx* into a list     #
 #that contains only the two rolling elements (number sides)               #
 ###########################################################################


 #############################
 #############################
 ##Start IRC client software##
 #############################
 #############################


 proc roll_formater {input} {
         set input [string tolower $input]
         set input [split $input d]
         set number [lindex $input 0]
         set sides [lindex $input 1]
         return "$number $sides"
 }

 proc recv {} {
         gets $::fd line ;#-- read a line from socket

        if {[lindex [split $line] 0] eq "PING"} {
                send "PONG [lindex [split $line] 1]" 
                return 
                #ping/pong relationship with server
        }

        if {[regexp {:([^!]*)![^ ].* +PRIVMSG ([^ :]+) +:(.*)} $line} {
                set temp [split $line :]
                set message [split [lindex $temp 2] " "]
                set name_long [lindex $temp 1]
                set name_short [split $name_long !] ; set name_short [lindex $name_short 0]
                set room [split $line #] ; set room [lindex $room 1] ; set room [split $room " "] ; set room "#[lindex $room 0]"
                set room [split $line #] ; set room [lindex $room 1] ; set room [split $room " "] ; set room "#[lindex $room 0]"
                #splits the text recieved in $line into a few parts, the most important bieng the roll that was called
                #and the message that called it (store in $message ex: 1d20 this is a roll)
                #and the message that called it (store in $message ex: 1d20 this is a roll)
                if {[regexp {[0-9]*[dD][0-9]+} $message]} {
                                #call the dicebot when $message starts with a dicebot call
                                set roll [matcher $message]
                                set roll [roll_formater $roll]
                                set roll [roller $name_short [lindex $roll 0] [lindex $roll 1]]
                                send "PRIVMSG $::chan :$roll"
                                }
        }
 }

 proc send {str} {
        #######################################################################
        # A very simple wrapper for sending a string, not forgetting to flush #
        # is called from within post procedure                                #
        #######################################################################
        puts $::fd $str
        flush $::fd
 }

 # And now, the action begins with "logging in" to the IRC server

 set ::fd [socket $::server 6667]
 send "NICK $::me"
 send "USER $::me 0 * :PicoIRC user"
 after 500
 send "JOIN $::chan" 

 # If the socket gets readable, recv will be called

 fileevent $::fd readable recv

davou I've edited the function roll_formater down so that it's much shorter... I've gotten the bot to connect to IRC, and join the channel mentioned in $::chan, but it leaves just as fast as it gets in... does anyone know why it's doing this? - RS: Easiest way to find out is to add puts $line right after the gets $::fp line and see what comes in.


See also irc


Category Internet