Add a command system

This commit is contained in:
raylu 2010-10-13 21:16:16 -04:00
parent f212894aed
commit 3e83d30e71
4 changed files with 51 additions and 1 deletions

View File

@ -7,6 +7,7 @@ include $(GOROOT)/src/Make.inc
TARG=rbot
GOFILES=\
rbot.go\
handler.go\
include $(GOROOT)/src/Make.cmd

44
handler.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"irc"
//"fmt"
"strings"
)
var commands = map [string]func(*irc.Conn, string, string, string) {
"kill": kill,
}
func handlePrivmsg(conn *irc.Conn, line *irc.Line) {
target := line.Args[0]
if target[0] == '#' || target[0] == '&' {
// message to a channel
command(conn, line.Nick, line.Text, target)
} else if target == nick {
// message to us
command(conn, line.Nick, line.Text, target)
}
}
func command(conn *irc.Conn, nick, text, target string) {
if text[:len(trigger)] != trigger {
return
}
split := strings.Split(text, " ", 2)
if len(split[0]) < 2 {
return
}
handler := commands[split[0][1:]]
if handler != nil {
if len(split) > 1 {
handler(conn, nick, split[1], target)
} else {
handler(conn, nick, "", target)
}
}
}
func kill(conn *irc.Conn, nick, args, target string) {
conn.Action(target, "dies.")
}

View File

@ -3,5 +3,6 @@ server: irc.rizon.net
nick: rbot
user: rbot
ssl: false
trigger: !
[#raylu]

View File

@ -7,7 +7,7 @@ import (
"github.com/kless/goconfig/config"
)
var nick, server, user string
var nick, server, user, trigger string
var ssl bool
var channels []string
@ -22,6 +22,7 @@ func main() {
conn.Join(c)
}
})
c.AddHandler("privmsg", handlePrivmsg)
for {
fmt.Printf("Connecting to %s...\n", server)
@ -62,4 +63,7 @@ func parseConfig(confFile string) {
i++
}
}
trigger, err = conf.String("DEFAULT", "trigger")
if err != nil { fmt.Printf("Config error: %s\n", err); os.Exit(1) }
}