Merge branch 'dev' of nix13.pw:ScuroNeko/Laniakea into dev
This commit is contained in:
6
bot.go
6
bot.go
@@ -262,12 +262,6 @@ func (b *Bot) Run() {
|
||||
middleware.Execute(ctx, b.dbContext)
|
||||
}
|
||||
|
||||
for _, plugin := range b.plugins {
|
||||
if plugin.UpdateListener != nil {
|
||||
(*plugin.UpdateListener)(ctx, b.dbContext)
|
||||
}
|
||||
}
|
||||
|
||||
if u.CallbackQuery != nil {
|
||||
b.handleCallback(u, ctx)
|
||||
} else {
|
||||
|
||||
13
handler.go
13
handler.go
@@ -7,6 +7,19 @@ import (
|
||||
"git.nix13.pw/scuroneko/laniakea/tgapi"
|
||||
)
|
||||
|
||||
func (b *Bot) handle(u *tgapi.Update) {
|
||||
ctx := &MsgContext{Bot: b, Update: *u, Api: b.api}
|
||||
for _, middleware := range b.middlewares {
|
||||
middleware.Execute(ctx, b.dbContext)
|
||||
}
|
||||
|
||||
if u.CallbackQuery != nil {
|
||||
b.handleCallback(u, ctx)
|
||||
} else {
|
||||
b.handleMessage(u, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) handleMessage(update *tgapi.Update, ctx *MsgContext) {
|
||||
if update.Message == nil {
|
||||
return
|
||||
|
||||
52
plugins.go
52
plugins.go
@@ -9,49 +9,42 @@ import (
|
||||
type CommandExecutor func(ctx *MsgContext, dbContext *DatabaseContext)
|
||||
|
||||
type PluginBuilder struct {
|
||||
name string
|
||||
commands map[string]*CommandExecutor
|
||||
payloads map[string]*CommandExecutor
|
||||
updateListener *CommandExecutor
|
||||
middlewares extypes.Slice[*PluginMiddleware]
|
||||
name string
|
||||
commands map[string]CommandExecutor
|
||||
payloads map[string]CommandExecutor
|
||||
middlewares extypes.Slice[*PluginMiddleware]
|
||||
}
|
||||
|
||||
type Plugin struct {
|
||||
Name string
|
||||
Commands map[string]*CommandExecutor
|
||||
Payloads map[string]*CommandExecutor
|
||||
UpdateListener *CommandExecutor
|
||||
Middlewares extypes.Slice[*PluginMiddleware]
|
||||
Name string
|
||||
Commands map[string]CommandExecutor
|
||||
Payloads map[string]CommandExecutor
|
||||
Middlewares extypes.Slice[*PluginMiddleware]
|
||||
}
|
||||
|
||||
func NewPlugin(name string) *PluginBuilder {
|
||||
return &PluginBuilder{
|
||||
name: name,
|
||||
commands: make(map[string]*CommandExecutor),
|
||||
payloads: make(map[string]*CommandExecutor),
|
||||
commands: make(map[string]CommandExecutor),
|
||||
payloads: make(map[string]CommandExecutor),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PluginBuilder) Command(f CommandExecutor, cmd ...string) *PluginBuilder {
|
||||
for _, c := range cmd {
|
||||
p.commands[c] = &f
|
||||
p.commands[c] = f
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *PluginBuilder) Payload(f CommandExecutor, payloads ...string) *PluginBuilder {
|
||||
for _, payload := range payloads {
|
||||
p.payloads[payload] = &f
|
||||
p.payloads[payload] = f
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *PluginBuilder) UpdateListener(listener CommandExecutor) *PluginBuilder {
|
||||
p.updateListener = &listener
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *PluginBuilder) Middleware(middleware *PluginMiddleware) *PluginBuilder {
|
||||
func (p *PluginBuilder) AddMiddleware(middleware *PluginMiddleware) *PluginBuilder {
|
||||
p.middlewares = p.middlewares.Push(middleware)
|
||||
return p
|
||||
}
|
||||
@@ -61,20 +54,17 @@ func (p *PluginBuilder) Build() Plugin {
|
||||
log.Printf("no command or payloads for %s", p.name)
|
||||
}
|
||||
return Plugin{
|
||||
Name: p.name,
|
||||
Commands: p.commands,
|
||||
Payloads: p.payloads,
|
||||
UpdateListener: p.updateListener,
|
||||
Middlewares: p.middlewares,
|
||||
p.name, p.commands,
|
||||
p.payloads, p.middlewares,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) Execute(cmd string, ctx *MsgContext, dbContext *DatabaseContext) {
|
||||
(*p.Commands[cmd])(ctx, dbContext)
|
||||
(p.Commands[cmd])(ctx, dbContext)
|
||||
}
|
||||
|
||||
func (p *Plugin) ExecutePayload(payload string, ctx *MsgContext, dbContext *DatabaseContext) {
|
||||
(*p.Payloads[payload])(ctx, dbContext)
|
||||
(p.Payloads[payload])(ctx, dbContext)
|
||||
}
|
||||
|
||||
func (p *Plugin) executeMiddlewares(ctx *MsgContext, db *DatabaseContext) bool {
|
||||
@@ -102,14 +92,6 @@ type MiddlewareBuilder struct {
|
||||
func NewMiddleware(name string, executor CommandExecutor) *MiddlewareBuilder {
|
||||
return &MiddlewareBuilder{name: name, executor: executor, order: 0, async: false}
|
||||
}
|
||||
func (m *MiddlewareBuilder) SetName(name string) *MiddlewareBuilder {
|
||||
m.name = name
|
||||
return m
|
||||
}
|
||||
func (m *MiddlewareBuilder) SetExecutor(executor CommandExecutor) *MiddlewareBuilder {
|
||||
m.executor = executor
|
||||
return m
|
||||
}
|
||||
func (m *MiddlewareBuilder) SetOrder(order int) *MiddlewareBuilder {
|
||||
m.order = order
|
||||
return m
|
||||
|
||||
@@ -44,6 +44,7 @@ func (b *RunnerBuilder) Build() Runner {
|
||||
}
|
||||
|
||||
func (b *Bot) ExecRunners() {
|
||||
b.logger.Infoln("Executing runners...")
|
||||
for _, runner := range b.runners {
|
||||
if !runner.Onetime && !runner.Async {
|
||||
b.logger.Warnf("Runner %s not onetime, but sync\n", runner.Name)
|
||||
|
||||
@@ -23,7 +23,7 @@ type Api struct {
|
||||
func NewAPI(token string) *Api {
|
||||
l := slog.CreateLogger().Level(utils.GetLoggerLevel()).Prefix("API")
|
||||
l.AddWriter(l.CreateJsonStdoutWriter())
|
||||
client := &http.Client{Timeout: time.Second * 10}
|
||||
client := &http.Client{Timeout: time.Second * 45}
|
||||
return &Api{token, client, l}
|
||||
}
|
||||
func (api *Api) CloseApi() error {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package tgapi
|
||||
|
||||
import "git.nix13.pw/scuroneko/extypes"
|
||||
|
||||
type MessageReplyMarkup struct {
|
||||
InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard"`
|
||||
}
|
||||
@@ -26,9 +28,9 @@ type Message struct {
|
||||
|
||||
Text string `json:"text"`
|
||||
|
||||
Photo []*PhotoSize `json:"photo,omitempty"`
|
||||
Caption string `json:"caption,omitempty"`
|
||||
CaptionEntities []MessageEntity `json:"caption_entities,omitempty"`
|
||||
Photo extypes.Slice[*PhotoSize] `json:"photo,omitempty"`
|
||||
Caption string `json:"caption,omitempty"`
|
||||
CaptionEntities []MessageEntity `json:"caption_entities,omitempty"`
|
||||
|
||||
Date int `json:"date"`
|
||||
EditDate int `json:"edit_date"`
|
||||
|
||||
6
utils.go
6
utils.go
@@ -1,5 +1,7 @@
|
||||
package laniakea
|
||||
|
||||
import "git.nix13.pw/scuroneko/laniakea/utils"
|
||||
|
||||
func Ptr[T any](v T) *T { return &v }
|
||||
|
||||
func Val[T any](p *T, def T) T {
|
||||
@@ -8,3 +10,7 @@ func Val[T any](p *T, def T) T {
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
const VersionString = utils.VersionString
|
||||
|
||||
var EscapeMarkdown = utils.EscapeMarkdown
|
||||
|
||||
Reference in New Issue
Block a user