feat: add a new service - Choice
All checks were successful
continuous-integration/drone/pr Build is passing

This commit is contained in:
Dawid Wysokiński 2022-10-23 08:32:18 +02:00
parent 58f73e9ca8
commit 2cc4538b1b
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
6 changed files with 66 additions and 25 deletions

View File

@ -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)
}

View File

@ -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},
}

View File

@ -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{

View File

@ -0,0 +1,6 @@
package domain
type Choice struct {
Name string
Value string
}

View File

@ -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
}

View File

@ -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)
}