2021-06-29 12:08:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-08-19 13:33:56 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
2021-06-29 12:08:26 +00:00
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-08-19 13:33:56 +00:00
|
|
|
registerAction(func() plugins.Actor { return &ActorSetVariable{} })
|
2021-06-29 12:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ActorSetVariable struct {
|
|
|
|
Variable string `json:"variable" yaml:"variable"`
|
|
|
|
Clear bool `json:"clear" yaml:"clear"`
|
|
|
|
Set string `json:"set" yaml:"set"`
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func (a ActorSetVariable) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule) (preventCooldown bool, err error) {
|
2021-06-29 12:08:26 +00:00
|
|
|
if a.Variable == "" {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, nil
|
2021-06-29 12:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
varName, err := formatMessage(a.Variable, m, r, nil)
|
|
|
|
if err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing variable name")
|
2021-06-29 12:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if a.Clear {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(
|
2021-06-29 12:08:26 +00:00
|
|
|
store.RemoveVariable(varName),
|
|
|
|
"removing variable",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := formatMessage(a.Set, m, r, nil)
|
|
|
|
if err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing value")
|
2021-06-29 12:08:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(
|
2021-06-29 12:08:26 +00:00
|
|
|
store.SetVariable(varName, value),
|
|
|
|
"setting variable",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a ActorSetVariable) IsAsync() bool { return false }
|
|
|
|
func (a ActorSetVariable) Name() string { return "setvariable" }
|