From ece131c14a21bcb51bf4c82a87a5e27a18d971d6 Mon Sep 17 00:00:00 2001 From: ScuroNeko Date: Thu, 12 Feb 2026 11:17:23 +0300 Subject: [PATCH 1/2] refactor --- bot.go | 23 +++-------------------- handler.go | 14 +++++++++++++- plugins.go | 52 +++++++++++++++++----------------------------------- runners.go | 1 + 4 files changed, 34 insertions(+), 56 deletions(-) diff --git a/bot.go b/bot.go index f17db42..930c61c 100644 --- a/bot.go +++ b/bot.go @@ -233,7 +233,6 @@ func (b *Bot) Run() { return } - b.logger.Infoln("Executing runners...") b.ExecRunners() b.logger.Infoln("Bot running. Press CTRL+C to exit.") @@ -247,33 +246,17 @@ func (b *Bot) Run() { }() for { - queue := b.updateQueue - if queue.IsEmpty() { + if b.updateQueue.IsEmpty() { time.Sleep(time.Millisecond * 25) continue } - u := queue.Dequeue() + u := b.updateQueue.Dequeue() if u == nil { b.logger.Errorln("update is nil") continue } - ctx := &MsgContext{Bot: b, Update: u, Api: b.api} - for _, middleware := range b.middlewares { - 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 { - b.handleMessage(u, ctx) - } + b.handle(u) } } diff --git a/handler.go b/handler.go index 487843d..030fe3c 100644 --- a/handler.go +++ b/handler.go @@ -5,6 +5,19 @@ import ( "strings" ) +func (b *Bot) handle(u *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 *Update, ctx *MsgContext) { if update.Message == nil { return @@ -30,7 +43,6 @@ func (b *Bot) handleMessage(update *Update, ctx *MsgContext) { text = strings.TrimSpace(text[len(prefix):]) for _, plugin := range b.plugins { - // Check every command for cmd := range plugin.Commands { if !strings.HasPrefix(text, cmd) { continue diff --git a/plugins.go b/plugins.go index 945bbc3..d6768f9 100644 --- a/plugins.go +++ b/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 diff --git a/runners.go b/runners.go index 64988ec..488a008 100644 --- a/runners.go +++ b/runners.go @@ -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) From 9ef9a240a400998fe67e9d8848f585fe67e17fd6 Mon Sep 17 00:00:00 2001 From: ScuroNeko Date: Thu, 12 Feb 2026 11:47:58 +0300 Subject: [PATCH 2/2] v0.5.0 --- bot.go | 6 ------ tgapi/api.go | 2 +- tgapi/messages_types.go | 8 +++++--- utils.go | 6 ++++++ 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/bot.go b/bot.go index 884a3e2..d3cc5e9 100644 --- a/bot.go +++ b/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 { diff --git a/tgapi/api.go b/tgapi/api.go index bb3ba9f..4cda187 100644 --- a/tgapi/api.go +++ b/tgapi/api.go @@ -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 { diff --git a/tgapi/messages_types.go b/tgapi/messages_types.go index 7a81c33..3662b20 100644 --- a/tgapi/messages_types.go +++ b/tgapi/messages_types.go @@ -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"` diff --git a/utils.go b/utils.go index 7dcedde..debe5f1 100644 --- a/utils.go +++ b/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