mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-14 02:52:42 +00:00
48 lines
1 KiB
Go
48 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/v2/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
|
||
|
}
|