go-dette/handlers.go

74 lines
2.2 KiB
Go
Raw Normal View History

package main
2023-01-24 23:17:30 +03:00
import (
"fmt"
"time"
"git.nefrace.ru/nefrace/nechotron"
"git.nefrace.ru/nefrace/tongo"
"github.com/NicoNex/echotron/v3"
)
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)
good, bad := GetTriggers()
value := 0
text := u.Text()
if StringHasPrefix(text, good...) {
value = 1
}
if StringHasPrefix(text, bad...) {
value = -1
}
store := tongo.NewStore[KarmaShot](db)
fromKarma, _ := store.Count(u.Ctx, tongo.E("to", from.ID))
totalFromKarma := from.KarmaOffset + fromKarma
if totalFromKarma < 0 {
_, err := u.AnswerText(
fmt.Sprintf("У тебя слишком маленькая карма *\\(%d\\), чтобы менять её другим\\.", totalFromKarma),
&echotron.MessageOptions{ParseMode: echotron.MarkdownV2, ReplyToMessageID: u.MessageID()})
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()
res, err := u.AnswerMarkdown(
fmt.Sprintf("*%s*, ты только недавно менял карму *%s*\\. Подожди минуту\\.", mentionFrom, mentionTo))
go func() {
time.Sleep(10 * time.Second)
u.Bot.DeleteMessage(u.ChatID(), res.Result.ID)
}()
return err
}
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
}