KickerBot/kicker/handlers.go

130 lines
3.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"
2021-12-03 12:19:55 +03:00
tb "gopkg.in/tucnak/telebot.v3"
)
func userJoined(c tb.Context) error {
2022-02-02 00:37:58 +03:00
bot := c.Bot()
captcha := captchagen.GenCaptcha()
reader := captcha.ToReader()
message := c.Message()
user := db.User{
2022-02-02 00:37:58 +03:00
Id: message.Sender.ID,
Username: message.Sender.Username,
FirstName: message.Sender.FirstName,
LastName: message.Sender.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)
msg := fmt.Sprintf("Приветствую тебя, %v!\nДля подтверждения, что ты человек, выбери логотип движка, которому посвящен данный чат, и отправь его номер сюда.\nЯ дам тебе две минуты на это.", user.FirstName)
2022-02-02 00:37:58 +03:00
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
}
2022-02-04 11:37:55 +03:00
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})
bot.Delete(&tb.Message{Chat: message.Chat, ID: user.JoinedMessage})
2022-02-04 11:37:55 +03:00
}
return nil
}
func checkCaptcha(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_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
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.Delete(&tb.Message{Chat: message.Chat, ID: user.JoinedMessage})
bot.Ban(message.Chat, &tb.ChatMember{User: sender})
_ = d.RemoveUser(ctx, user)
}
}
return nil
}
func botAdded(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
}
2021-12-03 12:19:55 +03:00
var HandlersV1 = []Handler{
2021-12-07 01:18:44 +03:00
{
Endpoint: tb.OnText,
Handler: checkCaptcha,
2021-12-07 01:18:44 +03:00
},
2021-12-03 12:19:55 +03:00
{
Endpoint: tb.OnAddedToGroup,
Handler: botAdded,
},
2021-12-07 01:18:44 +03:00
{
Endpoint: tb.OnUserJoined,
Handler: userJoined,
},
2022-02-04 11:37:55 +03:00
{
Endpoint: tb.OnUserLeft,
Handler: userLeft,
},
2021-12-03 12:19:55 +03:00
}