KickerBot/kicker/handlers.go

117 lines
3.1 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,
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)
str := fmt.Sprintf("%v", user)
2022-01-28 01:00:40 +03:00
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
2021-12-07 01:18:44 +03:00
db.Log("new user", str)
2022-02-02 00:37:58 +03:00
d.NewUser(ctx, user)
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: func(c tb.Context) error {
2022-02-02 00:37:58 +03:00
sender := c.Sender()
message := c.Message()
2022-02-02 00:53:12 +03:00
bot := c.Bot()
2022-02-02 00:37:58 +03:00
d := db.GetDatabase()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
2022-02-02 00:53:12 +03:00
if user, err := d.GetUser(ctx, db.User{Id: sender.ID, ChatId: message.Chat.ID}); err == nil {
2022-02-02 00:37:58 +03:00
text := message.Text
2022-02-02 00:53:12 +03:00
solved := false
2022-02-02 00:37:58 +03:00
if num, err := strconv.Atoi(text); err == nil {
if num == int(user.CorrectAnswer) {
_ = d.RemoveUser(ctx, user)
2022-02-02 00:53:12 +03:00
solved = true
bot.Delete(message)
bot.Delete(&tb.Message{Chat: message.Chat, ID: user.CaptchaMessage})
2022-02-02 00:37:58 +03:00
}
} else {
log.Print(err)
}
2022-02-02 00:53:12 +03:00
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)
2022-02-02 00:53:12 +03:00
}
2022-02-02 00:37:58 +03:00
}
2021-12-07 01:18:44 +03:00
return nil
},
},
2022-01-28 01:00:40 +03:00
{
Endpoint: "/gen",
Handler: func(c tb.Context) error {
captcha := captchagen.GenCaptcha()
2022-02-02 00:37:58 +03:00
reader := captcha.ToReader()
2022-01-28 01:00:40 +03:00
caption := fmt.Sprintf("Правильный ответ: %d", captcha.CorrectAnswer)
c.Reply(&tb.Photo{File: tb.FromReader(reader), Caption: caption})
return nil
},
},
2021-12-03 12:19:55 +03:00
{
Endpoint: tb.OnAddedToGroup,
2021-12-03 12:19:55 +03:00
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)
}
2021-12-07 01:18:44 +03:00
db.Log("new chat", chat)
return nil
},
},
2021-12-07 01:18:44 +03:00
{
Endpoint: tb.OnUserJoined,
Handler: userJoined,
},
2021-12-03 12:19:55 +03:00
}