220 lines
7.5 KiB
Go
220 lines
7.5 KiB
Go
package kicker
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"kickerbot/captchagen"
|
||
"kickerbot/db"
|
||
"log"
|
||
"strconv"
|
||
"time"
|
||
|
||
"git.nefrace.ru/nefrace/tongo"
|
||
"github.com/NicoNex/echotron/v3"
|
||
"go.mongodb.org/mongo-driver/mongo"
|
||
)
|
||
|
||
func userJoined(b *bot, update *echotron.Update) error {
|
||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||
defer cancel()
|
||
store := tongo.NewStore[db.User](Client)
|
||
usr := update.Message.NewChatMembers[0]
|
||
message := update.Message
|
||
user, err := store.GetOne(ctx, tongo.E("chat_id", update.ChatID()), tongo.E("user_id", usr.ID))
|
||
var captcha *captchagen.Captcha
|
||
if err != nil {
|
||
if errors.Is(err, mongo.ErrNoDocuments) {
|
||
captcha = captchagen.GenCaptcha()
|
||
user = &db.User{
|
||
Item: tongo.NewID(),
|
||
UserId: usr.ID,
|
||
Username: usr.Username,
|
||
FirstName: usr.FirstName,
|
||
LastName: usr.LastName,
|
||
IsJoined: false,
|
||
ChatId: message.Chat.ID,
|
||
JoinedMessage: message.ID,
|
||
CorrectAnswer: int8(captcha.CorrectAnswer),
|
||
DateJoined: time.Now(),
|
||
LastNotification: time.Now(),
|
||
}
|
||
} else {
|
||
log.Printf("Can't find user: %v", err)
|
||
}
|
||
}
|
||
if captcha == nil {
|
||
return nil
|
||
}
|
||
// _, err := b.DeleteMessage(update.Message.Chat.ID, update.Message.ID)
|
||
// if err != nil {
|
||
// log.Printf("Can't delete message: %v", err)
|
||
// }
|
||
bytes, err := captcha.ToBytes()
|
||
if err != nil {
|
||
log.Printf("Error creating captcha bytes: %v", bytes)
|
||
b.SendMessage("Не могу создать капчу, @nefrace, проверь логи.", update.Message.Chat.ID, &echotron.MessageOptions{MessageThreadID: int64(update.Message.ThreadID)})
|
||
}
|
||
|
||
// user.CorrectAnswer = int8(captcha.CorrectAnswer)
|
||
log.Print(user)
|
||
msg := fmt.Sprintf("Приветствую тебя, *[%s](tg://user?id=%d)*\\!\nДля подтверждения, что ты человек, выбери логотип движка, которому посвящен данный чат, и отправь его номер сюда\\.\n*_Я дам тебе десять минут на это\\._*", EscapeText(echotron.MarkdownV2, user.FirstName), user.UserId)
|
||
options := echotron.PhotoOptions{
|
||
Caption: msg,
|
||
ParseMode: echotron.MarkdownV2,
|
||
}
|
||
if message.Chat.IsForum {
|
||
options.MessageThreadID = int(b.CaptchaTopic)
|
||
}
|
||
result, err := b.SendPhoto(echotron.NewInputFileBytes("logos.png", *bytes), message.Chat.ID, &options)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
user.CaptchaMessage = result.Result.ID
|
||
store.InsertOne(ctx, user)
|
||
return nil
|
||
}
|
||
|
||
func userLeft(b *bot, update *echotron.Update) error {
|
||
message := update.Message
|
||
sender := message.From
|
||
store := tongo.NewStore[db.User](Client)
|
||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||
defer cancel()
|
||
if user, err := store.GetOne(ctx, tongo.E("user_id", sender.ID), tongo.E("chat_id", message.Chat.ID), tongo.E("is_joined", false)); err == nil { //d.GetUser(ctx, db.User{UserId: sender.ID, ChatId: message.Chat.ID}); err == nil {
|
||
store.DeleteByID(ctx, user.Id)
|
||
b.DeleteMessage(message.Chat.ID, message.ID)
|
||
b.DeleteMessage(message.Chat.ID, user.CaptchaMessage)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func userBanned(b *bot, update *echotron.Update) error {
|
||
m := update.ChatMember
|
||
c := m.Chat
|
||
u := m.NewChatMember.User
|
||
store := tongo.NewStore[db.User](Client)
|
||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||
defer cancel()
|
||
if user, err := store.GetOne(ctx, tongo.E("user_id", u.ID), tongo.E("chat_id", c.ID)); err == nil { //d.GetUser(ctx, db.User{UserId: sender.ID, ChatId: message.Chat.ID}); err == nil {
|
||
store.DeleteByID(ctx, user.Id)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func checkCaptcha(b *bot, update *echotron.Update) error {
|
||
message := update.Message
|
||
sender := message.From
|
||
store := tongo.NewStore[db.User](Client)
|
||
chatStore := tongo.NewStore[db.Chat](Client)
|
||
// d := db.GetDatabase()
|
||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||
defer cancel()
|
||
if user, err := store.GetOne(ctx, tongo.E("user_id", sender.ID), tongo.E("chat_id", message.Chat.ID), tongo.E("is_joined", false)); err == nil { //d.GetUser(ctx, db.User{UserId: sender.ID, ChatId: message.Chat.ID}); err == nil {
|
||
chat, err := chatStore.GetOne(ctx, tongo.E("chat_id", message.Chat.ID))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if message.Chat.IsForum {
|
||
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)})
|
||
go waitAndDelete(&b.API, res.Result, 10*time.Second)
|
||
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) {
|
||
user.IsJoined = true
|
||
store.ReplaceItem(ctx, *user, true)
|
||
solved = true
|
||
b.DeleteMessage(message.Chat.ID, message.ID)
|
||
b.DeleteMessage(message.Chat.ID, user.CaptchaMessage)
|
||
msg := fmt.Sprintf("Капча успешно пройдена пользователем *%s*", UserMention(sender))
|
||
timeout := 10 * time.Second
|
||
if chat.Greet != "" {
|
||
msg = fmt.Sprintf(chat.Greet, UserMention(sender))
|
||
timeout = 2 * time.Minute
|
||
}
|
||
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)
|
||
}
|
||
go waitAndDelete(&b.API, res.Result, timeout*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 {
|
||
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)
|
||
store.DeleteByID(ctx, user.Id)
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func botAdded(b *bot, update *echotron.Update) error {
|
||
m := update.Message
|
||
store := tongo.NewStore[db.Chat](Client)
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||
defer cancel()
|
||
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
|
||
}
|
||
|
||
func setTopic(b *bot, update *echotron.Update) error {
|
||
m := update.Message
|
||
if res, err := b.GetChatMember(m.Chat.ID, m.From.ID); err == nil {
|
||
m := res.Result
|
||
if !(m.Status == "administrator" || m.Status == "creator") {
|
||
return nil
|
||
}
|
||
}
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||
defer cancel()
|
||
store := tongo.NewStore[db.Chat](Client)
|
||
chat, err := store.GetOne(ctx, tongo.E("chat_id", m.Chat.ID))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
chat.TopicId = int64(m.ThreadID)
|
||
// upd := bson.D{{Key: "$set", Value: bson.D{{Key: "topic_id", Value: m.ThreadID}}}}
|
||
b.CaptchaTopic = int64(m.ThreadID)
|
||
err = store.ReplaceItem(ctx, *chat, false)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
b.DeleteMessage(m.Chat.ID, m.ID)
|
||
b.SendMessage("Данный топик выбран в качестве проверочного для пользователей", m.Chat.ID, &echotron.MessageOptions{MessageThreadID: int64(m.ThreadID)})
|
||
return nil
|
||
}
|