2022-10-02 06:29:23 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
2022-10-03 05:19:33 +00:00
|
|
|
"context"
|
2022-10-02 06:29:23 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-10-03 05:19:33 +00:00
|
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
|
2022-10-02 06:29:23 +00:00
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
)
|
|
|
|
|
2022-10-03 05:19:33 +00:00
|
|
|
type GroupService interface {
|
|
|
|
Create(ctx context.Context, params domain.CreateGroupParams) (domain.Group, error)
|
2022-10-12 04:45:16 +00:00
|
|
|
SetChannelGains(ctx context.Context, id, serverID, channel string) (domain.Group, error)
|
|
|
|
SetChannelLosses(ctx context.Context, id, serverID, channel string) (domain.Group, error)
|
2022-10-10 03:59:46 +00:00
|
|
|
List(ctx context.Context, params domain.ListGroupsParams) ([]domain.Group, error)
|
2022-10-12 05:19:07 +00:00
|
|
|
Delete(ctx context.Context, id, serverID string) error
|
2022-10-03 05:19:33 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 06:20:48 +00:00
|
|
|
type MonitorService interface {
|
|
|
|
Create(ctx context.Context, groupID, serverID, tribeTag string) (domain.Monitor, error)
|
|
|
|
}
|
|
|
|
|
2022-10-23 06:35:00 +00:00
|
|
|
type ChoiceService interface {
|
|
|
|
Versions(ctx context.Context) ([]domain.Choice, error)
|
2022-10-09 06:31:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-02 06:29:23 +00:00
|
|
|
type Bot struct {
|
2022-10-23 06:20:48 +00:00
|
|
|
s *discordgo.Session
|
|
|
|
groupSvc GroupService
|
|
|
|
monitorSvc MonitorService
|
2022-10-23 06:35:00 +00:00
|
|
|
choiceSvc ChoiceService
|
2022-10-02 06:29:23 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 06:35:00 +00:00
|
|
|
func NewBot(token string, groupSvc GroupService, monitorSvc MonitorService, client ChoiceService) (*Bot, error) {
|
2022-10-02 06:29:23 +00:00
|
|
|
s, err := discordgo.New("Bot " + token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("discordgo.New: %w", err)
|
|
|
|
}
|
2022-10-23 06:20:48 +00:00
|
|
|
|
2022-10-02 06:29:23 +00:00
|
|
|
if err = s.Open(); err != nil {
|
|
|
|
return nil, fmt.Errorf("s.Open: %w", err)
|
|
|
|
}
|
2022-10-23 06:20:48 +00:00
|
|
|
|
2022-10-23 06:35:00 +00:00
|
|
|
b := &Bot{s: s, groupSvc: groupSvc, monitorSvc: monitorSvc, choiceSvc: client}
|
2022-10-03 05:19:33 +00:00
|
|
|
if err = b.registerCommands(); err != nil {
|
|
|
|
_ = s.Close()
|
|
|
|
return nil, fmt.Errorf("couldn't register commands: %w", err)
|
|
|
|
}
|
2022-10-23 06:20:48 +00:00
|
|
|
|
2022-10-03 05:19:33 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) registerCommands() error {
|
2022-10-23 06:20:48 +00:00
|
|
|
commands := []command{
|
2022-10-23 06:35:00 +00:00
|
|
|
&groupCommand{groupSvc: b.groupSvc, choiceSvc: b.choiceSvc},
|
2022-10-23 06:20:48 +00:00
|
|
|
&monitorCommand{svc: b.monitorSvc},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range commands {
|
|
|
|
if err := b.registerCommand(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-03 05:19:33 +00:00
|
|
|
}
|
2022-10-23 06:20:48 +00:00
|
|
|
|
2022-10-03 05:19:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-11 05:29:42 +00:00
|
|
|
type command interface {
|
|
|
|
name() string
|
|
|
|
register(s *discordgo.Session) error
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:19:33 +00:00
|
|
|
func (b *Bot) registerCommand(cmd command) error {
|
|
|
|
if err := cmd.register(b.s); err != nil {
|
|
|
|
return fmt.Errorf("couldn't register command '%s': %w", cmd.name(), err)
|
|
|
|
}
|
|
|
|
return nil
|
2022-10-02 06:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) Close() error {
|
|
|
|
return b.s.Close()
|
|
|
|
}
|