KickerBot/kicker/kicker.go

121 lines
2.5 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 (
2022-11-09 00:45:08 +03:00
"context"
"kickerbot/db"
2021-12-03 12:19:55 +03:00
"log"
2022-11-09 00:45:08 +03:00
"strings"
2021-12-03 12:19:55 +03:00
"time"
2023-02-14 00:50:21 +03:00
"git.nefrace.ru/nefrace/tongo"
"github.com/NicoNex/echotron/v3"
2021-12-03 12:19:55 +03:00
)
2023-02-14 00:50:21 +03:00
var Client *tongo.Database
2022-11-09 00:45:08 +03:00
type bot struct {
chatID int64
CaptchaTopic int64
2023-02-14 00:50:21 +03:00
Me *echotron.User
echotron.API
2022-11-09 00:45:08 +03:00
}
2023-02-14 00:50:21 +03:00
func (b *bot) Update(update *echotron.Update) {
2022-11-09 01:23:32 +03:00
if update.Message != nil {
if len(update.Message.NewChatMembers) != 0 {
for _, user := range update.Message.NewChatMembers {
if user.ID == b.Me.ID {
botAdded(b, update)
}
2022-11-09 00:45:08 +03:00
}
2022-11-09 01:23:32 +03:00
userJoined(b, update)
return
2022-11-09 00:45:08 +03:00
}
2022-11-09 01:23:32 +03:00
if update.Message.LeftChatMember != nil {
userLeft(b, update)
2022-11-09 00:45:08 +03:00
return
}
2022-11-09 01:23:32 +03:00
if update.Message.Text != "" {
if update.Message.Text == "/settopic" {
setTopic(b, update)
return
}
checkCaptcha(b, update)
}
2022-11-09 00:45:08 +03:00
}
2021-12-03 12:19:55 +03:00
}
2021-12-03 14:35:07 +03:00
// Базовая структура для бота
2021-12-03 14:30:10 +03:00
type Kicker struct {
2022-11-09 00:45:08 +03:00
Token string
2023-02-14 00:50:21 +03:00
Dispatcher *echotron.Dispatcher
2021-12-03 12:19:55 +03:00
}
2023-02-14 00:50:21 +03:00
func (b *Kicker) NewBot(chatID int64) echotron.Bot {
2022-11-09 00:45:08 +03:00
d := db.GetDatabase()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
chat := db.Chat{
2023-02-14 00:50:21 +03:00
ChatId: chatID,
2022-11-09 00:45:08 +03:00
Title: "",
TopicId: 0,
}
if !d.ChatExists(ctx, chat) {
if err := d.NewChat(ctx, chat); err != nil {
return &bot{}
}
}
chat, _ = d.GetChat(ctx, chatID)
CaptchaTopic := chat.TopicId
result := &bot{
chatID,
CaptchaTopic,
nil,
2023-02-14 00:50:21 +03:00
echotron.NewAPI(b.Token),
2022-11-09 00:45:08 +03:00
}
me, err := result.GetMe()
2021-12-03 12:19:55 +03:00
if err != nil {
2022-11-09 00:45:08 +03:00
log.Println(err)
2021-12-03 12:19:55 +03:00
}
2022-11-09 00:45:08 +03:00
result.Me = me.Result
return result
}
// Initialize bot with token
func (b *Kicker) Init() error {
2023-02-14 00:50:21 +03:00
dsp := echotron.NewDispatcher(b.Token, b.NewBot)
2022-11-09 00:45:08 +03:00
b.Dispatcher = dsp
2021-12-03 12:19:55 +03:00
return nil
}
2022-11-09 00:45:08 +03:00
func (b *Kicker) Start() error {
return b.Dispatcher.Poll()
}
2023-02-14 00:50:21 +03:00
func EscapeText(parseMode echotron.ParseMode, text string) string {
2022-11-09 00:45:08 +03:00
var replacer *strings.Replacer
2023-02-14 00:50:21 +03:00
if parseMode == echotron.HTML {
2022-11-09 00:45:08 +03:00
replacer = strings.NewReplacer("<", "&lt;", ">", "&gt;", "&", "&amp;")
2023-02-14 00:50:21 +03:00
} else if parseMode == echotron.Markdown {
2022-11-09 00:45:08 +03:00
replacer = strings.NewReplacer("_", "\\_", "*", "\\*", "`", "\\`", "[", "\\[")
2023-02-14 00:50:21 +03:00
} else if parseMode == echotron.MarkdownV2 {
2022-11-09 00:45:08 +03:00
replacer = strings.NewReplacer(
"_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(",
"\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>",
"#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|",
"\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!",
)
} else {
return ""
2021-12-03 12:19:55 +03:00
}
2022-11-09 00:45:08 +03:00
return replacer.Replace(text)
2021-12-03 12:19:55 +03:00
}
2023-02-14 00:50:21 +03:00
func waitAndDelete(b *echotron.API, message *echotron.Message, t time.Duration) {
time.Sleep(t)
b.DeleteMessage(message.Chat.ID, message.ID)
}