74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package plugins
|
|
|
|
import (
|
|
"kurumibot/database/psql"
|
|
"path/filepath"
|
|
|
|
"git.nix13.pw/scuroneko/laniakea"
|
|
"git.nix13.pw/scuroneko/laniakea/tgapi"
|
|
"git.nix13.pw/scuroneko/laniakea/utils"
|
|
)
|
|
|
|
func RegisterAdmin(b *laniakea.Bot) {
|
|
p := laniakea.NewPlugin("Admin")
|
|
p.Command(uploadPhoto, "uploadPhoto")
|
|
p.Command(emojiId, "emojiId")
|
|
p.Command(test, "test")
|
|
|
|
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 test(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
|
ctx.Answer("Ok")
|
|
}
|
|
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))
|
|
}
|