memory leak

This commit is contained in:
2026-02-05 15:14:47 +03:00
parent b894cbd9c3
commit 0fded455b8
22 changed files with 420 additions and 170 deletions

View File

@@ -1,20 +1,65 @@
package plugins
import (
"bytes"
"encoding/json"
"fmt"
"kurumibot/database/mdb"
"strings"
"time"
"git.nix13.pw/scuroneko/extypes"
"git.nix13.pw/scuroneko/laniakea"
"git.nix13.pw/scuroneko/slog"
)
func InitLogsPlugin() {}
func RegisterLogs(bot *laniakea.Bot) {
p := laniakea.NewPlugin("Logs")
p.Command(getLogs, "logs")
p.Command(getMsgLogs, "msglogs")
p.Middleware(AdminMiddleware())
bot.AddPlugins(p.Build())
}
func InitLogMiddleware() laniakea.Middleware {
return laniakea.NewMiddleware("LogMiddleware", logMiddleware).SetAsync(true).Build()
}
func getLogs(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
logs, err := mdb.GetConsoleLogs(db)
if err != nil {
ctx.Error(err)
return
}
out := encodeLogs(logs)
ctx.Answer(strings.Join(out, ""))
}
func getMsgLogs(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
logs, err := mdb.GetMessageLogs(db)
if err != nil {
ctx.Error(err)
return
}
out := encodeLogs(logs)
ctx.Answer(strings.Join(out, ""))
}
// Utils
func encodeLogs[T comparable](logs extypes.Slice[T]) extypes.Slice[string] {
out := make(extypes.Slice[string], len(logs))
buf := bytes.NewBuffer(nil)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
for i, log := range logs {
_ = enc.Encode(log)
out[i] = fmt.Sprintf("`%s`", buf.String())
buf.Reset()
}
return out
}
func logMiddleware(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
if ctx.Msg == nil {
return