112 lines
3.3 KiB
Go
112 lines
3.3 KiB
Go
package main
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"time"
|
||
|
||
"git.nefrace.ru/nefrace/nechotron"
|
||
"git.nefrace.ru/nefrace/tongo"
|
||
"github.com/NicoNex/echotron/v3"
|
||
)
|
||
|
||
// Returns slices of good and bad triggers
|
||
func GetTriggers() map[string]int {
|
||
return map[string]int{
|
||
"+": 1,
|
||
"-": -1,
|
||
}
|
||
}
|
||
|
||
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)
|
||
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(
|
||
fmt.Sprintf("У тебя слишком маленькая карма *\\(%d\\), чтобы менять её другим\\.", totalFromKarma),
|
||
&echotron.MessageOptions{ParseMode: echotron.MarkdownV2, ReplyToMessageID: u.MessageID()})
|
||
go waitAndDelete(u, u.ChatID(), res.Result.ID)
|
||
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 waitAndDelete(u, u.ChatID(), res.Result.ID)
|
||
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
|
||
}
|
||
}
|
||
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
|
||
}
|
||
|
||
var topText = `
|
||
*Наш ТОП\-10 пользователей:*
|
||
|
||
%s
|
||
`
|
||
|
||
func handleTop(u *nechotron.Update, _ string) error {
|
||
store := tongo.NewStore[RatedUser](db)
|
||
users, err := store.GetMany(u.Ctx)
|
||
if err != nil {
|
||
return errors.Join(errors.New("can't get top10 users"), err)
|
||
}
|
||
text := ""
|
||
for n, u := range users {
|
||
text += fmt.Sprintf("%d \\- %s \\(%d\\)\n", n, nechotron.EscapeMd2(u.Name), u.TotalKarma)
|
||
}
|
||
text = fmt.Sprintf(topText, text)
|
||
u.AnswerMarkdown(text)
|
||
return nil
|
||
}
|