Skip to content

Commit

Permalink
chore: add update channel id command
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed May 31, 2023
1 parent 73d76ae commit cc50c49
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/db/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type ChannelUpdateQuery struct {
IsLive *bool
Category *string
Title *string

DangerNewChannelId *string
}

type ChannelInterface interface {
Expand Down
4 changes: 4 additions & 0 deletions internal/db/channel_impl_ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func (c *channelEntService) Update(
updateQuery.SetTitle(*query.Title)
}

if query.DangerNewChannelId != nil {
updateQuery.SetChannelID(*query.DangerNewChannelId)
}

newChannel, err := updateQuery.Save(context.Background())

if err != nil {
Expand Down
64 changes: 64 additions & 0 deletions internal/telegram/commands/change_channel_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package commands

import (
"context"
"github.com/mr-linch/go-tg/tgb"
"github.com/samber/lo"
"github.com/satont/twitch-notifier/internal/db"
"github.com/satont/twitch-notifier/internal/db/db_models"
tgtypes "github.com/satont/twitch-notifier/internal/telegram/types"
"github.com/satont/twitch-notifier/internal/types"
"go.uber.org/zap"
"strings"
)

type ChangeChannelId struct {
*tgtypes.CommandOpts
}

func (c *ChangeChannelId) HandleCommand(ctx context.Context, msg *tgb.MessageUpdate) error {
text := strings.ReplaceAll(msg.Message.Text, "/change_channel_id ", "")
splittedText := strings.Split(strings.TrimSpace(text), " ")

if len(splittedText) != 2 {
return nil
}

sourceChannelID := splittedText[0]
targetChannelID := splittedText[1]

_, err := c.Services.Channel.Update(
ctx,
sourceChannelID,
db_models.ChannelServiceTwitch,
&db.ChannelUpdateQuery{
DangerNewChannelId: &targetChannelID,
},
)

if err != nil {
zap.S().Error(err)
}

return msg.Answer("done").DoVoid(ctx)
}

var (
changeChannelIdFilter = tgb.Command("change_channel_id")
changeChannelIdFilterAdminFilter = func(services *types.Services) tgb.Filter {
return tgb.FilterFunc(func(ctx context.Context, update *tgb.Update) (bool, error) {
return lo.Contains(services.Config.TelegramBotAdmins, update.Message.Chat.ID.PeerID()), nil
})
}
)

func NewChangeChannelId(opts *tgtypes.CommandOpts) {
cmd := &ChangeChannelId{
CommandOpts: opts,
}
opts.Router.Message(
cmd.HandleCommand,
changeChannelIdFilter,
changeChannelIdFilterAdminFilter(opts.Services),
)
}
1 change: 1 addition & 0 deletions internal/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func NewTelegram(ctx context.Context, token string, services *types.Services) *T
commands.NewLiveCommand(commandOpts)
commands.NewBroadcastCommand(commandOpts)
commands.NewLanguagePicker(commandOpts)
commands.NewChangeChannelId(commandOpts)

poller := tgb.NewPoller(router, client)

Expand Down

0 comments on commit cc50c49

Please sign in to comment.