Captchas are time restricted now
This commit is contained in:
		@ -132,7 +132,7 @@ func handleNewJoined(ctx context.Context, b *bot.Bot, u *models.Update) {
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("Can't send message: ", err, "\n", text)
 | 
			
		||||
		}
 | 
			
		||||
		_, err = db.Exec(`INSERT INTO captchas (user_id, chat_id, message_id) values ($1, $2, $3)`, user.ID, u.Message.Chat.ID, msg.ID)
 | 
			
		||||
		_, err = db.Exec(`INSERT INTO captchas (user_id, chat_id, message_id, created) values ($1, $2, $3, $4)`, user.ID, u.Message.Chat.ID, msg.ID, time.Now().Unix())
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("newusers: can't add to db: ", err)
 | 
			
		||||
			return
 | 
			
		||||
@ -254,7 +254,7 @@ var NewCaptchaTemplate = `
 | 
			
		||||
*%s*, тебе необходимо пройти капчу для чата *%s*\.
 | 
			
		||||
 | 
			
		||||
Для этого посмотри на картинку, найди логотип движка, который относится к вышеуказанному чату, а потом введи сюда код, который расположен над или под ним\.
 | 
			
		||||
Время у тебя неограничено, я буду ждать\!
 | 
			
		||||
Я буду ждать\!
 | 
			
		||||
`
 | 
			
		||||
 | 
			
		||||
func handlePrivateStartCaptcha(ctx context.Context, b *bot.Bot, u *models.Update) {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										26
									
								
								src/main.go
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								src/main.go
									
									
									
									
									
								
							@ -77,7 +77,8 @@ task:
 | 
			
		||||
	for {
 | 
			
		||||
		select {
 | 
			
		||||
		case <-timer.C:
 | 
			
		||||
			deleteOldMessages(ctx, b)
 | 
			
		||||
			go deleteOldMessages(ctx, b)
 | 
			
		||||
			deleteOldCaptchas(ctx, b)
 | 
			
		||||
		case <-ctx.Done():
 | 
			
		||||
			break task
 | 
			
		||||
		}
 | 
			
		||||
@ -103,3 +104,26 @@ func deleteOldMessages(ctx context.Context, b *bot.Bot) {
 | 
			
		||||
		db.Exec(`DELETE FROM messagesToDelete WHERE id = $1`, message.Id)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
func deleteOldCaptchas(ctx context.Context, b *bot.Bot) {
 | 
			
		||||
	date := time.Now().Add(-24 * time.Hour).Unix()
 | 
			
		||||
	captchas := []Captcha{}
 | 
			
		||||
	err := db.Select(&captchas, `SELECT * from captchas WHERE created < $1`, date)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Println("delete_old: can't get messages: ", err)
 | 
			
		||||
	}
 | 
			
		||||
	for _, captcha := range captchas {
 | 
			
		||||
		if _, err := b.UnbanChatMember(ctx, &bot.UnbanChatMemberParams{
 | 
			
		||||
			ChatID: captcha.ChatID,
 | 
			
		||||
			UserID: captcha.UserID,
 | 
			
		||||
		}); err != nil {
 | 
			
		||||
			time.Sleep(200 * time.Millisecond)
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		time.Sleep(200 * time.Millisecond)
 | 
			
		||||
		success, err := b.DeleteMessage(ctx, &bot.DeleteMessageParams{ChatID: captcha.ChatID, MessageID: captcha.MessageID})
 | 
			
		||||
		if !success {
 | 
			
		||||
			log.Println("delete_old, captcha wasn't deleted: ", captcha.MessageID, " :: ", err)
 | 
			
		||||
		}
 | 
			
		||||
		db.Exec(`DELETE FROM captchas WHERE id = $1`, captcha.Id)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user