From d1635828672b10434b6925c477b61b10446a1b4f Mon Sep 17 00:00:00 2001 From: Satont Date: Sat, 26 Aug 2023 09:15:41 +0300 Subject: [PATCH] fix(events): do not return if responses empty --- .../internal/bots/handlers/message_keywods.go | 78 ++++++++++--------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/apps/bots/internal/bots/handlers/message_keywods.go b/apps/bots/internal/bots/handlers/message_keywods.go index e9659c329..43877aa4b 100644 --- a/apps/bots/internal/bots/handlers/message_keywods.go +++ b/apps/bots/internal/bots/handlers/message_keywods.go @@ -78,6 +78,7 @@ func (c *Handlers) handleKeywords( query := make(map[string]any) + var responses []string if !isOnCooldown && k.Response != "" { requestStruct := &parser.ParseTextRequestData{ Channel: &parser.Channel{ @@ -104,51 +105,52 @@ func (c *Handlers) handleKeywords( slog.Any("err", err), slog.String("channelId", msg.Channel.ID), ) - return } - _, err = c.eventsGrpc.KeywordMatched( - context.Background(), &events.KeywordMatchedMessage{ - BaseInfo: &events.BaseInfo{ChannelId: msg.Channel.ID}, - KeywordId: k.ID, - KeywordName: k.Text, - KeywordResponse: strings.Join(res.Responses, " "), - UserId: msg.User.ID, - UserName: msg.User.Name, - UserDisplayName: msg.User.DisplayName, - }, + responses = res.Responses + } + + _, err = c.eventsGrpc.KeywordMatched( + context.Background(), &events.KeywordMatchedMessage{ + BaseInfo: &events.BaseInfo{ChannelId: msg.Channel.ID}, + KeywordId: k.ID, + KeywordName: k.Text, + KeywordResponse: strings.Join(responses, " "), + UserId: msg.User.ID, + UserName: msg.User.Name, + UserDisplayName: msg.User.DisplayName, + }, + ) + if err != nil { + c.logger.Error( + "cannot send keywords matched event", + slog.Any("err", err), + slog.String("channelId", msg.Channel.ID), + slog.String("userId", msg.User.ID), ) - if err != nil { - c.logger.Error( - "cannot send keywords matched event", - slog.Any("err", err), - slog.String("channelId", msg.Channel.ID), - slog.String("userId", msg.User.ID), - ) - } + } - for _, r := range res.Responses { - validateResponseErr := ValidateResponseSlashes(r) - if validateResponseErr != nil { - c.BotClient.SayWithRateLimiting( - msg.Channel.Name, - validateResponseErr.Error(), - nil, - ) - } else { - c.BotClient.SayWithRateLimiting( - msg.Channel.Name, - r, - lo.If(k.IsReply, &msg.ID).Else(nil), - ) - } + for _, r := range responses { + validateResponseErr := ValidateResponseSlashes(r) + if validateResponseErr != nil { + c.BotClient.SayWithRateLimiting( + msg.Channel.Name, + validateResponseErr.Error(), + nil, + ) + } else { + c.BotClient.SayWithRateLimiting( + msg.Channel.Name, + r, + lo.If(k.IsReply, &msg.ID).Else(nil), + ) } - - query["cooldownExpireAt"] = time.Now(). - Add(time.Duration(k.Cooldown) * time.Second). - UTC() } + query["cooldownExpireAt"] = time.Now(). + Add(time.Duration(k.Cooldown) * time.Second). + UTC() + query["usages"] = k.Usages + timesInMessage err = c.db.Model(&k).Where("id = ?", k.ID).Select("*").Updates(query).Error if err != nil {