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