go-dette/handlers.go

210 lines
6.6 KiB
Go
Raw Normal View History

package main
2023-01-24 23:17:30 +03:00
import (
2023-04-13 11:13:24 +03:00
"encoding/json"
2023-01-24 23:17:30 +03:00
"fmt"
2023-06-07 21:21:54 +03:00
"log"
2023-04-13 11:13:24 +03:00
"math/rand"
"net/http"
"net/url"
2023-06-08 01:44:38 +03:00
"strings"
2023-01-24 23:17:30 +03:00
"time"
"git.nefrace.ru/nefrace/nechotron"
"git.nefrace.ru/nefrace/tongo"
"github.com/NicoNex/echotron/v3"
)
2023-06-08 01:44:38 +03:00
var docApiURL = "https://docs.godotengine.org/_/api/v2/search/?q=%s&project=godot&version=%s&language=en"
2023-04-13 11:13:24 +03:00
var docURL = "https://docs.godotengine.org/ru/stable/search.html?q=%s"
type DocResponse struct {
Count uint `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []DocResult `json:"results"`
}
type DocResult struct {
Title string `json:"title"`
Domain string `json:"domain"`
Path string `json:"path"`
Highlights DocHighlights `json:"highlights"`
}
type DocHighlights struct {
Title []string `json:"title"`
}
2023-06-08 01:44:38 +03:00
func getDocs(topic string, version string) (string, error) {
2023-04-13 11:13:24 +03:00
topic_escaped := nechotron.EscapeMd2(topic)
2023-06-07 21:21:54 +03:00
not_found := fmt.Sprintf("Извините, по запросу *%s* ничего не найдено.", topic_escaped)
2023-06-08 01:44:38 +03:00
req, err := url.ParseRequestURI(fmt.Sprintf(docApiURL, url.QueryEscape(topic), version))
2023-04-13 11:13:24 +03:00
if err != nil {
2023-06-07 21:21:54 +03:00
return not_found, err
2023-04-13 11:13:24 +03:00
}
result, err := http.Get(req.String())
if err != nil {
2023-06-07 21:21:54 +03:00
return not_found, err
2023-04-13 11:13:24 +03:00
}
defer result.Body.Close()
var response DocResponse
err = json.NewDecoder(result.Body).Decode(&response)
if err != nil {
2023-06-07 21:21:54 +03:00
return not_found, err
2023-04-13 11:13:24 +03:00
}
textResults := ""
for i, r := range response.Results {
if i > 9 {
break
}
text := nechotron.EscapeMd2(r.Title)
link, _ := url.JoinPath(r.Domain, r.Path)
textResults += fmt.Sprintf("%d\\. [%s](%s)\n", i+1, text, link)
}
2023-06-08 01:44:38 +03:00
text := fmt.Sprintf("Вот что я нашла по запросу *%s* \\(для версии `%s`\\): \n\n%s", topic_escaped, version, textResults)
2023-06-07 21:21:54 +03:00
return text, nil
}
func handleDocRequest(u *nechotron.Update) error {
topic := u.Ctx.Value(nechotron.FilteredValue("docTopic")).(string)
kb := nechotron.NewInlineKeyboard().
Row(
nechotron.InButtonCallback("Дай для 3.5", fmt.Sprintf("docs3:%s", topic)),
2023-06-08 01:44:38 +03:00
nechotron.InButtonCallback("А на русском можно?", "rudocs")).
2023-06-07 21:21:54 +03:00
Row(nechotron.InButtonURL("Поищу сам", fmt.Sprintf(docURL, url.QueryEscape(topic)))).
Row(nechotron.InButtonCallback("Спасибо, не надо", "delete"))
opts := nechotron.NewOptions().
MarkdownV2().
ReplyTo(u.MessageID()).
ReplyMarkup(kb.Markup()).
MessageOptions()
2023-06-08 01:44:38 +03:00
text, docerr := getDocs(topic, "latest")
2023-06-07 21:21:54 +03:00
if docerr != nil {
log.Println("Can't get docs: ", docerr)
}
_, err := u.AnswerText(text, opts)
2023-04-13 11:13:24 +03:00
return err
}
2023-06-08 01:44:38 +03:00
func handleDocRequest3(u *nechotron.Update) error {
topic := strings.TrimPrefix(u.Callback(), "docs3:")
kb := nechotron.NewInlineKeyboard().
Row(
nechotron.InButtonCallback("А на русском можно?", "rudocs")).
Row(nechotron.InButtonURL("Поищу сам", fmt.Sprintf(docURL, url.QueryEscape(topic)))).
Row(nechotron.InButtonCallback("Спасибо, не надо", "delete"))
opts := nechotron.NewOptions().
MarkdownV2().
ReplyMarkup(kb.Markup()).
MessageTextOptions()
text, docerr := getDocs(topic, "3.5")
if docerr != nil {
log.Println("Can't get docs: ", docerr)
}
_, err := u.EditText(text, opts)
return err
}
2023-01-24 23:17:30 +03:00
func handleKarma(u *nechotron.Update) error {
from, _ := u.Ctx.Value("userfrom").(*User)
to, _ := u.Ctx.Value("userto").(*User)
mentionFrom := nechotron.UserMention(u.Message.From)
mentionTo := nechotron.UserMention(u.Message.ReplyToMessage.From)
if from.ID == to.ID {
res, err := u.AnswerMarkdown(
fmt.Sprintf("Лайкать себя \\- плохая затея, *%s*", mentionFrom))
go func() {
time.Sleep(10 * time.Second)
u.Bot.DeleteMessage(u.ChatID(), res.Result.ID)
}()
return err
}
value := u.Ctx.Value(nechotron.FilteredValue("karmaValue")).(int)
// trigger := u.Ctx.Value(nechotron.FilteredValue("karmaTrigger")).(string)
2023-01-24 23:17:30 +03:00
store := tongo.NewStore[KarmaShot](db)
fromKarma, _ := store.Count(u.Ctx, tongo.E("to", from.ID))
totalFromKarma := from.KarmaOffset + fromKarma
if totalFromKarma < 0 {
res, err := u.AnswerText(
2023-01-24 23:17:30 +03:00
fmt.Sprintf("У тебя слишком маленькая карма *\\(%d\\), чтобы менять её другим\\.", totalFromKarma),
&echotron.MessageOptions{ParseMode: echotron.MarkdownV2, ReplyToMessageID: u.MessageID()})
go waitAndDelete(u, u.ChatID(), res.Result.ID)
2023-01-24 23:17:30 +03:00
return err
}
timeThreshold := time.Now().Add(1 * -time.Minute)
recentShots, err := store.Count(
u.Ctx,
tongo.E("from", from.ID),
tongo.E("to", to.ID),
tongo.E("when", tongo.D(tongo.E("$gte", timeThreshold))))
if err != nil {
return err
}
if recentShots > 0 {
// u.DeleteMessage()
2023-01-24 23:17:30 +03:00
res, err := u.AnswerMarkdown(
fmt.Sprintf("*%s*, ты только недавно менял карму *%s*\\. Подожди минуту\\.", mentionFrom, mentionTo))
go waitAndDelete(u, u.ChatID(), res.Result.ID)
2023-01-24 23:17:30 +03:00
return err
}
if to.ID == u.Bot.Me.ID {
if value > 0 {
u.AnswerMarkdown("*_Ой, это мне?_*")
} else {
res, err := u.AnswerMarkdown("*_Кажется, вы ошиблись адресатом :/_*")
go waitAndDelete(u, u.ChatID(), res.Result.ID)
return err
}
}
2023-01-24 23:17:30 +03:00
newShot := KarmaShot{
Item: tongo.NewID(),
From: from.ID,
To: to.ID,
MessageText: nechotron.GetText(u.Message.ReplyToMessage),
When: time.Now(),
Count: value,
}
store.InsertOne(u.Ctx, &newShot)
newKarma, _ := store.Count(u.Ctx, tongo.E("to", to.ID))
totalToKarma := to.KarmaOffset + newKarma
changeText := "повысил"
if value < 0 {
changeText = "понизил"
}
_, err = u.AnswerMarkdown(
fmt.Sprintf("*%s \\(%d\\)* только что %s карму *%s \\(%d\\)*", mentionFrom, totalFromKarma, changeText, mentionTo, totalToKarma))
return err
2023-04-13 11:13:24 +03:00
}
2023-04-13 11:13:24 +03:00
func handleOfftop(u *nechotron.Update) error {
text := "Держите [ссылку на оффтоп](%s)\\!"
offtopLink := "https://t.me/Godot_Engine_Offtop"
offtopAnswers, err := tongo.NewStore[TriggerText](db).GetMany(u.Ctx, tongo.E("trigger", "offtop"))
if err == nil {
if len(offtopAnswers) > 0 {
text = offtopAnswers[rand.Intn(len(offtopAnswers)-1)].Text
}
}
offtopUrl, err := tongo.NewStore[Config](db).GetOne(u.Ctx, tongo.E("name", "offtop_url"))
if err == nil {
offtopLink = offtopUrl.Value.(string)
}
text = fmt.Sprintf(text, offtopLink)
kb := nechotron.NewInlineKeyboard().Row(nechotron.InButtonCallback("Спасибо, не надо", "delete")).Markup()
opts := nechotron.NewOptions().
MarkdownV2().
ReplyTo(u.MessageID()).
ReplyMarkup(kb).
MessageOptions()
u.AnswerText(text, opts)
return nil
}
func handleDeleteCallback(u *nechotron.Update) error {
u.DeleteMessage()
return nil
}