Karma reactions and temporary messages with `waitAndDelete`

This commit is contained in:
nefrace 2023-02-10 17:00:50 +03:00
parent 2a29f0b89d
commit 149644173a
5 changed files with 45 additions and 7 deletions

View File

@ -14,6 +14,15 @@ func handleKarma(u *nechotron.Update) error {
to, _ := u.Ctx.Value("userto").(*User) to, _ := u.Ctx.Value("userto").(*User)
mentionFrom := nechotron.UserMention(u.Message.From) mentionFrom := nechotron.UserMention(u.Message.From)
mentionTo := nechotron.UserMention(u.Message.ReplyToMessage.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
}
good, bad := GetTriggers() good, bad := GetTriggers()
value := 0 value := 0
text := u.Text() text := u.Text()
@ -27,9 +36,10 @@ func handleKarma(u *nechotron.Update) error {
fromKarma, _ := store.Count(u.Ctx, tongo.E("to", from.ID)) fromKarma, _ := store.Count(u.Ctx, tongo.E("to", from.ID))
totalFromKarma := from.KarmaOffset + fromKarma totalFromKarma := from.KarmaOffset + fromKarma
if totalFromKarma < 0 { if totalFromKarma < 0 {
_, err := u.AnswerText( res, err := u.AnswerText(
fmt.Sprintf("У тебя слишком маленькая карма *\\(%d\\), чтобы менять её другим\\.", totalFromKarma), fmt.Sprintf("У тебя слишком маленькая карма *\\(%d\\), чтобы менять её другим\\.", totalFromKarma),
&echotron.MessageOptions{ParseMode: echotron.MarkdownV2, ReplyToMessageID: u.MessageID()}) &echotron.MessageOptions{ParseMode: echotron.MarkdownV2, ReplyToMessageID: u.MessageID()})
go waitAndDelete(u, u.ChatID(), res.Result.ID)
return err return err
} }
timeThreshold := time.Now().Add(1 * -time.Minute) timeThreshold := time.Now().Add(1 * -time.Minute)
@ -42,15 +52,21 @@ func handleKarma(u *nechotron.Update) error {
return err return err
} }
if recentShots > 0 { if recentShots > 0 {
u.DeleteMessage() // u.DeleteMessage()
res, err := u.AnswerMarkdown( res, err := u.AnswerMarkdown(
fmt.Sprintf("*%s*, ты только недавно менял карму *%s*\\. Подожди минуту\\.", mentionFrom, mentionTo)) fmt.Sprintf("*%s*, ты только недавно менял карму *%s*\\. Подожди минуту\\.", mentionFrom, mentionTo))
go func() { go waitAndDelete(u, u.ChatID(), res.Result.ID)
time.Sleep(10 * time.Second)
u.Bot.DeleteMessage(u.ChatID(), res.Result.ID)
}()
return err 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{ newShot := KarmaShot{
Item: tongo.NewID(), Item: tongo.NewID(),
From: from.ID, From: from.ID,

View File

@ -9,6 +9,7 @@ import (
"git.nefrace.ru/nefrace/tongo" "git.nefrace.ru/nefrace/tongo"
"github.com/NicoNex/echotron/v3" "github.com/NicoNex/echotron/v3"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
@ -41,5 +42,7 @@ func main() {
func createIndexes(db *tongo.Database) error { func createIndexes(db *tongo.Database) error {
userStore := tongo.NewStore[User](db) userStore := tongo.NewStore[User](db)
_, err := userStore.Coll.Indexes().CreateOne(context.Background(), userIndex, options.CreateIndexes()) _, err := userStore.Coll.Indexes().CreateOne(context.Background(), userIndex, options.CreateIndexes())
karmaShotStore := tongo.NewStore[KarmaShot](db)
_, err = karmaShotStore.Coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{karmaFromIndex, karmaToIndex}, options.CreateIndexes())
return err return err
} }

View File

@ -11,10 +11,12 @@ import (
func ExecTimeLogger(next nechotron.UpdateHandler) nechotron.UpdateHandler { func ExecTimeLogger(next nechotron.UpdateHandler) nechotron.UpdateHandler {
return func(u *nechotron.Update) error { return func(u *nechotron.Update) error {
log.Printf("=== EXECUTING UPDATE [%s] ===", u.UpdateID.String())
start := time.Now() start := time.Now()
err := next(u) err := next(u)
t := time.Since(start) t := time.Since(start)
log.Println("Update was handled in %d microseconds", t.Microseconds()) log.Println("Update was handled in %d microseconds", t.Microseconds())
log.Printf("=== END OF [%s] ===", u.UpdateID.String())
return err return err
} }
} }

View File

@ -61,6 +61,13 @@ type KarmaShot struct {
When time.Time When time.Time
} }
var karmaFromIndex = mongo.IndexModel{
Keys: tongo.D(tongo.E("from", 1)),
}
var karmaToIndex = mongo.IndexModel{
Keys: tongo.D(tongo.E("to", 1)),
}
func (KarmaShot) Coll() string { return "karma" } func (KarmaShot) Coll() string { return "karma" }
type Warn struct { type Warn struct {

View File

@ -1,6 +1,11 @@
package main package main
import "strings" import (
"strings"
"time"
"git.nefrace.ru/nefrace/nechotron"
)
func StringHasAny(s string, subs ...string) bool { func StringHasAny(s string, subs ...string) bool {
for _, sub := range subs { for _, sub := range subs {
@ -27,3 +32,8 @@ func StringHasSuffix(s string, subs ...string) bool {
} }
return false return false
} }
func waitAndDelete(u *nechotron.Update, chatId int64, messageId int) {
time.Sleep(10 * time.Second)
u.Bot.DeleteMessage(chatId, messageId)
}