twitch-bot/plugins/interface.go
Knut Ahlers 404ece80ed
Provide HTTP server and registration function
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-08-25 20:55:02 +02:00

64 lines
2.3 KiB
Go

package plugins
import (
"github.com/go-irc/irc"
"github.com/gorilla/mux"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
)
type (
Actor interface {
// Execute will be called after the config was read into the Actor
Execute(*irc.Client, *irc.Message, *Rule) (preventCooldown bool, err error)
// IsAsync may return true if the Execute function is to be executed
// in a Go routine as of long runtime. Normally it should return false
// except in very specific cases
IsAsync() bool
// Name must return an unique name for the actor in order to identify
// it in the logs for debugging purposes
Name() string
}
ActorCreationFunc func() Actor
ActorRegistrationFunc func(ActorCreationFunc)
CronRegistrationFunc func(spec string, cmd func()) (cron.EntryID, error)
HTTPRouterCreationFunc func(name string) *mux.Router
LoggerCreationFunc func(moduleName string) *log.Entry
MsgFormatter func(tplString string, m *irc.Message, r *Rule, fields map[string]interface{}) (string, error)
// RegisterFunc is the type of function your plugin must expose with the name Register
RegisterFunc func(RegistrationArguments) error
RegistrationArguments struct {
// FormatMessage is a method to convert templates into strings using internally known variables / configs
FormatMessage MsgFormatter
// GetHTTPRouter returns a new mux.Router with `/{name}/` prefix
GetHTTPRouter HTTPRouterCreationFunc
// GetLogger returns a sirupsen log.Entry pre-configured with the module name
GetLogger LoggerCreationFunc
// RegisterActor is used to register a new IRC rule-actor implementing the Actor interface
RegisterActor ActorRegistrationFunc
// RegisterCron is a method to register cron functions in the global cron instance
RegisterCron CronRegistrationFunc
// RegisterTemplateFunction can be used to register a new template functions
RegisterTemplateFunction TemplateFuncRegister
// SendMessage can be used to send a message not triggered by an event
SendMessage SendMessageFunc
}
SendMessageFunc func(*irc.Message) error
TemplateFuncGetter func(*irc.Message, *Rule, map[string]interface{}) interface{}
TemplateFuncRegister func(name string, fg TemplateFuncGetter)
)
func GenericTemplateFunctionGetter(f interface{}) TemplateFuncGetter {
return func(*irc.Message, *Rule, map[string]interface{}) interface{} { return f }
}