From 149644173a155de864de8a6f03dba0d3e9863d7f Mon Sep 17 00:00:00 2001 From: nefrace Date: Fri, 10 Feb 2023 17:00:50 +0300 Subject: [PATCH] Karma reactions and temporary messages with `waitAndDelete` --- handlers.go | 28 ++++++++++++++++++++++------ main.go | 3 +++ middleware.go | 2 ++ types.go | 7 +++++++ utils.go | 12 +++++++++++- 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/handlers.go b/handlers.go index ad2414b..5e3c1a2 100644 --- a/handlers.go +++ b/handlers.go @@ -14,6 +14,15 @@ func handleKarma(u *nechotron.Update) error { 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 + } good, bad := GetTriggers() value := 0 text := u.Text() @@ -27,9 +36,10 @@ func handleKarma(u *nechotron.Update) error { fromKarma, _ := store.Count(u.Ctx, tongo.E("to", from.ID)) totalFromKarma := from.KarmaOffset + fromKarma if totalFromKarma < 0 { - _, err := u.AnswerText( + 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) @@ -42,15 +52,21 @@ func handleKarma(u *nechotron.Update) error { return err } if recentShots > 0 { - u.DeleteMessage() + // 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) - }() + 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, diff --git a/main.go b/main.go index 1c53723..f3c74b6 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "git.nefrace.ru/nefrace/tongo" "github.com/NicoNex/echotron/v3" "github.com/joho/godotenv" + "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) @@ -41,5 +42,7 @@ func main() { func createIndexes(db *tongo.Database) error { userStore := tongo.NewStore[User](db) _, 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 } diff --git a/middleware.go b/middleware.go index e18f30a..bc949de 100644 --- a/middleware.go +++ b/middleware.go @@ -11,10 +11,12 @@ import ( func ExecTimeLogger(next nechotron.UpdateHandler) nechotron.UpdateHandler { return func(u *nechotron.Update) error { + log.Printf("=== EXECUTING UPDATE [%s] ===", u.UpdateID.String()) start := time.Now() err := next(u) t := time.Since(start) log.Println("Update was handled in %d microseconds", t.Microseconds()) + log.Printf("=== END OF [%s] ===", u.UpdateID.String()) return err } } diff --git a/types.go b/types.go index 0790a14..59164d8 100644 --- a/types.go +++ b/types.go @@ -61,6 +61,13 @@ type KarmaShot struct { 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" } type Warn struct { diff --git a/utils.go b/utils.go index 445f003..f48f1ba 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,11 @@ package main -import "strings" +import ( + "strings" + "time" + + "git.nefrace.ru/nefrace/nechotron" +) func StringHasAny(s string, subs ...string) bool { for _, sub := range subs { @@ -27,3 +32,8 @@ func StringHasSuffix(s string, subs ...string) bool { } return false } + +func waitAndDelete(u *nechotron.Update, chatId int64, messageId int) { + time.Sleep(10 * time.Second) + u.Bot.DeleteMessage(chatId, messageId) +}