nechotron/state.go

56 lines
870 B
Go

package nechotron
import (
"errors"
"github.com/NicoNex/echotron/v3"
)
type StateData map[string]interface{}
type Runnable interface {
Call(*Update) (*State, error)
}
type State struct {
fn stateFn
}
func (s *State) Call(u *Update) (*State, error) {
return s.fn(u)
}
type stateFn func(*Update) (*State, error)
func (d StateData) Set(name string, data interface{}) {
d[name] = data
}
func (d StateData) Get(name string) (interface{}, error) {
data, ok := d[name]
if !ok {
return nil, errors.New("can't get data from statedata")
}
return data, nil
}
func (d StateData) Has(name string) bool {
_, ok := d[name]
return ok
}
func (d StateData) Clear() {
for k := range d {
delete(d, k)
}
}
var EchoState = State{
fn: EchoFunc,
}
func EchoFunc(u *Update) (*State, error) {
u.AnswerText(u.Text(), &echotron.MessageOptions{})
return nil, nil
}