feat: upgrade to new version go-mc

This commit is contained in:
Vaala Cat
2023-12-06 21:40:12 +08:00
parent b023de86ee
commit 511c96c57e
7 changed files with 108 additions and 155 deletions

View File

@@ -1,52 +1,53 @@
package mc
import (
"sync"
"tg-mc/models"
"time"
)
// 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 Auth interface {
// IsAuthed(u models.User, expireMode bool) bool
// Auth(u models.User)
// Reject(u models.User)
// }
type Authcator struct {
UserMap *sync.Map
}
// type Authcator struct {
// UserMap *sync.Map
// }
var authcator *Authcator
// var authcator *Authcator
func GetAuthcator() Auth {
if authcator == nil {
authcator = &Authcator{
UserMap: &sync.Map{},
}
}
return 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) IsAuthed(u models.User, expireMode bool) bool {
// return true
// // 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) Auth(u models.User) {
// a.UserMap.Store(u.MCName, time.Now())
// }
func (a *Authcator) Reject(u models.User) {
a.UserMap.Delete(u.MCName)
}
// func (a *Authcator) Reject(u models.User) {
// a.UserMap.Delete(u.MCName)
// }

View File

@@ -4,10 +4,7 @@ import (
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"tg-mc/conf"
"tg-mc/defs"
"tg-mc/models"
su "tg-mc/services/utils"
"tg-mc/utils"
"time"
@@ -15,8 +12,6 @@ import (
"github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/data/packetid"
"github.com/Tnze/go-mc/net/packet"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/sirupsen/logrus"
)
func GetJoinedPlayer(m chat.Message) (userName string, err error) {
@@ -36,45 +31,46 @@ func GetLeftPlayer(m chat.Message) (userName string, err error) {
}
func HandleJoinGame(userName string, mention bool, expireMode bool) {
// return
u, err := models.GetUserByMCName(userName)
if err != nil {
logrus.Error("get user name error: ", err)
}
// u, err := models.GetUserByMCName(userName)
// if err != nil {
// logrus.Error("get user name error: ", err)
// }
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)
}
case StatusPending:
SendMsgToPlayer("你还没有绑定 Telegram 哦, 5秒后你将会被踢出。请在群组中发送 /bind <你的 MC 用户名> 进行绑定。", userName)
time.Sleep(5 * time.Second)
KickPlayer(userName)
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("用户:%v 没有绑定尝试登录已被T出", userName))
conf.Bot.Send(m)
case StatusBanned:
SendMsgToPlayer("你已被封禁,如有疑问请联系管理员。", userName)
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("用户:%v 已被封禁尝试登录已被T出", userName))
conf.Bot.Send(m)
default:
SendMsgToPlayer("未知错误,请联系管理员,你将被踢出", userName)
time.Sleep(3 * time.Second)
KickPlayer(userName)
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("用户:%v 登录失败错误未知已被T出", userName))
conf.Bot.Send(m)
// 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)
}
// case StatusPending:
// SendMsgToPlayer("你还没有绑定 Telegram 哦, 5秒后你将会被踢出。请在群组中发送 /bind <你的 MC 用户名> 进行绑定。", userName)
// time.Sleep(5 * time.Second)
// KickPlayer(userName)
// m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("用户:%v 没有绑定尝试登录已被T出", userName))
// conf.Bot.Send(m)
// case StatusBanned:
// SendMsgToPlayer("你已被封禁,如有疑问请联系管理员。", userName)
// m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("用户:%v 已被封禁尝试登录已被T出", userName))
// conf.Bot.Send(m)
// default:
// SendMsgToPlayer("未知错误,请联系管理员,你将被踢出", userName)
// time.Sleep(3 * time.Second)
// KickPlayer(userName)
// m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("用户:%v 登录失败错误未知已被T出", userName))
// conf.Bot.Send(m)
// }
}
func SendCommand(cmd string) error {
@@ -114,9 +110,9 @@ func isBotMsg(msg chat.Message) bool {
}
func HandleLeftGame(userName string) {
u, err := models.GetUserByMCName(userName)
if err != nil {
logrus.Error("get user name error: ", err)
}
GetAuthcator().Reject(u)
// u, err := models.GetUserByMCName(userName)
// if err != nil {
// logrus.Error("get user name error: ", err)
// }
// GetAuthcator().Reject(u)
}