Add a separate dispatcher for PRIVMSG

We're going to use that one a lot more than the others…
configurable-file-paths
Robert Gerus 2015-11-09 01:46:08 +01:00
parent fb8e2d8392
commit e816e72743
2 changed files with 34 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package bot
import (
"log"
"strings"
"github.com/arachnist/gorepost/irc"
)
@ -16,11 +17,27 @@ func RemoveCallback(command string) {
delete(Callbacks, command)
}
var MSGCallbacks = make(map[string]func(chan irc.Message, irc.Message))
func AddMSGCallback(command string, callback func(chan irc.Message, irc.Message)) {
MSGCallbacks[command] = callback
}
func RemoveMSGCallback(command string) {
delete(MSGCallbacks, command)
}
func Dispatcher(quit chan struct{}, output chan irc.Message, input chan irc.Message) {
log.Println("spawned Dispatcher")
for {
select {
case msg := <-input:
if msg.Command == "PRIVMSG" {
cmd := strings.Split(msg.Trailing, " ")[0]
if MSGCallbacks[cmd] != nil {
go MSGCallbacks[cmd](output, msg)
}
}
if Callbacks[msg.Command] != nil {
go Callbacks[msg.Command](output, msg)
}

17
bot/msgping.go Normal file
View File

@ -0,0 +1,17 @@
package bot
import (
"github.com/arachnist/gorepost/irc"
)
func papiez(output chan irc.Message, msg irc.Message) {
output <- irc.Message{
Command: "PRIVMSG",
Params: []string{msg.Prefix.Name},
Trailing: "pingity pong",
}
}
func init() {
AddMSGCallback(":ping", papiez)
}