Common code for implementing Twitch bots.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.3 KiB

package bot
import (
"context"
"raccatta.cc/bot/unstable/conpool"
"raccatta.cc/bot/unstable/joinmgr"
"raccatta.cc/tmi/ircon"
)
type messenger interface {
Message(con *conpool.Con, msg *ircon.Message)
}
// tmi manages conpool events and JOIN/PART via joinmgr.
type tmi struct {
cpool *conpool.Pool
jmgr *joinmgr.JM
messenger messenger
}
func newTMI(join15 int, login, oauth string) *tmi {
cpool := conpool.New(login, oauth)
t := &tmi{
jmgr: joinmgr.New(join15, cpool),
cpool: cpool,
}
t.cpool.MessageHandler(t)
return t
}
func (t *tmi) Run(ctx context.Context) {
t.jmgr.Run(ctx)
}
func (t *tmi) SendInContext(login, rawmsg string) bool {
return t.cpool.SendInContext("#"+login, rawmsg)
}
//
// The following methods implement the conpool interface.
//
func (t *tmi) Connected(c *conpool.Con) {
t.jmgr.Connected(c)
}
func (t *tmi) Disconnected(c *conpool.Con, err error) {
t.jmgr.Disconnected(c, err)
}
// Message handles messages received on a specific connection, which may happen
// concurrently across multiple connections.
func (t *tmi) Message(con *conpool.Con, msg *ircon.Message) {
switch msg.Command {
case "PING":
return
case "JOIN":
t.jmgr.Joined(con, msg)
case "PART":
t.jmgr.Parted(con, msg)
}
t.messenger.Message(con, msg)
}