twitch-bot/chatcommands.go
Knut Ahlers 0a937a65d0
prepare release v3.0.0
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2022-11-02 22:54:52 +01:00

47 lines
1 KiB
Go

package main
import (
"strings"
"sync"
"github.com/go-irc/irc"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
var (
availableChatcommands = map[string]plugins.MsgModificationFunc{}
availableChatcommandsLock = new(sync.RWMutex)
)
func registerChatcommand(linePrefix string, modFn plugins.MsgModificationFunc) {
availableChatcommandsLock.Lock()
defer availableChatcommandsLock.Unlock()
if _, ok := availableChatcommands[linePrefix]; ok {
log.WithField("linePrefix", linePrefix).Fatal("Duplicate registration of chatcommand")
}
availableChatcommands[linePrefix] = modFn
}
func handleChatcommandModifications(m *irc.Message) error {
availableChatcommandsLock.RLock()
defer availableChatcommandsLock.RUnlock()
msg := m.Trailing()
for prefix, modFn := range availableChatcommands {
if !strings.HasPrefix(msg, prefix) {
continue
}
if err := modFn(m); err != nil {
return errors.Wrap(err, "modifying message")
}
}
return nil
}