diff --git a/example.config.toml b/example.config.toml index 998e290..5afeda8 100644 --- a/example.config.toml +++ b/example.config.toml @@ -18,9 +18,9 @@ max_backups=3 #消息模板 [message] -join_hint="欢迎 [%s](%s) 加入 %s\n\n⚠️本群已开启新成员验证功能,未通过验证的用户无法发言 \n\n⏱本条消息 %d 秒后自动删除\n\n👇点击下方按钮自助解除禁言" -captcha_image="欢迎您加入[%s]!\n\n⚠本群已开启新成员验证功能。\n\n👆为了证明您不是机器人,请发送以上图片验证码内容\n\n🤖机器人将自动验证您发送的验证码内容是否正确\n\n⏱本条验证消息有效期[%d]秒" -verification_complete="恭喜您成功通过[🤖人机验证],系统已为您解除禁言限制。\n\n如若还是无法发言,请重启telegram客户端" +join_hint="欢迎 [%s](%s) 加入 %s\n ⚠️本群已开启新成员验证功能,未通过验证的用户无法发言 \n超时未完成验证会被移除本群! \n⏱本条消息 %d 秒后自动删除\n👇点击下方按钮自助解除禁言" +captcha_image="欢迎您加入[%s]!\n⚠本群已开启新成员验证功能。\n👆为了证明您不是机器人,请发送以上图片验证码内容\n🤖机器人将自动验证您发送的验证码内容是否正确\n⏱本条验证消息有效期[%d]秒" +verification_complete="恭喜您成功通过[🤖人机验证],系统已为您解除禁言限制。\n如若还是无法发言,请重启telegram客户端\n请同时注意群内发言规范,以防永久禁止" block_hint="\\#封禁预警\n[%s](%s) 请注意,您的消息中含有部分违禁词 \n⚠️您已被系统判断为高风险用户,已被封禁\n系统已向超管发送预警信息,若由超管判定为误杀,会及时将您解除封禁。\n您的违禁词包含:%s" #广告阻止 diff --git a/model/base.go b/model/base.go index 1b4e7b1..e58674b 100644 --- a/model/base.go +++ b/model/base.go @@ -6,8 +6,8 @@ import ( ) type BaseModel struct { - ID int64 `gorm:"column:id;primary_key" json:"id"` - CreatedAt carbon.Timestamp `gorm:"column:created_at" json:"created_at"` - UpdatedAt carbon.Timestamp `gorm:"column:updated_at" json:"updated_at"` - DeletedAt gorm.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` + ID int64 `gorm:"column:id;primary_key" json:"id"` + CreatedAt carbon.DateTime `gorm:"column:created_at" json:"created_at"` + UpdatedAt carbon.DateTime `gorm:"column:updated_at" json:"updated_at"` + DeletedAt gorm.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` } diff --git a/model/user_captcha_record.go b/model/user_captcha_record.go index cb24399..25dc048 100644 --- a/model/user_captcha_record.go +++ b/model/user_captcha_record.go @@ -10,18 +10,18 @@ const ( type UserCaptchaRecord struct { BaseModel - CaptchaId string `gorm:"column:captcha_id;unique_index:captcha_id_index" json:"captcha_id"` //验证id - TelegramChatName string `gorm:"column:telegram_chat_name" json:"telegram_chat_name"` - TelegramUserLastName string `gorm:"column:telegram_user_last_name" json:"telegram_user_last_name"` - TelegramUserFirstName string `gorm:"column:telegram_user_first_name" json:"telegram_user_first_name"` - TelegramUserId int64 `gorm:"column:telegram_user_id" json:"telegram_user_id"` - TelegramChatId int64 `gorm:"column:telegram_chat_id" json:"telegram_chat_id"` - CaptchaMessageId int `gorm:"column:captcha_message_id" json:"captcha_message_id"` // 群-待验证消息id - CaptchaStatus int `gorm:"column:captcha_status" json:"captcha_status"` // 验证状态 -1待验证 1已验证 2已超时 - CaptchaTimeoutMessageId int `gorm:"column:captcha_timeout_message_id" json:"captcha_timeout_message_id"` // 群-验证超时消息id - CaptchaTimeoutTime carbon.Timestamp `gorm:"column:captcha_timeout_time" json:"captcha_timeout_time"` // 验证超时时间 - CaptchaSuccessMessageId int `gorm:"column:captcha_success_message_id" json:"captcha_success_message_id"` // 群-验证成功的消息id - CaptchaSuccessTime carbon.Timestamp `gorm:"column:captcha_success_time" json:"captcha_success_time"` // 验证成功时间 + CaptchaId string `gorm:"column:captcha_id;unique_index:captcha_id_index" json:"captcha_id"` //验证id + TelegramChatName string `gorm:"column:telegram_chat_name" json:"telegram_chat_name"` + TelegramUserLastName string `gorm:"column:telegram_user_last_name" json:"telegram_user_last_name"` + TelegramUserFirstName string `gorm:"column:telegram_user_first_name" json:"telegram_user_first_name"` + TelegramUserId int64 `gorm:"column:telegram_user_id" json:"telegram_user_id"` + TelegramChatId int64 `gorm:"column:telegram_chat_id" json:"telegram_chat_id"` + + CaptchaMessageId int `gorm:"column:captcha_message_id" json:"captcha_message_id"` // 群-待验证消息id + CaptchaStatus int `gorm:"column:captcha_status" json:"captcha_status"` // 验证状态 -1待验证 1已验证 2已超时 + CaptchaTimeoutTime carbon.DateTime `gorm:"column:captcha_timeout_time" json:"captcha_timeout_time"` // 验证超时时间 + CaptchaTimeoutEndTime carbon.DateTime `gorm:"column:captcha_timeout_end_time" json:"captcha_timeout_end_time"` // 验证超时截止时间 + CaptchaSuccessTime carbon.DateTime `gorm:"column:captcha_success_time" json:"captcha_success_time"` // 验证成功时间 CaptchaCodeMessageId int `gorm:"column:captcha_code_message_id" json:"captcha_code_message_id"` // 验证码消息id CaptchaCode string `gorm:"column:captcha_code" json:"captcha_code"` // 验证码内容 diff --git a/service/userCaptchaRecord.go b/service/userCaptchaRecord.go index 10cb18a..90201a8 100644 --- a/service/userCaptchaRecord.go +++ b/service/userCaptchaRecord.go @@ -2,20 +2,19 @@ package service import ( "github.com/assimon/captcha-bot/model" - "github.com/assimon/captcha-bot/util/config" "github.com/assimon/captcha-bot/util/orm" "github.com/golang-module/carbon/v2" ) // CreateCaptchaRecord 创建验证记录 func CreateCaptchaRecord(record *model.UserCaptchaRecord) error { - err := orm.Gdb.Model(record).Create(record).Error + err := orm.Gdb.Model(&model.UserCaptchaRecord{}).Create(record).Error return err } // GetRecordByCaptchaId 通过载荷唯一标识获取记录 func GetRecordByCaptchaId(cId string) (record *model.UserCaptchaRecord, err error) { - err = orm.Gdb.Model(record).Where("captcha_id = ?", cId).Find(record).Error + err = orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_id = ?", cId).Find(&record).Error return } @@ -24,7 +23,7 @@ func TimeoutRecordByCaptchaId(cId string) error { err := orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_id = ?", cId). UpdateColumns(map[string]interface{}{ "captcha_status": model.CaptchaStatusTimeout, - "captcha_timeout_time": carbon.Now().Timestamp(), + "captcha_timeout_time": carbon.Now().ToDateTimeString(), }).Error return err } @@ -34,7 +33,7 @@ func SuccessRecordByCaptchaId(cId string) error { err := orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_id = ?", cId). UpdateColumns(map[string]interface{}{ "captcha_status": model.CaptchaStatusSuccess, - "captcha_success_time": carbon.Now().Timestamp(), + "captcha_success_time": carbon.Now().ToDateTimeString(), }).Error return err } @@ -55,8 +54,7 @@ func SetCaptchaCodeMessageIdByCaptchaId(cId string, msgId int) error { // GetTimeoutCaptchaRecords 获取已经超时的待验证记录 func GetTimeoutCaptchaRecords() (records []model.UserCaptchaRecord, err error) { - now := carbon.Now().SubSeconds(config.SystemC.CaptchaTimeout) err = orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_status = ?", model.CaptchaStatusPending). - Where("created_at > ?", now).Find(&records).Error + Where("captcha_timeout_end_time < ?", carbon.Now().ToDateTimeString()).Find(&records).Error return } diff --git a/telegram/handle.go b/telegram/handle.go index b415f0e..54b19e0 100644 --- a/telegram/handle.go +++ b/telegram/handle.go @@ -262,6 +262,7 @@ func UserJoinGroup(c tb.Context) error { TelegramChatId: c.Message().Chat.ID, CaptchaMessageId: captchaMessage.ID, CaptchaStatus: model.CaptchaStatusPending, + CaptchaTimeoutEndTime: carbon.DateTime{Carbon: carbon.Now().AddSeconds(config.SystemC.CaptchaTimeout)}, } err = service.CreateCaptchaRecord(record) return err diff --git a/telegram/sync.go b/telegram/sync.go index b755509..bbb7f52 100644 --- a/telegram/sync.go +++ b/telegram/sync.go @@ -13,7 +13,7 @@ import ( func RunSyncTask() { c := cron.New() - c.AddFunc("*/10 * * * * *", func() { + c.AddFunc("*/5 * * * * *", func() { go func() { E.MustPanicErrorFunc(TimeoutLeaveGroupToUser) }() diff --git a/util/orm/gorm.go b/util/orm/gorm.go index 815670b..47433df 100644 --- a/util/orm/gorm.go +++ b/util/orm/gorm.go @@ -5,13 +5,16 @@ import ( "github.com/assimon/captcha-bot/util/config" "gorm.io/driver/sqlite" "gorm.io/gorm" + "gorm.io/gorm/logger" "log" ) var Gdb *gorm.DB func InitDb() { - db, err := gorm.Open(sqlite.Open(config.AppPath+"/db/geecaptcha.db"), &gorm.Config{}) + db, err := gorm.Open(sqlite.Open(config.AppPath+"/db/geecaptcha.db"), &gorm.Config{ + Logger: logger.Default.LogMode(logger.Info), + }) if err != nil { log.Fatal("open database err:", err) }