2021-06-29 12:08:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-08-28 15:27:24 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
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"
|
2021-08-28 15:27:24 +00:00
|
|
|
"github.com/gorilla/mux"
|
2021-06-29 12:08:26 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-08-19 13:33:56 +00:00
|
|
|
registerAction(func() plugins.Actor { return &ActorSetVariable{} })
|
2021-08-28 15:27:24 +00:00
|
|
|
|
|
|
|
registerRoute(plugins.HTTPRouteRegistrationArgs{
|
|
|
|
Description: "Returns the value as a plain string",
|
|
|
|
HandlerFunc: routeActorSetVarGetValue,
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Module: "setvariable",
|
|
|
|
Name: "Get Variable Value",
|
|
|
|
Path: "/{name}",
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
|
|
|
|
RouteParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "Name of the variable to query",
|
|
|
|
Name: "name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
registerRoute(plugins.HTTPRouteRegistrationArgs{
|
|
|
|
Description: "Updates the value of the variable",
|
|
|
|
HandlerFunc: routeActorSetVarSetValue,
|
|
|
|
Method: http.MethodPatch,
|
|
|
|
Module: "setvariable",
|
|
|
|
Name: "Set Variable Value",
|
|
|
|
Path: "/{name}",
|
|
|
|
QueryParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "Value to set for the given variable",
|
|
|
|
Name: "value",
|
|
|
|
Required: true,
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RouteParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "Name of the variable to update",
|
|
|
|
Name: "name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
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-09-02 21:26:39 +00:00
|
|
|
func (a ActorSetVariable) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData plugins.FieldCollection) (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
|
|
|
}
|
|
|
|
|
2021-09-02 15:09:30 +00:00
|
|
|
varName, err := formatMessage(a.Variable, m, r, eventData)
|
2021-06-29 12:08:26 +00:00
|
|
|
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",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-02 15:09:30 +00:00
|
|
|
value, err := formatMessage(a.Set, m, r, eventData)
|
2021-06-29 12:08:26 +00:00
|
|
|
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" }
|
2021-08-28 15:27:24 +00:00
|
|
|
|
|
|
|
func routeActorSetVarGetValue(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text-plain")
|
|
|
|
fmt.Fprint(w, store.GetVariable(mux.Vars(r)["name"]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func routeActorSetVarSetValue(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := store.SetVariable(mux.Vars(r)["name"], r.FormValue("value")); err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "updating value").Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|