Skip to content

Commit

Permalink
feat(bots): trigger handlers on user notice message
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed Dec 30, 2022
1 parent 66e178c commit 70ea639
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 40 deletions.
33 changes: 32 additions & 1 deletion apps/bots/internal/bots/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,40 @@ func newBot(opts *ClientOpts) *types.BotClient {
defer messagesCounter.Inc()
botHandlers.OnUserStateMessage(message)
})
client.OnUserNoticeMessage(func(message irc.UserNoticeMessage) {
botHandlers.OnMessage(handlers.Message{
ID: message.ID,
Channel: handlers.MessageChannel{
ID: message.RoomID,
Name: message.Channel,
},
User: handlers.MessageUser{
ID: message.User.ID,
Name: message.User.Name,
DisplayName: message.User.DisplayName,
Badges: message.User.Badges,
},
Message: message.Message,
Emotes: message.Emotes,
})
})
client.OnPrivateMessage(func(message irc.PrivateMessage) {
defer messagesCounter.Inc()
botHandlers.OnPrivateMessage(message)
botHandlers.OnMessage(handlers.Message{
ID: message.ID,
Channel: handlers.MessageChannel{
ID: message.RoomID,
Name: message.Channel,
},
User: handlers.MessageUser{
ID: message.User.ID,
Name: message.User.Name,
DisplayName: message.User.DisplayName,
Badges: message.User.Badges,
},
Message: message.Message,
Emotes: message.Emotes,
})
})

err = client.Connect()
Expand Down
9 changes: 4 additions & 5 deletions apps/bots/internal/bots/handlers/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package handlers
import (
"strings"

irc "github.com/gempir/go-twitch-irc/v3"
"github.com/samber/lo"
"github.com/satont/tsuwari/apps/bots/internal/bots/handlers/messages"
)

func (c *Handlers) OnPrivateMessage(msg irc.PrivateMessage) {
func (c *Handlers) OnMessage(msg Message) {
userBadges := createUserBadges(msg.User.Badges)

splittedMsg := strings.Split(msg.Message, " ")
Expand All @@ -27,11 +26,11 @@ func (c *Handlers) OnPrivateMessage(msg irc.PrivateMessage) {
go c.handleKeywords(msg, userBadges)

go func() {
messages.IncrementUserMessages(c.db, msg.User.ID, msg.RoomID)
messages.IncrementUserMessages(c.db, msg.User.ID, msg.Channel.ID)
messages.StoreMessage(
c.db,
msg.ID,
msg.RoomID,
msg.Channel.ID,
msg.User.ID,
msg.User.Name,
msg.Message,
Expand All @@ -41,7 +40,7 @@ func (c *Handlers) OnPrivateMessage(msg irc.PrivateMessage) {
),
)
}()
go messages.IncrementStreamParsedMessages(c.db, msg.RoomID)
go messages.IncrementStreamParsedMessages(c.db, msg.Channel.ID)
}

func createUserBadges(badges map[string]int) []string {
Expand Down
13 changes: 6 additions & 7 deletions apps/bots/internal/bots/handlers/message_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package handlers
import (
"context"

irc "github.com/gempir/go-twitch-irc/v3"
"github.com/samber/lo"
"github.com/satont/tsuwari/libs/grpc/generated/parser"
)

func (c *Handlers) handleCommand(msg irc.PrivateMessage, userBadges []string) {
func (c *Handlers) handleCommand(msg Message, userBadges []string) {
requestStruct := &parser.ProcessCommandRequest{
Sender: &parser.Sender{
Id: msg.User.ID,
Expand All @@ -17,8 +16,8 @@ func (c *Handlers) handleCommand(msg irc.PrivateMessage, userBadges []string) {
Badges: userBadges,
},
Channel: &parser.Channel{
Id: msg.RoomID,
Name: msg.Channel,
Id: msg.Channel.ID,
Name: msg.Channel.Name,
},
Message: &parser.Message{
Id: msg.ID,
Expand All @@ -35,7 +34,7 @@ func (c *Handlers) handleCommand(msg irc.PrivateMessage, userBadges []string) {
for _, v := range res.Responses {
r := v
go c.BotClient.SayWithRateLimiting(
msg.Channel,
msg.Channel.Name,
r,
lo.If(res.IsReply, lo.ToPtr(msg.ID)).Else(nil),
)
Expand All @@ -45,13 +44,13 @@ func (c *Handlers) handleCommand(msg irc.PrivateMessage, userBadges []string) {
validateResposeErr := ValidateResponseSlashes(r)
if validateResposeErr != nil {
c.BotClient.SayWithRateLimiting(
msg.Channel,
msg.Channel.Name,
validateResposeErr.Error(),
nil,
)
} else {
c.BotClient.SayWithRateLimiting(
msg.Channel,
msg.Channel.Name,
r,
lo.If(res.IsReply, &msg.ID).Else(nil),
)
Expand Down
13 changes: 6 additions & 7 deletions apps/bots/internal/bots/handlers/message_greetings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import (
"context"
"fmt"

irc "github.com/gempir/go-twitch-irc/v3"
"github.com/samber/lo"
model "github.com/satont/tsuwari/libs/gomodels"
"github.com/satont/tsuwari/libs/grpc/generated/parser"
"gorm.io/gorm"
)

func (c *Handlers) handleGreetings(
msg irc.PrivateMessage,
msg Message,
userBadges []string,
) {
entity := model.ChannelsGreetings{}
err := c.db.
Where(
`"channelId" = ? AND "userId" = ? AND "processed" = ? AND "enabled" = ?`,
msg.RoomID,
msg.Channel.ID,
msg.User.ID,
false,
true,
Expand All @@ -39,8 +38,8 @@ func (c *Handlers) handleGreetings(

requestStruct := &parser.ParseTextRequestData{
Channel: &parser.Channel{
Id: msg.RoomID,
Name: msg.Channel,
Id: msg.Channel.ID,
Name: msg.Channel.Name,
},
Message: &parser.Message{
Text: entity.Text,
Expand All @@ -65,13 +64,13 @@ func (c *Handlers) handleGreetings(
validateResposeErr := ValidateResponseSlashes(r)
if validateResposeErr != nil {
c.BotClient.SayWithRateLimiting(
msg.Channel,
msg.Channel.Name,
validateResposeErr.Error(),
nil,
)
} else {
c.BotClient.SayWithRateLimiting(
msg.Channel,
msg.Channel.Name,
r,
lo.If(entity.IsReply, &msg.ID).Else(nil),
)
Expand Down
15 changes: 7 additions & 8 deletions apps/bots/internal/bots/handlers/message_keywods.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import (
"sync"
"time"

irc "github.com/gempir/go-twitch-irc/v3"
"github.com/samber/lo"
model "github.com/satont/tsuwari/libs/gomodels"
"github.com/satont/tsuwari/libs/grpc/generated/parser"
)

func (c *Handlers) handleKeywords(
msg irc.PrivateMessage,
msg Message,
userBadges []string,
) {
keywords := []model.ChannelsKeywords{}
err := c.db.Where(`"channelId" = ? AND "enabled" = ?`, msg.RoomID, true).Find(&keywords).Error
err := c.db.Where(`"channelId" = ? AND "enabled" = ?`, msg.Channel.ID, true).Find(&keywords).Error
if err != nil {
fmt.Println(err)
return
Expand All @@ -43,7 +42,7 @@ func (c *Handlers) handleKeywords(
regx, err := regexp.Compile(k.Text)
if err != nil {
c.BotClient.SayWithRateLimiting(
msg.Channel,
msg.Channel.Name,
fmt.Sprintf("regular expression is wrong for keyword %s", k.Text),
nil,
)
Expand Down Expand Up @@ -75,8 +74,8 @@ func (c *Handlers) handleKeywords(
if !isOnCooldown && k.Response != "" {
requestStruct := &parser.ParseTextRequestData{
Channel: &parser.Channel{
Id: msg.RoomID,
Name: msg.Channel,
Id: msg.Channel.ID,
Name: msg.Channel.Name,
},
Sender: &parser.Sender{
Id: msg.User.ID,
Expand All @@ -101,13 +100,13 @@ func (c *Handlers) handleKeywords(
validateResposeErr := ValidateResponseSlashes(r)
if validateResposeErr != nil {
c.BotClient.SayWithRateLimiting(
msg.Channel,
msg.Channel.Name,
validateResposeErr.Error(),
nil,
)
} else {
c.BotClient.SayWithRateLimiting(
msg.Channel,
msg.Channel.Name,
r,
lo.If(k.IsReply, &msg.ID).Else(nil),
)
Expand Down
23 changes: 11 additions & 12 deletions apps/bots/internal/bots/handlers/message_moderation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"
"time"

irc "github.com/gempir/go-twitch-irc/v3"
"github.com/samber/lo"
"github.com/satont/go-helix/v2"
"github.com/satont/tsuwari/apps/bots/internal/bots/handlers/moderation"
Expand All @@ -24,7 +23,7 @@ type parsers struct {
db *gorm.DB
}

var functionsMapping = map[string]func(c *parsers, settings *model.ChannelsModerationSettings, ircMsg irc.PrivateMessage, badges []string) *handleResult{
var functionsMapping = map[string]func(c *parsers, settings *model.ChannelsModerationSettings, ircMsg Message, badges []string) *handleResult{
"links": (*parsers).linksParser,
"blacklists": (*parsers).blacklistsParser,
"symbols": (*parsers).symbolsParser,
Expand All @@ -33,13 +32,13 @@ var functionsMapping = map[string]func(c *parsers, settings *model.ChannelsModer
"emotes": (*parsers).emotesParser,
}

func (c *Handlers) moderateMessage(msg irc.PrivateMessage, badges []string) bool {
func (c *Handlers) moderateMessage(msg Message, badges []string) bool {
if lo.Some(badges, []string{"BROADCASTER", "MODERATOR"}) {
return false
}

settings := []model.ChannelsModerationSettings{}
if err := c.db.Where(`"channelId" = ? AND "enabled" = ?`, msg.RoomID, true).Find(&settings).Error; err != nil {
if err := c.db.Where(`"channelId" = ? AND "enabled" = ?`, msg.Channel.ID, true).Find(&settings).Error; err != nil {
c.logger.Sugar().Error(err)
return false
}
Expand All @@ -66,7 +65,7 @@ func (c *Handlers) moderateMessage(msg irc.PrivateMessage, badges []string) bool
if res != nil {
if res.IsDelete {
res, err := c.BotClient.Api.Client.DeleteMessage(&helix.DeleteMessageParams{
BroadcasterID: msg.RoomID,
BroadcasterID: msg.Channel.ID,
ModeratorID: c.BotClient.Model.ID,
MessageID: msg.ID,
})
Expand All @@ -80,7 +79,7 @@ func (c *Handlers) moderateMessage(msg irc.PrivateMessage, badges []string) bool
} else {
if res.Time != nil {
res, err := c.BotClient.Api.Client.BanUser(&helix.BanUserParams{
BroadcasterID: msg.RoomID,
BroadcasterID: msg.Channel.ID,
ModeratorId: c.BotClient.Model.ID,
Body: helix.BanUserRequestBody{
Duration: int(*res.Time),
Expand Down Expand Up @@ -148,7 +147,7 @@ func (c *parsers) returnByWarnedState(

func (c *parsers) linksParser(
settings *model.ChannelsModerationSettings,
ircMsg irc.PrivateMessage,
ircMsg Message,
badges []string,
) *handleResult {
containLink := moderation.HasLink(ircMsg.Message, true)
Expand All @@ -175,7 +174,7 @@ func (c *parsers) linksParser(

func (c *parsers) blacklistsParser(
settings *model.ChannelsModerationSettings,
ircMsg irc.PrivateMessage,
ircMsg Message,
badges []string,
) *handleResult {
hasBlackListedWord := moderation.HasBlackListedWord(ircMsg.Message, settings.BlackListSentences)
Expand All @@ -189,7 +188,7 @@ func (c *parsers) blacklistsParser(

func (c *parsers) symbolsParser(
settings *model.ChannelsModerationSettings,
ircMsg irc.PrivateMessage,
ircMsg Message,
badges []string,
) *handleResult {
if !settings.MaxPercentage.Valid {
Expand All @@ -210,7 +209,7 @@ func (c *parsers) symbolsParser(

func (c *parsers) longMessageParser(
settings *model.ChannelsModerationSettings,
ircMsg irc.PrivateMessage,
ircMsg Message,
badges []string,
) *handleResult {
if !settings.TriggerLength.Valid {
Expand All @@ -228,7 +227,7 @@ func (c *parsers) longMessageParser(

func (c *parsers) capsParser(
settings *model.ChannelsModerationSettings,
ircMsg irc.PrivateMessage,
ircMsg Message,
badges []string,
) *handleResult {
if !settings.MaxPercentage.Valid {
Expand All @@ -251,7 +250,7 @@ func (c *parsers) capsParser(

func (c *parsers) emotesParser(
settings *model.ChannelsModerationSettings,
ircMsg irc.PrivateMessage,
ircMsg Message,
badges []string,
) *handleResult {
if !settings.TriggerLength.Valid {
Expand Down
23 changes: 23 additions & 0 deletions apps/bots/internal/bots/handlers/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package handlers

import "github.com/gempir/go-twitch-irc/v3"

type MessageUser struct {
ID string
Name string
DisplayName string
Badges map[string]int
}

type MessageChannel struct {
ID string
Name string
}

type Message struct {
ID string
Channel MessageChannel
User MessageUser
Message string
Emotes []*twitch.Emote
}

0 comments on commit 70ea639

Please sign in to comment.