mirror of https://github.com/velour/catbase.git
bot: update callback registry types
Unfortunately, we can't hash the plugins. I went ahead and hashed the types instead.
This commit is contained in:
parent
5ee5f33e36
commit
f661d7dca2
12
bot/bot.go
12
bot/bot.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
@ -249,11 +250,12 @@ func (b *bot) RegisterFilter(name string, f func(string) string) {
|
||||||
|
|
||||||
// Register a callback
|
// Register a callback
|
||||||
func (b *bot) Register(p Plugin, kind Kind, cb Callback) {
|
func (b *bot) Register(p Plugin, kind Kind, cb Callback) {
|
||||||
if _, ok := b.callbacks[p]; !ok {
|
t := reflect.TypeOf(p)
|
||||||
b.callbacks[p] = make(map[Kind][]Callback)
|
if _, ok := b.callbacks[t]; !ok {
|
||||||
|
b.callbacks[t] = make(map[Kind][]Callback)
|
||||||
}
|
}
|
||||||
if _, ok := b.callbacks[p][kind]; !ok {
|
if _, ok := b.callbacks[t][kind]; !ok {
|
||||||
b.callbacks[p][kind] = []Callback{}
|
b.callbacks[t][kind] = []Callback{}
|
||||||
}
|
}
|
||||||
b.callbacks[p][kind] = append(b.callbacks[p][kind], cb)
|
b.callbacks[t][kind] = append(b.callbacks[t][kind], cb)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -39,7 +40,8 @@ RET:
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *bot) runCallback(plugin Plugin, evt Kind, message msg.Message, args ...interface{}) bool {
|
func (b *bot) runCallback(plugin Plugin, evt Kind, message msg.Message, args ...interface{}) bool {
|
||||||
for _, cb := range b.callbacks[plugin][evt] {
|
t := reflect.TypeOf(plugin)
|
||||||
|
for _, cb := range b.callbacks[t][evt] {
|
||||||
if cb(evt, message, args) {
|
if cb(evt, message, args) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
package bot
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"github.com/velour/catbase/bot/msg"
|
"github.com/velour/catbase/bot/msg"
|
||||||
"github.com/velour/catbase/bot/user"
|
"github.com/velour/catbase/bot/user"
|
||||||
|
@ -32,7 +34,7 @@ const (
|
||||||
|
|
||||||
type Kind int
|
type Kind int
|
||||||
type Callback func(Kind, msg.Message, ...interface{}) bool
|
type Callback func(Kind, msg.Message, ...interface{}) bool
|
||||||
type CallbackMap map[Plugin]map[Kind][]Callback
|
type CallbackMap map[reflect.Type]map[Kind][]Callback
|
||||||
|
|
||||||
// Bot interface serves to allow mocking of the actual bot
|
// Bot interface serves to allow mocking of the actual bot
|
||||||
type Bot interface {
|
type Bot interface {
|
||||||
|
|
Loading…
Reference in New Issue