59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package plugins
|
|
|
|
import (
|
|
"kurumibot/database/psql"
|
|
"path/filepath"
|
|
|
|
"git.nix13.pw/scuroneko/laniakea"
|
|
)
|
|
|
|
func RegisterAdmin(b *laniakea.Bot) {
|
|
p := laniakea.NewPlugin("Admin")
|
|
p.Command(uploadPhoto, "uploadPhoto")
|
|
p.Command(test, "test")
|
|
|
|
p.Middleware(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 test(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
|
ctx.Answer("Ok")
|
|
}
|
|
func uploadPhoto(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
|
ctx.SendAction(laniakea.ChatActionUploadPhoto)
|
|
photoId := ctx.Msg.Photo.Last().FileID
|
|
f, err := ctx.Bot.GetFile(&laniakea.GetFileP{FileId: photoId})
|
|
if err != nil {
|
|
ctx.Error(err)
|
|
return
|
|
}
|
|
u := laniakea.NewUploader(ctx.Bot)
|
|
content, err := ctx.Bot.GetFileByLink(f.FilePath)
|
|
if err != nil {
|
|
ctx.Error(err)
|
|
return
|
|
}
|
|
filename := filepath.Base(f.FilePath)
|
|
msg, err := u.UploadPhoto(laniakea.NewUploaderFile(filename, content), laniakea.SendPhotoBaseP{
|
|
ChatID: ctx.Msg.Chat.ID,
|
|
Caption: ctx.Msg.Caption,
|
|
})
|
|
if err != nil {
|
|
ctx.Error(err)
|
|
return
|
|
}
|
|
ctx.Answer(laniakea.EscapeMarkdown(msg.Photo.Last().FileID))
|
|
}
|