memory leak
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user