132 lines
3.5 KiB
Go
132 lines
3.5 KiB
Go
package kicker
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"kickerbot/captchagen"
|
||
"kickerbot/db"
|
||
"log"
|
||
"strconv"
|
||
"time"
|
||
|
||
tb "gopkg.in/tucnak/telebot.v3"
|
||
)
|
||
|
||
func userJoined(c tb.Context) error {
|
||
bot := c.Bot()
|
||
captcha := captchagen.GenCaptcha()
|
||
reader := captcha.ToReader()
|
||
message := c.Message()
|
||
user := db.User{
|
||
Id: message.Sender.ID,
|
||
Username: message.Sender.Username,
|
||
FirstName: message.Sender.FirstName,
|
||
LastName: message.Sender.LastName,
|
||
IsBanned: false,
|
||
ChatId: message.Chat.ID,
|
||
CorrectAnswer: int8(captcha.CorrectAnswer),
|
||
DateJoined: time.Now().Unix(),
|
||
}
|
||
user.CorrectAnswer = int8(captcha.CorrectAnswer)
|
||
d := db.GetDatabase()
|
||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||
defer cancel()
|
||
log.Print(user)
|
||
msg := fmt.Sprintf("Приветствую тебя, %v!\nДля подтверждения, что ты человек, выбери логотип движка, которому посвящен данный чат, и отправь его номер сюда.\nЯ дам тебе минуту на это.", user.FirstName)
|
||
photo := tb.Photo{File: tb.FromReader(reader), Caption: msg}
|
||
result, err := bot.Send(tb.ChatID(message.Chat.ID), &photo, &tb.SendOptions{ReplyTo: message})
|
||
if err != nil {
|
||
return err
|
||
}
|
||
user.CaptchaMessage = result.ID
|
||
|
||
d.NewUser(ctx, user)
|
||
return nil
|
||
}
|
||
|
||
func userLeft(c tb.Context) error {
|
||
bot := c.Bot()
|
||
message := c.Message()
|
||
sender := c.Sender()
|
||
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)
|
||
bot.Delete(&tb.Message{Chat: message.Chat, ID: user.CaptchaMessage})
|
||
}
|
||
return nil
|
||
}
|
||
|
||
var HandlersV1 = []Handler{
|
||
{
|
||
Endpoint: tb.OnText,
|
||
Handler: func(c tb.Context) error {
|
||
sender := c.Sender()
|
||
message := c.Message()
|
||
bot := c.Bot()
|
||
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 {
|
||
text := message.Text
|
||
solved := false
|
||
if num, err := strconv.Atoi(text); err == nil {
|
||
if num == int(user.CorrectAnswer) {
|
||
_ = d.RemoveUser(ctx, user)
|
||
solved = true
|
||
bot.Delete(message)
|
||
bot.Delete(&tb.Message{Chat: message.Chat, ID: user.CaptchaMessage})
|
||
}
|
||
} else {
|
||
log.Print(err)
|
||
}
|
||
if !solved {
|
||
bot.Delete(message)
|
||
bot.Delete(&tb.Message{Chat: message.Chat, ID: user.CaptchaMessage})
|
||
bot.Ban(message.Chat, &tb.ChatMember{User: sender})
|
||
_ = d.RemoveUser(ctx, user)
|
||
}
|
||
}
|
||
return nil
|
||
},
|
||
},
|
||
// {
|
||
// Endpoint: "/gen",
|
||
// Handler: func(c tb.Context) error {
|
||
// captcha := captchagen.GenCaptcha()
|
||
// reader := captcha.ToReader()
|
||
// caption := fmt.Sprintf("Правильный ответ: %d", captcha.CorrectAnswer)
|
||
// c.Reply(&tb.Photo{File: tb.FromReader(reader), Caption: caption})
|
||
// return nil
|
||
// },
|
||
// },
|
||
{
|
||
Endpoint: tb.OnAddedToGroup,
|
||
Handler: func(c tb.Context) error {
|
||
m := c.Message()
|
||
chat := db.Chat{
|
||
Id: m.Chat.ID,
|
||
Title: m.Chat.Title,
|
||
}
|
||
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
|
||
},
|
||
},
|
||
|
||
{
|
||
Endpoint: tb.OnUserJoined,
|
||
Handler: userJoined,
|
||
},
|
||
{
|
||
Endpoint: tb.OnUserLeft,
|
||
Handler: userLeft,
|
||
},
|
||
}
|