KickerBot/kicker/handlers.go

183 lines
6.4 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"
2023-02-14 00:50:21 +03:00
"git.nefrace.ru/nefrace/tongo"
"github.com/NicoNex/echotron/v3"
2021-12-03 12:19:55 +03:00
)
2023-02-14 00:50:21 +03:00
func userJoined(b *bot, update *echotron.Update) error {
2022-02-02 00:37:58 +03:00
captcha := captchagen.GenCaptcha()
2023-09-09 01:45:47 +03:00
// _, err := b.DeleteMessage(update.Message.Chat.ID, update.Message.ID)
// if err != nil {
// log.Printf("Can't delete message: %v", err)
// }
2022-11-09 00:45:08 +03:00
bytes, err := captcha.ToBytes()
if err != nil {
2022-12-12 13:23:37 +03:00
log.Printf("Error creating captcha bytes: %v", bytes)
b.SendMessage("Не могу создать капчу, @nefrace, проверь логи.", update.Message.From.ID, &echotron.MessageOptions{MessageThreadID: int64(update.Message.ThreadID)})
2022-11-09 00:45:08 +03:00
}
message := update.Message
2023-02-14 00:50:21 +03:00
store := tongo.NewStore[db.User](Client)
user := db.User{
2023-02-14 11:28:46 +03:00
Item: tongo.NewID(),
UserId: message.From.ID,
Username: message.From.Username,
FirstName: message.From.FirstName,
LastName: message.From.LastName,
IsBanned: false,
IsJoined: false,
2023-02-14 11:28:46 +03:00
ChatId: message.Chat.ID,
JoinedMessage: message.ID,
CorrectAnswer: int8(captcha.CorrectAnswer),
DateJoined: time.Now(),
LastNotification: time.Now(),
}
2023-02-14 00:50:21 +03:00
// user.CorrectAnswer = int8(captcha.CorrectAnswer)
2022-02-02 00:37:58 +03:00
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
log.Print(user)
msg := fmt.Sprintf("Приветствую тебя, *[%s](tg://user?id=%d)*\\!\nДля подтверждения, что ты человек, выбери логотип движка, которому посвящен данный чат, и отправь его номер сюда\\.\n*_Я дам тебе десять минут на это\\._*", EscapeText(echotron.MarkdownV2, user.FirstName), user.UserId)
2023-02-14 00:50:21 +03:00
options := echotron.PhotoOptions{
2022-11-09 00:45:08 +03:00
Caption: msg,
2023-02-14 00:50:21 +03:00
ParseMode: echotron.MarkdownV2,
2022-11-09 00:45:08 +03:00
}
if message.Chat.IsForum {
options.MessageThreadID = int(b.CaptchaTopic)
}
2023-02-14 00:50:21 +03:00
result, err := b.SendPhoto(echotron.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
2023-02-14 00:50:21 +03:00
store.InsertOne(ctx, &user)
return nil
}
2023-02-14 00:50:21 +03:00
func userLeft(b *bot, update *echotron.Update) error {
2022-11-09 00:45:08 +03:00
message := update.Message
sender := message.From
2023-02-14 00:50:21 +03:00
store := tongo.NewStore[db.User](Client)
2022-02-04 11:37:55 +03:00
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
2023-09-09 01:45:47 +03:00
if user, err := store.GetOne(ctx, tongo.E("user_id", sender.ID), tongo.E("chat_id", message.Chat.ID), tongo.E("is_joined", false), tongo.E("is_banned", false)); err == nil { //d.GetUser(ctx, db.User{UserId: sender.ID, ChatId: message.Chat.ID}); err == nil {
2023-02-14 00:50:21 +03:00
store.DeleteByID(ctx, user.Id)
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
}
2023-02-14 00:50:21 +03:00
func checkCaptcha(b *bot, update *echotron.Update) error {
2022-11-09 00:45:08 +03:00
message := update.Message
2022-11-09 01:23:32 +03:00
sender := message.From
2023-02-14 00:50:21 +03:00
store := tongo.NewStore[db.User](Client)
2023-02-14 10:49:39 +03:00
chatStore := tongo.NewStore[db.Chat](Client)
// d := db.GetDatabase()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
2023-02-14 00:50:21 +03:00
if user, err := store.GetOne(ctx, tongo.E("user_id", sender.ID), tongo.E("chat_id", message.Chat.ID)); err == nil { //d.GetUser(ctx, db.User{UserId: sender.ID, ChatId: message.Chat.ID}); err == nil {
2022-11-09 00:45:08 +03:00
if message.Chat.IsForum {
2023-02-14 10:49:39 +03:00
chat, err := chatStore.GetOne(ctx, tongo.E("chat_id", message.Chat.ID))
2022-11-09 00:45:08 +03:00
if err != nil {
return err
}
if message.ThreadID != int(chat.TopicId) {
b.DeleteMessage(message.Chat.ID, message.ID)
text := fmt.Sprintf("*%s*, сначала пройди капчу\\!", UserMention(sender))
res, _ := b.SendMessage(text, message.Chat.ID, &echotron.MessageOptions{ParseMode: echotron.MarkdownV2, MessageThreadID: int64(message.ThreadID)})
2023-02-14 11:07:45 +03:00
go waitAndDelete(&b.API, res.Result, 10*time.Second)
2022-11-09 00:45:08 +03:00
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) {
2023-09-09 01:45:47 +03:00
user.IsJoined = true
store.ReplaceItem(ctx, *user, true)
solved = true
2022-11-09 00:45:08 +03:00
b.DeleteMessage(message.Chat.ID, message.ID)
b.DeleteMessage(message.Chat.ID, user.CaptchaMessage)
msg := fmt.Sprintf("Капча успешно пройдена пользователем *%s*", UserMention(sender))
2023-02-14 00:50:21 +03:00
options := echotron.MessageOptions{
ParseMode: echotron.MarkdownV2,
}
if message.Chat.IsForum {
options.MessageThreadID = b.CaptchaTopic
}
res, err := b.SendMessage(msg, message.Chat.ID, &options)
if err != nil {
log.Printf("Can't send welcome message: %s", err)
}
2023-02-14 00:50:21 +03:00
go waitAndDelete(&b.API, res.Result, 10*time.Second)
// time.Sleep(time.Second * 10)
// _, err = b.DeleteMessage(message.Chat.ID, res.Result.ID)
if err != nil {
log.Printf("Can't delete welcome message: %s", 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)
2023-09-09 01:45:47 +03:00
user.IsBanned = true
store.ReplaceItem(ctx, *user, true)
}
}
return nil
}
2023-02-14 00:50:21 +03:00
func botAdded(b *bot, update *echotron.Update) error {
2022-11-09 00:45:08 +03:00
m := update.Message
2023-02-14 11:07:45 +03:00
store := tongo.NewStore[db.Chat](Client)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
2023-02-14 11:07:45 +03:00
chat, err := store.GetOne(ctx, tongo.E("chat_id", m.Chat.ID))
if err != nil {
chat = &db.Chat{
Item: tongo.NewID(),
ChatId: m.Chat.ID,
Title: m.Chat.Title,
TopicId: 0,
}
}
_, err = store.InsertOne(ctx, chat)
if err != nil {
log.Print(err)
}
return nil
}
2023-02-14 00:50:21 +03:00
func setTopic(b *bot, update *echotron.Update) error {
2022-11-09 00:45:08 +03:00
m := update.Message
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
2023-02-14 00:50:21 +03:00
store := tongo.NewStore[db.Chat](Client)
chat, err := store.GetOne(ctx, tongo.E("chat_id", m.Chat.ID))
2022-11-09 00:45:08 +03:00
if err != nil {
return err
}
2023-02-14 00:50:21 +03:00
chat.TopicId = int64(m.ThreadID)
// upd := bson.D{{Key: "$set", Value: bson.D{{Key: "topic_id", Value: m.ThreadID}}}}
2022-11-09 00:45:08 +03:00
b.CaptchaTopic = int64(m.ThreadID)
2023-02-14 00:50:21 +03:00
err = store.ReplaceItem(ctx, *chat, false)
2022-11-09 00:45:08 +03:00
if err != nil {
return err
}
b.DeleteMessage(m.Chat.ID, m.ID)
b.SendMessage("Данный топик выбран в качестве проверочного для пользователей", m.Chat.ID, &echotron.MessageOptions{MessageThreadID: int64(m.ThreadID)})
2022-11-09 00:45:08 +03:00
return nil
2021-12-03 12:19:55 +03:00
}