KickerBot/kicker/handlers.go

181 lines
6.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package kicker
import (
"context"
"fmt"
"kickerbot/captchagen"
"kickerbot/db"
"log"
"strconv"
"time"
"git.nefrace.ru/nefrace/tongo"
"github.com/NicoNex/echotron/v3"
)
func userJoined(b *bot, update *echotron.Update) error {
captcha := captchagen.GenCaptcha()
_, 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.From.ID, &echotron.MessageOptions{MessageThreadID: int64(update.Message.ThreadID)})
}
message := update.Message
store := tongo.NewStore[db.User](Client)
user := db.User{
Item: tongo.NewID(),
UserId: message.From.ID,
Username: message.From.Username,
FirstName: message.From.FirstName,
LastName: message.From.LastName,
IsBanned: false,
IsJoined: false,
ChatId: message.Chat.ID,
JoinedMessage: message.ID,
CorrectAnswer: int8(captcha.CorrectAnswer),
DateJoined: time.Now(),
LastNotification: time.Now(),
}
// user.CorrectAnswer = int8(captcha.CorrectAnswer)
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)
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)); 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 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)); err == nil { //d.GetUser(ctx, db.User{UserId: sender.ID, ChatId: message.Chat.ID}); err == nil {
if message.Chat.IsForum {
chat, err := chatStore.GetOne(ctx, tongo.E("chat_id", message.Chat.ID))
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)})
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) {
_ = store.DeleteByID(ctx, user.Id)
solved = true
b.DeleteMessage(message.Chat.ID, message.ID)
b.DeleteMessage(message.Chat.ID, user.CaptchaMessage)
msg := fmt.Sprintf("Капча успешно пройдена пользователем *%s*", UserMention(sender))
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, 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 {
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
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
}