2021-12-03 14:30:10 +03:00
package kicker
2021-12-03 12:19:55 +03:00
import (
2021-12-06 11:46:11 +03:00
"context"
2021-12-07 00:36:29 +03:00
"fmt"
2022-01-28 01:00:40 +03:00
"kickerbot/captchagen"
2021-12-06 11:46:11 +03:00
"kickerbot/db"
"log"
2022-02-02 00:37:58 +03:00
"strconv"
2021-12-06 11:46:11 +03:00
"time"
2022-11-09 00:45:08 +03:00
tb "github.com/NicoNex/echotron/v3"
"go.mongodb.org/mongo-driver/bson"
2021-12-03 12:19:55 +03:00
)
2022-11-09 00:45:08 +03:00
func userJoined ( b * bot , update * tb . Update ) error {
2022-02-02 00:37:58 +03:00
captcha := captchagen . GenCaptcha ( )
2022-11-09 00:45:08 +03:00
bytes , err := captcha . ToBytes ( )
if err != nil {
fmt . Printf ( "Error creating captcha bytes: %v" , bytes )
b . SendMessage ( "Н е могу создать капчу, @nefrace, проверь логи." , update . Message . From . ID , & tb . MessageOptions { MessageThreadID : update . Message . ThreadID } )
}
message := update . Message
2021-12-07 00:36:29 +03:00
user := db . User {
2022-11-09 00:45:08 +03:00
Id : message . From . ID ,
Username : message . From . Username ,
FirstName : message . From . FirstName ,
LastName : message . From . LastName ,
2021-12-07 00:36:29 +03:00
IsBanned : false ,
2022-02-02 00:37:58 +03:00
ChatId : message . Chat . ID ,
2022-04-05 11:18:10 +03:00
JoinedMessage : message . ID ,
2022-02-02 00:37:58 +03:00
CorrectAnswer : int8 ( captcha . CorrectAnswer ) ,
DateJoined : time . Now ( ) . Unix ( ) ,
2021-12-07 00:36:29 +03:00
}
2022-02-02 00:37:58 +03:00
user . CorrectAnswer = int8 ( captcha . CorrectAnswer )
2021-12-07 10:30:23 +03:00
d := db . GetDatabase ( )
2022-02-02 00:37:58 +03:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , 15 * time . Second )
2021-12-07 10:30:23 +03:00
defer cancel ( )
2021-12-07 00:36:29 +03:00
log . Print ( user )
2022-11-09 00:45:08 +03:00
msg := fmt . Sprintf ( "Приветствую тебя, *[%s](tg://user?id=%d)*\\!\nДля подтверждения, что ты человек, выбери логотип движка, которому посвящен данный чат, и отправь е г о номер сюда\\.\n*_Я дам тебе две минуты на это\\._*" , EscapeText ( tb . MarkdownV2 , user . FirstName ) , user . Id )
options := tb . PhotoOptions {
Caption : msg ,
ParseMode : tb . MarkdownV2 ,
}
if message . Chat . IsForum {
options . MessageThreadID = int ( b . CaptchaTopic )
}
result , err := b . SendPhoto ( tb . NewInputFileBytes ( "logos.png" , * bytes ) , message . Chat . ID , & options )
2022-02-02 00:37:58 +03:00
if err != nil {
return err
}
2022-11-09 00:45:08 +03:00
user . CaptchaMessage = result . Result . ID
2022-02-02 00:37:58 +03:00
d . NewUser ( ctx , user )
2021-12-07 00:36:29 +03:00
return nil
}
2022-11-09 00:45:08 +03:00
func userLeft ( b * bot , update * tb . Update ) error {
message := update . Message
sender := message . From
2022-02-04 11:37:55 +03:00
d := db . GetDatabase ( )
ctx , cancel := context . WithTimeout ( context . Background ( ) , 15 * time . Second )
defer cancel ( )
if user , err := d . GetUser ( ctx , db . User { Id : sender . ID , ChatId : message . Chat . ID } ) ; err == nil {
d . RemoveUser ( ctx , user )
2022-11-09 00:45:08 +03:00
b . DeleteMessage ( message . Chat . ID , message . ID )
b . DeleteMessage ( message . Chat . ID , user . CaptchaMessage )
2022-02-04 11:37:55 +03:00
}
return nil
}
2022-11-09 00:45:08 +03:00
func checkCaptcha ( b * bot , update * tb . Update ) error {
message := update . Message
sender := message . ForwardFrom
2022-07-21 11:15:38 +03:00
d := db . GetDatabase ( )
ctx , cancel := context . WithTimeout ( context . Background ( ) , 15 * time . Second )
defer cancel ( )
if user , err := d . GetUser ( ctx , db . User { Id : sender . ID , ChatId : message . Chat . ID } ) ; err == nil {
2022-11-09 00:45:08 +03:00
if message . Chat . IsForum {
chat , err := d . GetChat ( ctx , message . Chat . ID )
if err != nil {
return err
}
if message . ThreadID != int ( chat . TopicId ) {
b . DeleteMessage ( message . Chat . ID , message . ID )
return nil
}
}
2022-07-21 11:15:38 +03:00
text_runes := [ ] rune ( message . Text )
guess := string ( text_runes [ 0 ] )
solved := false
if num , err := strconv . Atoi ( guess ) ; err == nil {
if num == int ( user . CorrectAnswer ) {
_ = d . RemoveUser ( ctx , user )
solved = true
2022-11-09 00:45:08 +03:00
b . DeleteMessage ( message . Chat . ID , message . ID )
b . DeleteMessage ( message . Chat . ID , user . CaptchaMessage )
2022-07-21 11:15:38 +03:00
}
} else {
2022-11-09 00:45:08 +03:00
log . Println ( err )
return err
2022-07-21 11:15:38 +03:00
}
if ! solved {
2022-11-09 00:45:08 +03:00
b . DeleteMessage ( message . Chat . ID , message . ID )
b . DeleteMessage ( message . Chat . ID , user . CaptchaMessage )
b . DeleteMessage ( message . Chat . ID , user . JoinedMessage )
b . BanChatMember ( message . Chat . ID , sender . ID , nil )
2022-07-21 11:15:38 +03:00
_ = d . RemoveUser ( ctx , user )
}
}
return nil
}
2022-11-09 00:45:08 +03:00
func botAdded ( b * bot , update * tb . Update ) error {
m := update . Message
2022-07-21 11:15:38 +03:00
chat := db . Chat {
2022-11-09 00:45:08 +03:00
Id : m . Chat . ID ,
Title : m . Chat . Title ,
TopicId : 0 ,
2022-07-21 11:15:38 +03:00
}
ctx , cancel := context . WithTimeout ( context . Background ( ) , 10 * time . Second )
defer cancel ( )
d := db . GetDatabase ( )
err := d . NewChat ( ctx , chat )
if err != nil {
log . Print ( err )
}
return nil
}
2022-11-09 00:45:08 +03:00
func setTopic ( b * bot , update * tb . Update ) error {
m := update . Message
d := db . GetDatabase ( )
ctx , cancel := context . WithTimeout ( context . Background ( ) , 5 * time . Second )
defer cancel ( )
chat , err := d . GetChat ( ctx , m . Chat . ID )
if err != nil {
return err
}
upd := bson . D { { Key : "$set" , Value : bson . D { { Key : "topic_id" , Value : m . ThreadID } } } }
b . CaptchaTopic = int64 ( m . ThreadID )
err = d . UpdateChat ( ctx , chat , upd )
if err != nil {
return err
}
b . DeleteMessage ( m . Chat . ID , m . ID )
b . SendMessage ( "Данный топик выбран в качестве проверочного для пользователей" , m . Chat . ID , & tb . MessageOptions { MessageThreadID : m . ThreadID } )
return nil
2021-12-03 12:19:55 +03:00
}