feat: approve button

This commit is contained in:
Vaala Cat
2023-08-28 15:57:58 +08:00
parent 46cf57764c
commit 5528766c13
15 changed files with 469 additions and 176 deletions

52
services/mc/auth.go Normal file
View File

@@ -0,0 +1,52 @@
package mc
import (
"sync"
"tg-mc/models"
"time"
)
type Auth interface {
IsAuthed(u models.User, expireMode bool) bool
Auth(u models.User)
Reject(u models.User)
}
type Authcator struct {
UserMap *sync.Map
}
var authcator *Authcator
func GetAuthcator() Auth {
if authcator == nil {
authcator = &Authcator{
UserMap: &sync.Map{},
}
}
return authcator
}
func (a *Authcator) IsAuthed(u models.User, expireMode bool) bool {
if u.MCName != "VaalaCat" {
return true
}
if approveTime, ok := a.UserMap.Load(u.MCName); ok {
if !expireMode {
return true
} else if time.Since(approveTime.(time.Time)) < 30*time.Second {
return true
} else {
return false
}
}
return false
}
func (a *Authcator) Auth(u models.User) {
a.UserMap.Store(u.MCName, time.Now())
}
func (a *Authcator) Reject(u models.User) {
a.UserMap.Delete(u.MCName)
}

View File

@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"tg-mc/conf"
"tg-mc/defs"
"tg-mc/models"
su "tg-mc/services/utils"
"tg-mc/utils"
@@ -34,16 +35,26 @@ func GetLeftPlayer(m chat.Message) (userName string, err error) {
return
}
func HandleJoinGame(userName string, mention bool) {
func HandleJoinGame(userName string, mention bool, expireMode bool) {
u, err := models.GetUserByMCName(userName)
if err != nil {
logrus.Error("get user name error: ", err)
}
time.Sleep(3 * time.Second)
switch u.Status {
case StatusNormal:
if !GetAuthcator().IsAuthed(u, expireMode) {
m := tgbotapi.NewMessage(u.TGID, fmt.Sprintf("MC用户%v 尝试登录请手动允许每次授权持续30秒", userName))
m.ReplyMarkup = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("批准", defs.NewApproveCommand(u.MCName).ToJSON()),
tgbotapi.NewInlineKeyboardButtonData("拒绝", defs.NewRejectCommand(u.MCName).ToJSON())),
)
conf.Bot.Send(m)
kickPlayer(userName)
return
}
if mention {
SendMsgToPlayer("欢迎回来!", userName)
}
@@ -93,14 +104,19 @@ func CronKick() {
utils.CronStart(func() {
users := su.GetAlivePlayerList()
for _, u := range users {
HandleJoinGame(u, false)
HandleJoinGame(u, false, false)
}
})
}
func isBotMsg(msg chat.Message) bool {
if msg.Translate == "commands.message.display.outgoing" {
return true
}
return false
return msg.Translate == "commands.message.display.outgoing"
}
func HandleLeftGame(userName string) {
u, err := models.GetUserByMCName(userName)
if err != nil {
logrus.Error("get user name error: ", err)
}
GetAuthcator().Reject(u)
}

View File

@@ -80,7 +80,14 @@ func onSystemMsg(msg chat.Message, overlay bool) error {
logrus.Error("user join error ", err)
break
}
go HandleJoinGame(userName, true)
go HandleJoinGame(userName, true, true)
case EventPlayerLeft:
userName, err := GetLeftPlayer(msg)
if err != nil {
logrus.Error("user left error ", err)
break
}
go HandleLeftGame(userName)
default:
break
}