feat: add a new service - Choice
All checks were successful
continuous-integration/drone/pr Build is passing
All checks were successful
continuous-integration/drone/pr Build is passing
This commit is contained in:
parent
58f73e9ca8
commit
2cc4538b1b
|
@ -41,6 +41,7 @@ func New() *cli.Command {
|
||||||
groupRepo := bundb.NewGroup(db)
|
groupRepo := bundb.NewGroup(db)
|
||||||
monitorRepo := bundb.NewMonitor(db)
|
monitorRepo := bundb.NewMonitor(db)
|
||||||
|
|
||||||
|
choiceSvc := service.NewChoice(client)
|
||||||
groupSvc := service.NewGroup(groupRepo, client)
|
groupSvc := service.NewGroup(groupRepo, client)
|
||||||
monitorSvc := service.NewMonitor(monitorRepo, groupRepo, client)
|
monitorSvc := service.NewMonitor(monitorRepo, groupRepo, client)
|
||||||
|
|
||||||
|
@ -49,7 +50,7 @@ func New() *cli.Command {
|
||||||
return fmt.Errorf("newBotConfig: %w", err)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("discord.NewBot: %w", err)
|
return fmt.Errorf("discord.NewBot: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
|
||||||
"gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp"
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -21,18 +20,18 @@ type MonitorService interface {
|
||||||
Create(ctx context.Context, groupID, serverID, tribeTag string) (domain.Monitor, error)
|
Create(ctx context.Context, groupID, serverID, tribeTag string) (domain.Monitor, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type TWHelpClient interface {
|
type ChoiceService interface {
|
||||||
ListVersions(ctx context.Context) ([]twhelp.Version, error)
|
Versions(ctx context.Context) ([]domain.Choice, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Bot struct {
|
type Bot struct {
|
||||||
s *discordgo.Session
|
s *discordgo.Session
|
||||||
groupSvc GroupService
|
groupSvc GroupService
|
||||||
monitorSvc MonitorService
|
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)
|
s, err := discordgo.New("Bot " + token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("discordgo.New: %w", err)
|
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)
|
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 {
|
if err = b.registerCommands(); err != nil {
|
||||||
_ = s.Close()
|
_ = s.Close()
|
||||||
return nil, fmt.Errorf("couldn't register commands: %w", err)
|
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 {
|
func (b *Bot) registerCommands() error {
|
||||||
commands := []command{
|
commands := []command{
|
||||||
&groupCommand{svc: b.groupSvc, client: b.client},
|
&groupCommand{groupSvc: b.groupSvc, choiceSvc: b.choiceSvc},
|
||||||
&monitorCommand{svc: b.monitorSvc},
|
&monitorCommand{svc: b.monitorSvc},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type groupCommand struct {
|
type groupCommand struct {
|
||||||
svc GroupService
|
groupSvc GroupService
|
||||||
client TWHelpClient
|
choiceSvc ChoiceService
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *groupCommand) name() string {
|
func (c *groupCommand) name() string {
|
||||||
|
@ -191,19 +191,20 @@ func (c *groupCommand) create(s *discordgo.Session) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *groupCommand) getVersionChoices() ([]*discordgo.ApplicationCommandOptionChoice, 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 {
|
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))
|
dcChoices := make([]*discordgo.ApplicationCommandOptionChoice, 0, len(choices))
|
||||||
for _, v := range versions {
|
for _, v := range choices {
|
||||||
versionChoices = append(versionChoices, &discordgo.ApplicationCommandOptionChoice{
|
dcChoices = append(dcChoices, &discordgo.ApplicationCommandOptionChoice{
|
||||||
Name: v.Host,
|
Name: v.Name,
|
||||||
Value: v.Code,
|
Value: v.Value,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return versionChoices, nil
|
|
||||||
|
return dcChoices, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *groupCommand) handle(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
group, err := c.svc.Create(ctx, params)
|
group, err := c.groupSvc.Create(ctx, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
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) {
|
func (c *groupCommand) handleList(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
groups, err := c.svc.List(ctx, domain.ListGroupsParams{
|
groups, err := c.groupSvc.List(ctx, domain.ListGroupsParams{
|
||||||
ServerIDs: []string{i.GuildID},
|
ServerIDs: []string{i.GuildID},
|
||||||
})
|
})
|
||||||
if err != nil {
|
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 {
|
if err != nil {
|
||||||
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
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 {
|
if err != nil {
|
||||||
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
@ -423,7 +424,7 @@ func (c *groupCommand) handleUnsetChannelGains(s *discordgo.Session, i *discordg
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
group := i.ApplicationCommandData().Options[0].Options[0].Options[0].StringValue()
|
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 {
|
if err != nil {
|
||||||
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
@ -446,7 +447,7 @@ func (c *groupCommand) handleUnsetChannelLosses(s *discordgo.Session, i *discord
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
group := i.ApplicationCommandData().Options[0].Options[0].Options[0].StringValue()
|
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 {
|
if err != nil {
|
||||||
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
@ -469,7 +470,7 @@ func (c *groupCommand) handleDelete(s *discordgo.Session, i *discordgo.Interacti
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
group := i.ApplicationCommandData().Options[0].Options[0].StringValue()
|
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{
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &discordgo.InteractionResponseData{
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
|
6
internal/domain/choice.go
Normal file
6
internal/domain/choice.go
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package domain
|
||||||
|
|
||||||
|
type Choice struct {
|
||||||
|
Name string
|
||||||
|
Value string
|
||||||
|
}
|
33
internal/service/choice.go
Normal file
33
internal/service/choice.go
Normal 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
|
||||||
|
}
|
|
@ -10,6 +10,7 @@ import (
|
||||||
|
|
||||||
//counterfeiter:generate -o internal/mock/twhelp_client.gen.go . TWHelpClient
|
//counterfeiter:generate -o internal/mock/twhelp_client.gen.go . TWHelpClient
|
||||||
type TWHelpClient interface {
|
type TWHelpClient interface {
|
||||||
|
ListVersions(ctx context.Context) ([]twhelp.Version, error)
|
||||||
GetServer(ctx context.Context, version, server string) (twhelp.Server, error)
|
GetServer(ctx context.Context, version, server string) (twhelp.Server, error)
|
||||||
GetTribeByTag(ctx context.Context, version, server, tag string) (twhelp.Tribe, error)
|
GetTribeByTag(ctx context.Context, version, server, tag string) (twhelp.Tribe, error)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user