mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-12-20 11:51:17 +00:00
Simplify function definitions
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
f2f4d43412
commit
cad3f982cd
6 changed files with 50 additions and 42 deletions
|
@ -8,7 +8,7 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
registerAction(func(c *irc.Client, m *irc.Message, r *ruleAction) error {
|
||||
registerAction(func(c *irc.Client, m *irc.Message, ruleDef *rule, r *ruleAction) error {
|
||||
if r.Ban == nil {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
registerAction(func(c *irc.Client, m *irc.Message, r *ruleAction) error {
|
||||
registerAction(func(c *irc.Client, m *irc.Message, ruleDef *rule, r *ruleAction) error {
|
||||
if r.Counter == nil {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -6,12 +6,12 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
registerAction(func(c *irc.Client, m *irc.Message, r *ruleAction) error {
|
||||
registerAction(func(c *irc.Client, m *irc.Message, ruleDef *rule, r *ruleAction) error {
|
||||
if r.Respond == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
msg, err := formatMessage(*r.Respond, m, nil)
|
||||
msg, err := formatMessage(*r.Respond, m, ruleDef, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "preparing response")
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
registerAction(func(c *irc.Client, m *irc.Message, r *ruleAction) error {
|
||||
registerAction(func(c *irc.Client, m *irc.Message, ruleDef *rule, r *ruleAction) error {
|
||||
if r.Timeout == nil {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ var (
|
|||
availableActionsLock = new(sync.RWMutex)
|
||||
)
|
||||
|
||||
type actionFunc func(*irc.Client, *irc.Message, *ruleAction) error
|
||||
type actionFunc func(*irc.Client, *irc.Message, *rule, *ruleAction) error
|
||||
|
||||
func registerAction(af actionFunc) {
|
||||
availableActionsLock.Lock()
|
||||
|
@ -22,12 +22,12 @@ func registerAction(af actionFunc) {
|
|||
availableActions = append(availableActions, af)
|
||||
}
|
||||
|
||||
func triggerActions(c *irc.Client, m *irc.Message, ra *ruleAction) error {
|
||||
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, ra); err != nil {
|
||||
if err := af(c, m, rule, ra); err != nil {
|
||||
return errors.Wrap(err, "execute action")
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ func triggerActions(c *irc.Client, m *irc.Message, ra *ruleAction) error {
|
|||
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, a); err != nil {
|
||||
if err := triggerActions(c, m, r, a); err != nil {
|
||||
log.WithError(err).Error("Unable to trigger action")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,9 +12,48 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var messageFunctions = korvike.GetFunctionMap()
|
||||
func formatMessage(tplString string, m *irc.Message, r *rule, fields map[string]interface{}) (string, error) {
|
||||
// Create anonymous functions in current context in order to access function variables
|
||||
messageFunctions := korvike.GetFunctionMap()
|
||||
messageFunctions["fixUsername"] = func(username string) string { return strings.TrimLeft(username, "@") }
|
||||
|
||||
func formatMessage(tplString string, m *irc.Message, fields map[string]interface{}) (string, error) {
|
||||
messageFunctions["getArg"] = func(arg int) (string, error) {
|
||||
msgParts := strings.Split(m.Trailing(), " ")
|
||||
if len(msgParts) <= arg {
|
||||
return "", errors.New("argument not found")
|
||||
}
|
||||
|
||||
return msgParts[arg], nil
|
||||
}
|
||||
|
||||
messageFunctions["getCounterValue"] = func(name string, _ ...string) int64 {
|
||||
return store.GetCounterValue(name)
|
||||
}
|
||||
|
||||
messageFunctions["getTag"] = func(tag string) string {
|
||||
s, _ := m.GetTag(tag)
|
||||
return s
|
||||
}
|
||||
|
||||
messageFunctions["group"] = func(idx int) (string, error) {
|
||||
fields := r.matchMessage.FindStringSubmatch(m.Trailing())
|
||||
if len(fields) <= idx {
|
||||
return "", errors.New("group not found")
|
||||
}
|
||||
|
||||
return fields[idx], nil
|
||||
}
|
||||
|
||||
messageFunctions["recentGame"] = func(username string, v ...string) (string, error) {
|
||||
game, _, err := twitch.getRecentStreamInfo(username)
|
||||
if err != nil && len(v) > 0 {
|
||||
return v[0], nil
|
||||
}
|
||||
|
||||
return game, err
|
||||
}
|
||||
|
||||
// Parse and execute template
|
||||
tpl, err := template.
|
||||
New(tplString).
|
||||
Funcs(messageFunctions).
|
||||
|
@ -36,34 +75,3 @@ func formatMessage(tplString string, m *irc.Message, fields map[string]interface
|
|||
|
||||
return buf.String(), errors.Wrap(err, "execute template")
|
||||
}
|
||||
|
||||
func init() {
|
||||
messageFunctions["fixUsername"] = func(username string) string { return strings.TrimLeft(username, "@") }
|
||||
|
||||
messageFunctions["getArg"] = func(m *irc.Message, params ...int) (string, error) {
|
||||
msgParts := strings.Split(m.Trailing(), " ")
|
||||
if len(msgParts) < params[0]+1 {
|
||||
return "", errors.New("argument not found")
|
||||
}
|
||||
|
||||
return msgParts[params[0]], nil
|
||||
}
|
||||
|
||||
messageFunctions["getCounterValue"] = func(name string, _ ...string) int64 {
|
||||
return store.GetCounterValue(name)
|
||||
}
|
||||
|
||||
messageFunctions["getTag"] = func(m *irc.Message, params ...string) string {
|
||||
s, _ := m.GetTag(params[0])
|
||||
return s
|
||||
}
|
||||
|
||||
messageFunctions["recentGame"] = func(username string, v ...string) (string, error) {
|
||||
game, _, err := twitch.getRecentStreamInfo(username)
|
||||
if err != nil && len(v) > 0 {
|
||||
return v[0], nil
|
||||
}
|
||||
|
||||
return game, err
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue