refactoring, fixes and laniakea v0.8
This commit is contained in:
@@ -2,9 +2,10 @@ package plugins
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"kurumibot/database/psql"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"ymgb/database"
|
||||
"ymgb/database/psql"
|
||||
|
||||
"git.nix13.pw/scuroneko/laniakea"
|
||||
"git.nix13.pw/scuroneko/laniakea/tgapi"
|
||||
@@ -12,18 +13,18 @@ import (
|
||||
"github.com/vinovest/sqlx"
|
||||
)
|
||||
|
||||
func RegisterAdmin(b *laniakea.Bot) {
|
||||
p := laniakea.NewPlugin("Admin")
|
||||
func RegisterAdmin() *laniakea.Plugin[database.Context] {
|
||||
p := laniakea.NewPlugin[database.Context]("Admin")
|
||||
p.AddCommand(laniakea.NewCommand(uploadPhoto, "uploadPhoto").SkipCommandAutoGen())
|
||||
p.AddCommand(laniakea.NewCommand(emojiId, "emojiId").SkipCommandAutoGen())
|
||||
p.AddCommand(laniakea.NewCommand(getProxy, "proxy").SkipCommandAutoGen())
|
||||
p.AddCommand(laniakea.NewCommand(execSql, "sql").SkipCommandAutoGen())
|
||||
|
||||
p.AddMiddleware(AdminMiddleware())
|
||||
b.AddPlugins(p)
|
||||
return p
|
||||
}
|
||||
func AdminMiddleware() laniakea.Middleware {
|
||||
m := laniakea.NewMiddleware("AdminMiddleware", func(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) bool {
|
||||
func AdminMiddleware() laniakea.Middleware[database.Context] {
|
||||
m := laniakea.NewMiddleware("AdminMiddleware", func(ctx *laniakea.MsgContext, db *database.Context) bool {
|
||||
rep := psql.NewUserRepository(db)
|
||||
u, err := rep.GetById(ctx.FromID)
|
||||
if err != nil {
|
||||
@@ -34,12 +35,12 @@ func AdminMiddleware() laniakea.Middleware {
|
||||
return *m
|
||||
}
|
||||
|
||||
func execSql(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
func execSql(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
stmt := strings.Join(ctx.Args, " ")
|
||||
stmt = db.PostgresSQL.Rebind(stmt)
|
||||
stmt = db.Postgres.Rebind(stmt)
|
||||
|
||||
var res []map[string]any
|
||||
r, err := db.PostgresSQL.Query(stmt)
|
||||
r, err := db.Postgres.Query(stmt)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
@@ -61,7 +62,7 @@ func execSql(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
}
|
||||
ctx.Answerf("`%s`", data)
|
||||
}
|
||||
func getProxy(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
||||
func getProxy(ctx *laniakea.MsgContext, _ *database.Context) {
|
||||
ruProxy := "tg://proxy?port=3128&secret=7qaZyfQN-IQ7ZMwrR_zWnHBvem9uLnJ1&server=185.231.245.25"
|
||||
fiProxy := "tg://proxy?port=3128&secret=7vmNtw_233xvIRFvImm2PLtvem9uLnJ1&server=46.243.6.125"
|
||||
kb := laniakea.NewInlineKeyboard(1)
|
||||
@@ -69,7 +70,7 @@ func getProxy(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
||||
kb.AddUrlButtonStyle("🇫🇮Finland", laniakea.ButtonStylePrimary, fiProxy)
|
||||
ctx.Keyboard("Доступные прокси", kb)
|
||||
}
|
||||
func emojiId(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
||||
func emojiId(ctx *laniakea.MsgContext, _ *database.Context) {
|
||||
var id string
|
||||
for _, e := range ctx.Msg.Entities {
|
||||
if e.Type != tgapi.MessageEntityCustomEmoji {
|
||||
@@ -80,7 +81,7 @@ func emojiId(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
||||
}
|
||||
ctx.Answer(id)
|
||||
}
|
||||
func uploadPhoto(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
||||
func uploadPhoto(ctx *laniakea.MsgContext, _ *database.Context) {
|
||||
ctx.SendAction(tgapi.ChatActionUploadPhoto)
|
||||
photoId := ctx.Msg.Photo.Last().FileID
|
||||
f, err := ctx.Api.GetFile(tgapi.GetFileP{FileId: photoId})
|
||||
@@ -90,7 +91,7 @@ func uploadPhoto(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
||||
}
|
||||
u := tgapi.NewUploader(ctx.Api)
|
||||
defer u.Close()
|
||||
content, err := ctx.Bot.GetFileByLink(f.FilePath)
|
||||
content, err := ctx.Api.GetFileByLink(f.FilePath)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user