Skip to content

Commit

Permalink
优化一系列操作
Browse files Browse the repository at this point in the history
  • Loading branch information
assimon committed Nov 14, 2023
1 parent b20779b commit 37ba44a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 28 deletions.
6 changes: 3 additions & 3 deletions example.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

#广告阻止
Expand Down
8 changes: 4 additions & 4 deletions model/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
24 changes: 12 additions & 12 deletions model/user_captcha_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"` // 验证码内容
Expand Down
12 changes: 5 additions & 7 deletions service/userCaptchaRecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
1 change: 1 addition & 0 deletions telegram/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion telegram/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func RunSyncTask() {
c := cron.New()
c.AddFunc("*/10 * * * * *", func() {
c.AddFunc("*/5 * * * * *", func() {
go func() {
E.MustPanicErrorFunc(TimeoutLeaveGroupToUser)
}()
Expand Down
5 changes: 4 additions & 1 deletion util/orm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 37ba44a

Please sign in to comment.