From 88b809fe86b88889dbc66b83676250c74765fa55 Mon Sep 17 00:00:00 2001 From: Satont Date: Fri, 24 Jan 2025 12:20:41 +0300 Subject: [PATCH] upd --- apps/parser/cmd/main.go | 15 +-- apps/parser/internal/commands/commands.go | 40 +++++--- .../internal/commands/prefix/setprefix.go | 95 +++++++++++++++++++ .../internal/types/services/services.go | 30 +++--- 4 files changed, 148 insertions(+), 32 deletions(-) create mode 100644 apps/parser/internal/commands/prefix/setprefix.go diff --git a/apps/parser/cmd/main.go b/apps/parser/cmd/main.go index 14826d46b..79526738d 100644 --- a/apps/parser/cmd/main.go +++ b/apps/parser/cmd/main.go @@ -178,13 +178,14 @@ func main() { Events: clients.NewEvents(config.AppEnv), Ytsr: clients.NewYtsr(config.AppEnv), }, - TaskDistributor: taskQueueDistributor, - Bus: bus, - CommandsCache: commandscache.New(db, redisClient), - SevenTvCache: seventv.New(redisClient), - SevenTvCacheBySevenTvID: seventv.NewBySeventvID(redisClient), - RedSync: redSync, - CommandsPrefixCache: commandsPrefixRepoCache, + TaskDistributor: taskQueueDistributor, + Bus: bus, + CommandsCache: commandscache.New(db, redisClient), + SevenTvCache: seventv.New(redisClient), + SevenTvCacheBySevenTvID: seventv.NewBySeventvID(redisClient), + RedSync: redSync, + CommandsPrefixCache: commandsPrefixRepoCache, + CommandsPrefixRepository: commandsPrefixRepo, } variablesService := variables.New( diff --git a/apps/parser/internal/commands/commands.go b/apps/parser/internal/commands/commands.go index d083113ed..fa679a904 100644 --- a/apps/parser/internal/commands/commands.go +++ b/apps/parser/internal/commands/commands.go @@ -28,6 +28,7 @@ import ( "github.com/satont/twir/apps/parser/internal/commands/overlays/brb" "github.com/satont/twir/apps/parser/internal/commands/overlays/kappagen" "github.com/satont/twir/apps/parser/internal/commands/permit" + "github.com/satont/twir/apps/parser/internal/commands/prefix" "github.com/satont/twir/apps/parser/internal/commands/shoutout" "github.com/satont/twir/apps/parser/internal/commands/song" sr_youtube "github.com/satont/twir/apps/parser/internal/commands/songrequest/youtube" @@ -123,6 +124,7 @@ func New(opts *Opts) *Commands { seventv.EmoteAdd, clip.MakeClip, marker.Marker, + prefix.SetPrefix, }, func(v *types.DefaultCommand) (string, *types.DefaultCommand) { return v.Name, v }, @@ -206,18 +208,40 @@ func (c *Commands) FindChannelCommandInInput( return &res } +func (c *Commands) getCommandsPrefix(ctx context.Context, channelId string) (string, error) { + var commandsPrefix string + fetchedCommandsPrefix, err := c.services.CommandsPrefixCache.Get(ctx, channelId) + if err != nil && !errors.Is(err, channelscommandsprefixrepository.ErrNotFound) { + return "", err + } + + if fetchedCommandsPrefix == channelscommandsprefixmodel.Nil { + commandsPrefix = "!" + } else { + commandsPrefix = fetchedCommandsPrefix.Prefix + } + + return commandsPrefix, nil +} + func (c *Commands) ParseCommandResponses( ctx context.Context, command *FindByMessageResult, requestData twitch.TwitchChatMessage, ) *busparser.CommandParseResponse { + commandsPrefix, err := c.getCommandsPrefix(ctx, requestData.BroadcasterUserId) + if err != nil { + c.services.Logger.Sugar().Error(err) + return nil + } + result := &busparser.CommandParseResponse{ KeepOrder: command.Cmd.KeepResponsesOrder, IsReply: command.Cmd.IsReply, } var cmdParams *string - params := strings.TrimSpace(requestData.Message.Text[1:][len(command.FoundBy):]) + params := strings.TrimSpace(requestData.Message.Text[len(commandsPrefix):][len(command.FoundBy):]) // this shit comes from 7tv for bypass message duplicate params = strings.ReplaceAll(params, "\U000e0000", "") params = strings.TrimSpace(params) @@ -302,7 +326,7 @@ func (c *Commands) ParseCommandResponses( Channel: parseCtxChannel, Sender: parseCtxSender, Text: cmdParams, - RawText: requestData.Message.Text[1:], + RawText: requestData.Message.Text[len(commandsPrefix):], IsCommand: true, Services: c.services, Cacher: cacher.NewCacher( @@ -433,18 +457,12 @@ func (c *Commands) ProcessChatMessage(ctx context.Context, data twitch.TwitchCha *busparser.CommandParseResponse, error, ) { - var commandsPrefix string - fetchedCommandsPrefix, err := c.services.CommandsPrefixCache.Get(ctx, data.BroadcasterUserId) - if err != nil && !errors.Is(err, channelscommandsprefixrepository.ErrNotFound) { + commandsPrefix, err := c.getCommandsPrefix(ctx, data.BroadcasterUserId) + if err != nil { + c.services.Logger.Sugar().Error(err) return nil, err } - if fetchedCommandsPrefix == channelscommandsprefixmodel.Nil { - commandsPrefix = "!" - } else { - commandsPrefix = fetchedCommandsPrefix.Prefix - } - if !strings.HasPrefix(data.Message.Text, commandsPrefix) { return nil, nil } diff --git a/apps/parser/internal/commands/prefix/setprefix.go b/apps/parser/internal/commands/prefix/setprefix.go new file mode 100644 index 000000000..76de8ea64 --- /dev/null +++ b/apps/parser/internal/commands/prefix/setprefix.go @@ -0,0 +1,95 @@ +package prefix + +import ( + "context" + "errors" + "unicode/utf8" + + "github.com/guregu/null" + "github.com/lib/pq" + command_arguments "github.com/satont/twir/apps/parser/internal/command-arguments" + "github.com/satont/twir/apps/parser/internal/types" + model "github.com/satont/twir/libs/gomodels" + channelscommandsprefixrepository "github.com/twirapp/twir/libs/repositories/channels_commands_prefix" + channelscommandsprefixmodel "github.com/twirapp/twir/libs/repositories/channels_commands_prefix/model" +) + +const setPrefixArgName = "prefix" + +var SetPrefix = &types.DefaultCommand{ + ChannelsCommands: &model.ChannelsCommands{ + Name: "prefix set", + Description: null.StringFrom( + "Set prefix for commands", + ), + RolesIDS: pq.StringArray{model.ChannelRoleTypeModerator.String()}, + Module: "MODERATION", + IsReply: true, + }, + SkipToxicityCheck: true, + Args: []command_arguments.Arg{ + command_arguments.String{ + Name: setPrefixArgName, + }, + }, + Handler: func(ctx context.Context, parseCtx *types.ParseContext) ( + *types.CommandsHandlerResult, + error, + ) { + prefixArg := parseCtx.ArgsParser.Get(setPrefixArgName) + if prefixArg == nil { + return nil, &types.CommandHandlerError{ + Message: "prefix is required", + } + } + + if utf8.RuneCountInString(prefixArg.String()) > 10 { + return nil, &types.CommandHandlerError{ + Message: "prefix cannot be longer than 10 characters", + } + } + + currentPrefix, err := parseCtx.Services.CommandsPrefixRepository.GetByChannelID( + ctx, + parseCtx.Channel.ID, + ) + if err != nil && !errors.Is(err, channelscommandsprefixrepository.ErrNotFound) { + return nil, &types.CommandHandlerError{ + Message: "cannot get current prefix", + Err: err, + } + } + + if currentPrefix == channelscommandsprefixmodel.Nil { + _, err = parseCtx.Services.CommandsPrefixRepository.Create( + ctx, + channelscommandsprefixrepository.CreateInput{ + ChannelID: parseCtx.Channel.ID, + Prefix: prefixArg.String(), + }, + ) + if err != nil { + return nil, &types.CommandHandlerError{ + Message: "cannot create prefix", + Err: err, + } + } + } else { + _, err = parseCtx.Services.CommandsPrefixRepository.Update( + ctx, + currentPrefix.ID, + channelscommandsprefixrepository.UpdateInput{ + Prefix: prefixArg.String(), + }, + ) + if err != nil { + return nil, &types.CommandHandlerError{ + Message: "cannot update prefix", + Err: err, + } + } + } + + return &types.CommandsHandlerResult{Result: []string{"Prefix updated"}}, nil + }, +} diff --git a/apps/parser/internal/types/services/services.go b/apps/parser/internal/types/services/services.go index b8f5bfde6..25cb2a81b 100644 --- a/apps/parser/internal/types/services/services.go +++ b/apps/parser/internal/types/services/services.go @@ -15,6 +15,7 @@ import ( "github.com/twirapp/twir/libs/grpc/websockets" "github.com/twirapp/twir/libs/grpc/ytsr" "github.com/twirapp/twir/libs/integrations/seventv" + channelscommandsprefixrepository "github.com/twirapp/twir/libs/repositories/channels_commands_prefix" channelscommandsprefixmodel "github.com/twirapp/twir/libs/repositories/channels_commands_prefix/model" "go.uber.org/zap" "gorm.io/gorm" @@ -29,18 +30,19 @@ type Grpc struct { } type Services struct { - Config *cfg.Config - Logger *zap.Logger - Gorm *gorm.DB - Sqlx *sqlx.DB - Redis *redis.Client - GrpcClients *Grpc - TaskDistributor task_queue.TaskDistributor - Bus *buscore.Bus - CommandsCache *generic_cacher.GenericCacher[[]model.ChannelsCommands] - CommandsPrefixCache *generic_cacher.GenericCacher[channelscommandsprefixmodel.ChannelsCommandsPrefix] - SevenTvCache *generic_cacher.GenericCacher[*seventv.ProfileResponse] - SevenTvCacheBySevenTvID *generic_cacher.GenericCacher[*seventv.ProfileResponse] - RedSync *redsync.Redsync - CommandsLock *redsync.Mutex + Config *cfg.Config + Logger *zap.Logger + Gorm *gorm.DB + Sqlx *sqlx.DB + Redis *redis.Client + GrpcClients *Grpc + TaskDistributor task_queue.TaskDistributor + Bus *buscore.Bus + CommandsCache *generic_cacher.GenericCacher[[]model.ChannelsCommands] + CommandsPrefixCache *generic_cacher.GenericCacher[channelscommandsprefixmodel.ChannelsCommandsPrefix] + SevenTvCache *generic_cacher.GenericCacher[*seventv.ProfileResponse] + SevenTvCacheBySevenTvID *generic_cacher.GenericCacher[*seventv.ProfileResponse] + RedSync *redsync.Redsync + CommandsLock *redsync.Mutex + CommandsPrefixRepository channelscommandsprefixrepository.Repository }