mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-12-20 20:01:17 +00:00
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/go-irc/irc"
|
|
"github.com/pkg/errors"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var (
|
|
availableActions []actionFunc
|
|
availableActionsLock = new(sync.RWMutex)
|
|
)
|
|
|
|
type actionFunc func(*irc.Client, *irc.Message, *rule, *ruleAction) error
|
|
|
|
func registerAction(af actionFunc) {
|
|
availableActionsLock.Lock()
|
|
defer availableActionsLock.Unlock()
|
|
|
|
availableActions = append(availableActions, af)
|
|
}
|
|
|
|
func triggerActions(c *irc.Client, m *irc.Message, rule *rule, ra *ruleAction) error {
|
|
availableActionsLock.RLock()
|
|
defer availableActionsLock.RUnlock()
|
|
|
|
for _, af := range availableActions {
|
|
if err := af(c, m, rule, ra); err != nil {
|
|
return errors.Wrap(err, "execute action")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func handleMessage(c *irc.Client, m *irc.Message, event *string) {
|
|
for _, r := range config.GetMatchingRules(m, event) {
|
|
for _, a := range r.Actions {
|
|
if err := triggerActions(c, m, r, a); err != nil {
|
|
log.WithError(err).Error("Unable to trigger action")
|
|
}
|
|
}
|
|
|
|
// Lock command
|
|
timerStore.AddCooldown(r.MatcherID())
|
|
}
|
|
}
|