Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed Jan 24, 2025
1 parent e2ceb67 commit 88b809f
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 32 deletions.
15 changes: 8 additions & 7 deletions apps/parser/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
40 changes: 29 additions & 11 deletions apps/parser/internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
Expand Down
95 changes: 95 additions & 0 deletions apps/parser/internal/commands/prefix/setprefix.go
Original file line number Diff line number Diff line change
@@ -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
},
}
30 changes: 16 additions & 14 deletions apps/parser/internal/types/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

0 comments on commit 88b809f

Please sign in to comment.