109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package plugins
|
|
|
|
import (
|
|
"encoding/json"
|
|
"kurumibot/database/psql"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.nix13.pw/scuroneko/laniakea"
|
|
"git.nix13.pw/scuroneko/laniakea/tgapi"
|
|
"git.nix13.pw/scuroneko/laniakea/utils"
|
|
"github.com/vinovest/sqlx"
|
|
)
|
|
|
|
func RegisterAdmin(b *laniakea.Bot) {
|
|
p := laniakea.NewPlugin("Admin")
|
|
p.Command(uploadPhoto, "uploadPhoto")
|
|
p.Command(emojiId, "emojiId")
|
|
p.Command(getProxy, "proxy")
|
|
p.Command(execSql, "sql")
|
|
|
|
p.AddMiddleware(AdminMiddleware())
|
|
b.AddPlugins(p.Build())
|
|
}
|
|
func AdminMiddleware() *laniakea.PluginMiddleware {
|
|
m := laniakea.NewPluginMiddleware(func(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) bool {
|
|
rep := psql.NewUserRepository(db)
|
|
u, err := rep.GetById(ctx.FromID)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return u.Group.IsAdmin
|
|
})
|
|
return m
|
|
}
|
|
|
|
func execSql(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
|
stmt := strings.Join(ctx.Args, " ")
|
|
stmt = db.PostgresSQL.Rebind(stmt)
|
|
|
|
var res []map[string]any
|
|
r, err := db.PostgresSQL.Query(stmt)
|
|
if err != nil {
|
|
ctx.Error(err)
|
|
return
|
|
}
|
|
defer r.Close()
|
|
for r.Next() {
|
|
a := make(map[string]any)
|
|
if err = sqlx.MapScan(r, a); err != nil {
|
|
ctx.Error(err)
|
|
return
|
|
}
|
|
res = append(res, a)
|
|
}
|
|
|
|
data, err := json.MarshalIndent(res, "", " ")
|
|
if err != nil {
|
|
ctx.Error(err)
|
|
return
|
|
}
|
|
ctx.Answerf("`%s`", data)
|
|
}
|
|
func getProxy(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
|
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)
|
|
kb.AddUrlButtonStyle("🇷🇺Russia", laniakea.ButtonStylePrimary, ruProxy)
|
|
kb.AddUrlButtonStyle("🇫🇮Finland", laniakea.ButtonStylePrimary, fiProxy)
|
|
ctx.Keyboard("Доступные прокси", kb)
|
|
}
|
|
func emojiId(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
|
var id string
|
|
for _, e := range ctx.Msg.Entities {
|
|
if e.Type != tgapi.MessageEntityCustomEmoji {
|
|
continue
|
|
}
|
|
id = e.CustomEmojiID
|
|
break
|
|
}
|
|
ctx.Answer(id)
|
|
}
|
|
func uploadPhoto(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
|
ctx.SendAction(tgapi.ChatActionUploadPhoto)
|
|
photoId := ctx.Msg.Photo.Last().FileID
|
|
f, err := ctx.Api.GetFile(tgapi.GetFileP{FileId: photoId})
|
|
if err != nil {
|
|
ctx.Error(err)
|
|
return
|
|
}
|
|
u := tgapi.NewUploader(ctx.Api)
|
|
defer u.Close()
|
|
content, err := ctx.Bot.GetFileByLink(f.FilePath)
|
|
if err != nil {
|
|
ctx.Error(err)
|
|
return
|
|
}
|
|
filename := filepath.Base(f.FilePath)
|
|
msg, err := u.UploadPhoto(tgapi.UploadPhotoP{
|
|
ChatID: ctx.Msg.Chat.ID,
|
|
Caption: ctx.Msg.Caption,
|
|
}, tgapi.NewUploaderFile(filename, content))
|
|
if err != nil {
|
|
ctx.Error(err)
|
|
return
|
|
}
|
|
ctx.Answer(utils.EscapeMarkdown(msg.Photo.Last().FileID))
|
|
}
|