Karma reactions and temporary messages with `waitAndDelete`
This commit is contained in:
parent
2a29f0b89d
commit
149644173a
28
handlers.go
28
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,
|
||||
|
|
3
main.go
3
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
7
types.go
7
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 {
|
||||
|
|
12
utils.go
12
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue