fix: support kick cron
This commit is contained in:
parent
f0c2083bd7
commit
60d6572331
@ -9,7 +9,7 @@ import (
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
TGID int64
|
||||
TGID int64 `gorm:"unique"`
|
||||
MCName string
|
||||
Status int // 0: pending, 1: normal, 2: banned
|
||||
}
|
||||
@ -38,15 +38,13 @@ func GetUserByMCName(mcName string) (user User, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func CreateUserIfNotExist(tgID int64, u *User) (err error) {
|
||||
err = database.GetDB().Where(
|
||||
&User{TGID: tgID},
|
||||
).FirstOrCreate(&u).Error
|
||||
func CreateUser(u *User) (err error) {
|
||||
err = database.GetDB().Create(&u).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (u *User) Delete(tgID int64) error {
|
||||
return database.GetDB().Where(
|
||||
&User{TGID: tgID},
|
||||
).Delete(&u).Error
|
||||
).Unscoped().Delete(&u).Error
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ func GetLeftPlayer(m chat.Message) (userName string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func HandleJoinGame(userName string) {
|
||||
func HandleJoinGame(userName string, mention bool) {
|
||||
|
||||
u, err := models.GetUserByMCName(userName)
|
||||
if err != nil {
|
||||
@ -42,7 +42,9 @@ func HandleJoinGame(userName string) {
|
||||
|
||||
switch u.Status {
|
||||
case StatusNormal:
|
||||
if mention {
|
||||
SendMsgToPlayer("欢迎回来!", userName)
|
||||
}
|
||||
case StatusPending:
|
||||
SendMsgToPlayer("你还没有绑定 Telegram 哦, 5秒后你将会被踢出。请在群组中发送 /bind <你的 MC 用户名> 进行绑定。", userName)
|
||||
time.Sleep(5 * time.Second)
|
||||
@ -83,7 +85,7 @@ func CronKick() {
|
||||
utils.CronStart(func() {
|
||||
users := su.GetAlivePlayerList()
|
||||
for _, u := range users {
|
||||
kickPlayer(u)
|
||||
HandleJoinGame(u, false)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -80,12 +80,12 @@ func onSystemMsg(msg chat.Message, overlay bool) error {
|
||||
logrus.Error("user join error ", err)
|
||||
break
|
||||
}
|
||||
go HandleJoinGame(userName)
|
||||
go HandleJoinGame(userName, true)
|
||||
default:
|
||||
break
|
||||
}
|
||||
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("%v", msg))
|
||||
conf.Bot.Send(m)
|
||||
// m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("%v", msg))
|
||||
// conf.Bot.Send(m)
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
@ -59,11 +59,16 @@ func Run(sendFunc func(string)) {
|
||||
}
|
||||
if m.Command() == "bind" {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
models.CreateUserIfNotExist(m.From.ID, &models.User{
|
||||
err := models.CreateUser(&models.User{
|
||||
TGID: m.From.ID,
|
||||
MCName: m.CommandArguments(),
|
||||
Status: 1,
|
||||
})
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "绑定失败, err: "+err.Error())
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
m := tgbotapi.NewMessage(m.Chat.ID,
|
||||
fmt.Sprintf("绑定成功,你的MCID是%v", m.CommandArguments()))
|
||||
conf.Bot.Send(m)
|
||||
@ -87,6 +92,18 @@ func Run(sendFunc func(string)) {
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
if m.Command() == "get" {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
u, err := models.GetUserByTGID(m.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "你还没有绑定")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("你的MCID是%v", u.MCName))
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
}(update.Message)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user