mirror of https://github.com/velour/catbase.git
bot: switch plugins to matching instance
instead of string
This commit is contained in:
parent
82dcf410f2
commit
933e514ddd
13
bot/bot.go
13
bot/bot.go
|
@ -248,13 +248,12 @@ func (b *bot) RegisterFilter(name string, f func(string) string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register a callback
|
// Register a callback
|
||||||
func (b *bot) Register(name string, kind Kind, cb Callback) {
|
func (b *bot) Register(p Plugin, kind Kind, cb Callback) {
|
||||||
name = strings.ToLower(name)
|
if _, ok := b.callbacks[p]; !ok {
|
||||||
if _, ok := b.callbacks[name]; !ok {
|
b.callbacks[p] = make(map[Kind][]Callback)
|
||||||
b.callbacks[name] = make(map[Kind][]Callback)
|
|
||||||
}
|
}
|
||||||
if _, ok := b.callbacks[name][kind]; !ok {
|
if _, ok := b.callbacks[p][kind]; !ok {
|
||||||
b.callbacks[name][kind] = []Callback{}
|
b.callbacks[p][kind] = []Callback{}
|
||||||
}
|
}
|
||||||
b.callbacks[name][kind] = append(b.callbacks[name][kind], cb)
|
b.callbacks[p][kind] = append(b.callbacks[p][kind], cb)
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ func (b *bot) Receive(kind Kind, msg msg.Message, args ...interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, name := range b.pluginOrdering {
|
for _, name := range b.pluginOrdering {
|
||||||
if b.runCallback(name, kind, msg, args) {
|
if b.runCallback(b.plugins[name], kind, msg, args) {
|
||||||
goto RET
|
goto RET
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ RET:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *bot) runCallback(plugin string, 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] {
|
for _, cb := range b.callbacks[plugin][evt] {
|
||||||
if cb(evt, message, args) {
|
if cb(evt, message, args) {
|
||||||
return true
|
return true
|
||||||
|
@ -75,10 +75,9 @@ func (b *bot) checkHelp(channel string, parts []string) {
|
||||||
b.listVars(channel, parts)
|
b.listVars(channel, parts)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
plugin := b.plugins[parts[1]]
|
plugin, ok := b.plugins[parts[1]]
|
||||||
if plugin != nil {
|
if ok {
|
||||||
// TODO: Maybe broke
|
b.runCallback(plugin, Help, msg.Message{Channel: channel}, channel, parts)
|
||||||
b.runCallback(parts[1], Help, msg.Message{Channel: channel}, channel, parts)
|
|
||||||
} else {
|
} else {
|
||||||
msg := fmt.Sprintf("I'm sorry, I don't know what %s is!", parts[1])
|
msg := fmt.Sprintf("I'm sorry, I don't know what %s is!", parts[1])
|
||||||
b.Send(Message, channel, msg)
|
b.Send(Message, channel, msg)
|
||||||
|
@ -201,7 +200,7 @@ func (b *bot) selfSaid(channel, message string, action bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, name := range b.pluginOrdering {
|
for _, name := range b.pluginOrdering {
|
||||||
if b.runCallback(name, SelfMessage, msg) {
|
if b.runCallback(b.plugins[name], SelfMessage, msg) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,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[string]map[Kind][]Callback
|
type CallbackMap map[Plugin]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 {
|
||||||
|
@ -49,7 +49,7 @@ type Bot interface {
|
||||||
// First arg should be one of bot.Message/Reply/Action/etc
|
// First arg should be one of bot.Message/Reply/Action/etc
|
||||||
Receive(Kind, msg.Message, ...interface{})
|
Receive(Kind, msg.Message, ...interface{})
|
||||||
// Register a callback
|
// Register a callback
|
||||||
Register(string, Kind, Callback)
|
Register(Plugin, Kind, Callback)
|
||||||
|
|
||||||
Filter(msg.Message, string) string
|
Filter(msg.Message, string) string
|
||||||
LastMessage(string) (msg.Message, error)
|
LastMessage(string) (msg.Message, error)
|
||||||
|
|
|
@ -31,8 +31,8 @@ func New(b bot.Bot) *AdminPlugin {
|
||||||
db: b.DB(),
|
db: b.DB(),
|
||||||
cfg: b.Config(),
|
cfg: b.Config(),
|
||||||
}
|
}
|
||||||
b.Register("admin", bot.Message, p.message)
|
b.Register(p, bot.Message, p.message)
|
||||||
b.Register("admin", bot.Help, p.help)
|
b.Register(p, bot.Help, p.help)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,24 +52,24 @@ type BabblerArc struct {
|
||||||
Frequency int64 `db:"frequency"`
|
Frequency int64 `db:"frequency"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(bot bot.Bot) *BabblerPlugin {
|
func New(b bot.Bot) *BabblerPlugin {
|
||||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||||
|
|
||||||
if _, err := bot.DB().Exec(`create table if not exists babblers (
|
if _, err := b.DB().Exec(`create table if not exists babblers (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
babbler string
|
babbler string
|
||||||
);`); err != nil {
|
);`); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := bot.DB().Exec(`create table if not exists babblerWords (
|
if _, err := b.DB().Exec(`create table if not exists babblerWords (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
word string
|
word string
|
||||||
);`); err != nil {
|
);`); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := bot.DB().Exec(`create table if not exists babblerNodes (
|
if _, err := b.DB().Exec(`create table if not exists babblerNodes (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
babblerId integer,
|
babblerId integer,
|
||||||
wordId integer,
|
wordId integer,
|
||||||
|
@ -79,7 +79,7 @@ func New(bot bot.Bot) *BabblerPlugin {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := bot.DB().Exec(`create table if not exists babblerArcs (
|
if _, err := b.DB().Exec(`create table if not exists babblerArcs (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
fromNodeId integer,
|
fromNodeId integer,
|
||||||
toNodeId interger,
|
toNodeId interger,
|
||||||
|
@ -89,17 +89,20 @@ func New(bot bot.Bot) *BabblerPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin := &BabblerPlugin{
|
plugin := &BabblerPlugin{
|
||||||
Bot: bot,
|
Bot: b,
|
||||||
db: bot.DB(),
|
db: b.DB(),
|
||||||
WithGoRoutines: true,
|
WithGoRoutines: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin.createNewWord("")
|
plugin.createNewWord("")
|
||||||
|
|
||||||
|
b.Register(plugin, bot.Message, plugin.message)
|
||||||
|
b.Register(plugin, bot.Help, plugin.help)
|
||||||
|
|
||||||
return plugin
|
return plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BabblerPlugin) Message(message msg.Message) bool {
|
func (p *BabblerPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
||||||
lowercase := strings.ToLower(message.Body)
|
lowercase := strings.ToLower(message.Body)
|
||||||
tokens := strings.Fields(lowercase)
|
tokens := strings.Fields(lowercase)
|
||||||
numTokens := len(tokens)
|
numTokens := len(tokens)
|
||||||
|
@ -146,7 +149,7 @@ func (p *BabblerPlugin) Message(message msg.Message) bool {
|
||||||
return saidSomething
|
return saidSomething
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BabblerPlugin) Help(channel string, parts []string) {
|
func (p *BabblerPlugin) help(kind bot.Kind, msg msg.Message, args ...interface{}) bool {
|
||||||
commands := []string{
|
commands := []string{
|
||||||
"initialize babbler for seabass",
|
"initialize babbler for seabass",
|
||||||
"merge babbler drseabass into seabass",
|
"merge babbler drseabass into seabass",
|
||||||
|
@ -155,15 +158,8 @@ func (p *BabblerPlugin) Help(channel string, parts []string) {
|
||||||
"seabass says-middle-out ...",
|
"seabass says-middle-out ...",
|
||||||
"seabass says-bridge ... | ...",
|
"seabass says-bridge ... | ...",
|
||||||
}
|
}
|
||||||
p.Bot.Send(bot.Message, channel, strings.Join(commands, "\n\n"))
|
p.Bot.Send(bot.Message, msg.Channel, strings.Join(commands, "\n\n"))
|
||||||
}
|
return true
|
||||||
|
|
||||||
func (p *BabblerPlugin) Event(kind string, message msg.Message) bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *BabblerPlugin) BotMessage(message msg.Message) bool {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BabblerPlugin) RegisterWeb() *string {
|
func (p *BabblerPlugin) RegisterWeb() *string {
|
||||||
|
@ -934,5 +930,3 @@ func (p *BabblerPlugin) babbleSeedBookends(babblerName string, start, end []stri
|
||||||
|
|
||||||
return strings.Join(words, " "), nil
|
return strings.Join(words, " "), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BabblerPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }
|
|
||||||
|
|
Loading…
Reference in New Issue