2022-10-25 16:47:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2022-10-25 16:47:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|