2021-06-29 12:08:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registerAction(func() Actor { return &ActorSetVariable{} })
|
|
|
|
}
|
|
|
|
|
|
|
|
type ActorSetVariable struct {
|
|
|
|
Variable string `json:"variable" yaml:"variable"`
|
|
|
|
Clear bool `json:"clear" yaml:"clear"`
|
|
|
|
Set string `json:"set" yaml:"set"`
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:12:10 +00:00
|
|
|
func (a ActorSetVariable) Execute(c *irc.Client, m *irc.Message, r *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" }
|