From 2cc4538b1b80121ba3a554908f2d5794b0a2eae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Sun, 23 Oct 2022 08:32:18 +0200 Subject: [PATCH] feat: add a new service - Choice --- cmd/dcbot/internal/run/run.go | 3 ++- internal/discord/bot.go | 13 ++++++------ internal/discord/command_group.go | 35 ++++++++++++++++--------------- internal/domain/choice.go | 6 ++++++ internal/service/choice.go | 33 +++++++++++++++++++++++++++++ internal/service/service.go | 1 + 6 files changed, 66 insertions(+), 25 deletions(-) create mode 100644 internal/domain/choice.go create mode 100644 internal/service/choice.go diff --git a/cmd/dcbot/internal/run/run.go b/cmd/dcbot/internal/run/run.go index b0d91f1..7ecc25f 100644 --- a/cmd/dcbot/internal/run/run.go +++ b/cmd/dcbot/internal/run/run.go @@ -41,6 +41,7 @@ func New() *cli.Command { groupRepo := bundb.NewGroup(db) monitorRepo := bundb.NewMonitor(db) + choiceSvc := service.NewChoice(client) groupSvc := service.NewGroup(groupRepo, client) monitorSvc := service.NewMonitor(monitorRepo, groupRepo, client) @@ -49,7 +50,7 @@ func New() *cli.Command { return fmt.Errorf("newBotConfig: %w", err) } - bot, err := discord.NewBot(cfg.Token, groupSvc, monitorSvc, client) + bot, err := discord.NewBot(cfg.Token, groupSvc, monitorSvc, choiceSvc) if err != nil { return fmt.Errorf("discord.NewBot: %w", err) } diff --git a/internal/discord/bot.go b/internal/discord/bot.go index 39573e9..f99309e 100644 --- a/internal/discord/bot.go +++ b/internal/discord/bot.go @@ -5,7 +5,6 @@ import ( "fmt" "gitea.dwysokinski.me/twhelp/dcbot/internal/domain" - "gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp" "github.com/bwmarrin/discordgo" ) @@ -21,18 +20,18 @@ type MonitorService interface { Create(ctx context.Context, groupID, serverID, tribeTag string) (domain.Monitor, error) } -type TWHelpClient interface { - ListVersions(ctx context.Context) ([]twhelp.Version, error) +type ChoiceService interface { + Versions(ctx context.Context) ([]domain.Choice, error) } type Bot struct { s *discordgo.Session groupSvc GroupService monitorSvc MonitorService - client TWHelpClient + choiceSvc ChoiceService } -func NewBot(token string, groupSvc GroupService, monitorSvc MonitorService, client TWHelpClient) (*Bot, error) { +func NewBot(token string, groupSvc GroupService, monitorSvc MonitorService, client ChoiceService) (*Bot, error) { s, err := discordgo.New("Bot " + token) if err != nil { return nil, fmt.Errorf("discordgo.New: %w", err) @@ -42,7 +41,7 @@ func NewBot(token string, groupSvc GroupService, monitorSvc MonitorService, clie return nil, fmt.Errorf("s.Open: %w", err) } - b := &Bot{s: s, groupSvc: groupSvc, monitorSvc: monitorSvc, client: client} + b := &Bot{s: s, groupSvc: groupSvc, monitorSvc: monitorSvc, choiceSvc: client} if err = b.registerCommands(); err != nil { _ = s.Close() return nil, fmt.Errorf("couldn't register commands: %w", err) @@ -53,7 +52,7 @@ func NewBot(token string, groupSvc GroupService, monitorSvc MonitorService, clie func (b *Bot) registerCommands() error { commands := []command{ - &groupCommand{svc: b.groupSvc, client: b.client}, + &groupCommand{groupSvc: b.groupSvc, choiceSvc: b.choiceSvc}, &monitorCommand{svc: b.monitorSvc}, } diff --git a/internal/discord/command_group.go b/internal/discord/command_group.go index b7e820b..224ad9b 100644 --- a/internal/discord/command_group.go +++ b/internal/discord/command_group.go @@ -10,8 +10,8 @@ import ( ) type groupCommand struct { - svc GroupService - client TWHelpClient + groupSvc GroupService + choiceSvc ChoiceService } func (c *groupCommand) name() string { @@ -191,19 +191,20 @@ func (c *groupCommand) create(s *discordgo.Session) error { } func (c *groupCommand) getVersionChoices() ([]*discordgo.ApplicationCommandOptionChoice, error) { - versions, err := c.client.ListVersions(context.Background()) + choices, err := c.choiceSvc.Versions(context.Background()) if err != nil { - return nil, fmt.Errorf("TWHelpClient.ListVersions: %w", err) + return nil, fmt.Errorf("ChoiceService.Versions: %w", err) } - versionChoices := make([]*discordgo.ApplicationCommandOptionChoice, 0, len(versions)) - for _, v := range versions { - versionChoices = append(versionChoices, &discordgo.ApplicationCommandOptionChoice{ - Name: v.Host, - Value: v.Code, + dcChoices := make([]*discordgo.ApplicationCommandOptionChoice, 0, len(choices)) + for _, v := range choices { + dcChoices = append(dcChoices, &discordgo.ApplicationCommandOptionChoice{ + Name: v.Name, + Value: v.Value, }) } - return versionChoices, nil + + return dcChoices, nil } func (c *groupCommand) handle(s *discordgo.Session, i *discordgo.InteractionCreate) { @@ -273,7 +274,7 @@ func (c *groupCommand) handleCreate(s *discordgo.Session, i *discordgo.Interacti return } - group, err := c.svc.Create(ctx, params) + group, err := c.groupSvc.Create(ctx, params) if err != nil { _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, @@ -295,7 +296,7 @@ func (c *groupCommand) handleCreate(s *discordgo.Session, i *discordgo.Interacti func (c *groupCommand) handleList(s *discordgo.Session, i *discordgo.InteractionCreate) { ctx := context.Background() - groups, err := c.svc.List(ctx, domain.ListGroupsParams{ + groups, err := c.groupSvc.List(ctx, domain.ListGroupsParams{ ServerIDs: []string{i.GuildID}, }) if err != nil { @@ -352,7 +353,7 @@ func (c *groupCommand) handleSetChannelGains(s *discordgo.Session, i *discordgo. } } - _, err := c.svc.SetChannelGains(ctx, group, i.GuildID, channel) + _, err := c.groupSvc.SetChannelGains(ctx, group, i.GuildID, channel) if err != nil { _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, @@ -388,7 +389,7 @@ func (c *groupCommand) handleSetChannelLosses(s *discordgo.Session, i *discordgo } } - _, err := c.svc.SetChannelLosses(ctx, group, i.GuildID, channel) + _, err := c.groupSvc.SetChannelLosses(ctx, group, i.GuildID, channel) if err != nil { _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, @@ -423,7 +424,7 @@ func (c *groupCommand) handleUnsetChannelGains(s *discordgo.Session, i *discordg ctx := context.Background() group := i.ApplicationCommandData().Options[0].Options[0].Options[0].StringValue() - _, err := c.svc.SetChannelGains(ctx, group, i.GuildID, "") + _, err := c.groupSvc.SetChannelGains(ctx, group, i.GuildID, "") if err != nil { _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, @@ -446,7 +447,7 @@ func (c *groupCommand) handleUnsetChannelLosses(s *discordgo.Session, i *discord ctx := context.Background() group := i.ApplicationCommandData().Options[0].Options[0].Options[0].StringValue() - _, err := c.svc.SetChannelLosses(ctx, group, i.GuildID, "") + _, err := c.groupSvc.SetChannelLosses(ctx, group, i.GuildID, "") if err != nil { _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, @@ -469,7 +470,7 @@ func (c *groupCommand) handleDelete(s *discordgo.Session, i *discordgo.Interacti ctx := context.Background() group := i.ApplicationCommandData().Options[0].Options[0].StringValue() - if err := c.svc.Delete(ctx, group, i.GuildID); err != nil { + if err := c.groupSvc.Delete(ctx, group, i.GuildID); err != nil { _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ diff --git a/internal/domain/choice.go b/internal/domain/choice.go new file mode 100644 index 0000000..c55e992 --- /dev/null +++ b/internal/domain/choice.go @@ -0,0 +1,6 @@ +package domain + +type Choice struct { + Name string + Value string +} diff --git a/internal/service/choice.go b/internal/service/choice.go new file mode 100644 index 0000000..6e1fd0b --- /dev/null +++ b/internal/service/choice.go @@ -0,0 +1,33 @@ +package service + +import ( + "context" + "fmt" + + "gitea.dwysokinski.me/twhelp/dcbot/internal/domain" +) + +type Choice struct { + client TWHelpClient +} + +func NewChoice(client TWHelpClient) *Choice { + return &Choice{client: client} +} + +func (c *Choice) Versions(ctx context.Context) ([]domain.Choice, error) { + versions, err := c.client.ListVersions(ctx) + if err != nil { + return nil, fmt.Errorf("TWHelpClient.ListVersions: %w", err) + } + + choices := make([]domain.Choice, 0, len(versions)) + for _, v := range versions { + choices = append(choices, domain.Choice{ + Name: v.Host, + Value: v.Code, + }) + } + + return choices, nil +} diff --git a/internal/service/service.go b/internal/service/service.go index 8abd446..710c043 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -10,6 +10,7 @@ import ( //counterfeiter:generate -o internal/mock/twhelp_client.gen.go . TWHelpClient type TWHelpClient interface { + ListVersions(ctx context.Context) ([]twhelp.Version, error) GetServer(ctx context.Context, version, server string) (twhelp.Server, error) GetTribeByTag(ctx context.Context, version, server, tag string) (twhelp.Tribe, error) } -- 2.45.1