This commit is contained in:
2025-09-29 16:44:08 +03:00
parent 3d1263b3e0
commit 0cc146edd9
2 changed files with 61 additions and 37 deletions

54
bot.go
View File

@@ -217,32 +217,31 @@ func (b *Bot) Run() {
} }
u := queue.Dequeue() u := queue.Dequeue()
ctx := &MsgContext{
Bot: b,
Update: u,
}
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 { if u.CallbackQuery != nil {
b.handleCallback(u) b.handleCallback(u, ctx)
} else { } else {
b.handleMessage(u) b.handleMessage(u, ctx)
} }
} }
} }
// {"callback_query":{"chat_instance":"6202057960757700762","data":"aboba","from":{"first_name":"scuroneko","id":314834933,"is_bot":false,"language_code":"ru","username":"scuroneko"},"id":"1352205741990111553","message":{"chat":{"first_name":"scuroneko","id":314834933,"type":"private","username":"scuroneko"},"date":1734338107,"from":{"first_name":"Kurumi","id":7718900880,"is_bot":true,"username":"kurumi_game_bot"},"message_id":19,"reply_markup":{"inline_keyboard":[[{"callback_data":"aboba","text":"Test"},{"callback_data":"another","text":"Another"}]]},"text":"Aboba"}},"update_id":350979488} // {"callback_query":{"chat_instance":"6202057960757700762","data":"aboba","from":{"first_name":"scuroneko","id":314834933,"is_bot":false,"language_code":"ru","username":"scuroneko"},"id":"1352205741990111553","message":{"chat":{"first_name":"scuroneko","id":314834933,"type":"private","username":"scuroneko"},"date":1734338107,"from":{"first_name":"Kurumi","id":7718900880,"is_bot":true,"username":"kurumi_game_bot"},"message_id":19,"reply_markup":{"inline_keyboard":[[{"callback_data":"aboba","text":"Test"},{"callback_data":"another","text":"Another"}]]},"text":"Aboba"}},"update_id":350979488}
func (b *Bot) handleMessage(update *Update) { func (b *Bot) handleMessage(update *Update, ctx *MsgContext) {
ctx := &MsgContext{
Bot: b,
Update: update,
}
for _, middleware := range b.middlewares {
middleware.Execute(ctx, b.dbContext)
}
for _, plugin := range b.plugins {
if plugin.UpdateListener != nil {
(*plugin.UpdateListener)(ctx, b.dbContext)
}
}
var text string var text string
if update.Message == nil { if update.Message == nil {
return return
@@ -265,7 +264,6 @@ func (b *Bot) handleMessage(update *Update) {
text = strings.TrimSpace(text[len(prefix):]) text = strings.TrimSpace(text[len(prefix):])
for _, plugin := range b.plugins { for _, plugin := range b.plugins {
// Check every command // Check every command
for cmd := range plugin.Commands { for cmd := range plugin.Commands {
if !strings.HasPrefix(text, cmd) { if !strings.HasPrefix(text, cmd) {
@@ -280,23 +278,7 @@ func (b *Bot) handleMessage(update *Update) {
} }
} }
func (b *Bot) handleCallback(update *Update) { func (b *Bot) handleCallback(update *Update, ctx *MsgContext) {
ctx := &MsgContext{
Bot: b,
Update: update,
}
for _, m := range b.middlewares {
m.Execute(ctx, b.dbContext)
}
for _, plugin := range b.plugins {
if plugin.UpdateListener != nil {
lis := *plugin.UpdateListener
lis(ctx, b.dbContext)
}
}
for _, plugin := range b.plugins { for _, plugin := range b.plugins {
for payload := range plugin.Payloads { for payload := range plugin.Payloads {
if !strings.HasPrefix(update.CallbackQuery.Data, payload) { if !strings.HasPrefix(update.CallbackQuery.Data, payload) {

View File

@@ -5,6 +5,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sort"
"strings" "strings"
"time" "time"
@@ -142,6 +143,47 @@ func (l *Logger) formatTraceback(mt *MethodTraceback) string {
return fmt.Sprintf("%s:%s:%d", mt.filename, mt.Method, mt.line) return fmt.Sprintf("%s:%s:%d", mt.filename, mt.Method, mt.line)
} }
func (l *Logger) getFullTraceback(skip int) []*MethodTraceback {
pc := make([]uintptr, 15)
runtime.Callers(skip, pc)
list := make([]*MethodTraceback, 0)
frames := runtime.CallersFrames(pc)
for {
frame, more := frames.Next()
if !more {
break
}
details := runtime.FuncForPC(frame.PC)
signature := details.Name()
path, line := details.FileLine(frame.PC)
splitPath := strings.Split(path, "/")
splitSignature := strings.Split(signature, ".")
pkg, method := splitSignature[0], splitSignature[len(splitSignature)-1]
tb := &MethodTraceback{
filename: splitPath[len(splitPath)-1],
fullPath: path,
line: line,
signature: signature,
Package: pkg,
Method: method,
}
list = append(list, tb)
}
sort.Slice(list, func(i, j int) bool {
return j < i
})
return list
}
func (l *Logger) formatFullTraceback(tracebacks []*MethodTraceback) string {
formatted := make([]string, 0)
for _, tb := range tracebacks {
formatted = append(formatted, l.formatTraceback(tb))
}
return strings.Join(formatted, "->")
}
func (l *Logger) buildString(level LogLevel, m []any) string { func (l *Logger) buildString(level LogLevel, m []any) string {
args := []string{ args := []string{
fmt.Sprintf("[%s]", l.prefix), fmt.Sprintf("[%s]", l.prefix),
@@ -174,7 +216,7 @@ func (l *Logger) print(level LogLevel, m []any) {
} }
for _, writer := range l.writers { for _, writer := range l.writers {
writer(level, l.prefix, l.formatTraceback(l.getTraceback()), m) writer(level, l.prefix, l.formatFullTraceback(l.getFullTraceback(4)), m)
} }
if l.f != nil { if l.f != nil {