KickerBot/kicker/handlers.go

150 lines
4.5 KiB
Go
Raw Normal View History

2021-12-03 14:30:10 +03:00
package kicker
2021-12-03 12:19:55 +03:00
import (
"context"
"fmt"
2022-01-28 01:00:40 +03:00
"kickerbot/captchagen"
"kickerbot/db"
"log"
2022-02-02 00:37:58 +03:00
"strconv"
"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
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,
IsBanned: false,
2022-02-02 00:37:58 +03:00
ChatId: message.Chat.ID,
JoinedMessage: message.ID,
2022-02-02 00:37:58 +03:00
CorrectAnswer: int8(captcha.CorrectAnswer),
DateJoined: time.Now().Unix(),
}
2022-02-02 00:37:58 +03:00
user.CorrectAnswer = int8(captcha.CorrectAnswer)
d := db.GetDatabase()
2022-02-02 00:37:58 +03:00
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
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)
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
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
}
}
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)
}
} else {
2022-11-09 00:45:08 +03:00
log.Println(err)
return err
}
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)
_ = 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
chat := db.Chat{
2022-11-09 00:45:08 +03:00
Id: m.Chat.ID,
Title: m.Chat.Title,
TopicId: 0,
}
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
}