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-27 09:56:40 +00:00
|
|
|
"time"
|
2022-10-02 06:29:23 +00:00
|
|
|
|
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-27 09:56:40 +00:00
|
|
|
"github.com/robfig/cron/v3"
|
2022-10-29 10:44:11 +00:00
|
|
|
"go.uber.org/zap"
|
2022-10-02 06:29:23 +00:00
|
|
|
)
|
|
|
|
|
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-28 11:59:20 +00:00
|
|
|
SetInternals(ctx context.Context, id, serverID string, internals bool) (domain.Group, error)
|
|
|
|
SetBarbarians(ctx context.Context, id, serverID string, barbarians bool) (domain.Group, error)
|
2022-10-31 05:52:20 +00:00
|
|
|
CleanUp(ctx context.Context) 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-27 13:18:58 +00:00
|
|
|
List(ctx context.Context, groupID, serverID string) ([]domain.MonitorWithTribe, error)
|
2022-10-27 09:56:40 +00:00
|
|
|
Execute(ctx context.Context) ([]domain.EnnoblementNotification, error)
|
2022-10-23 07:44:20 +00:00
|
|
|
Delete(ctx context.Context, id, serverID string) error
|
2022-10-23 06:20:48 +00:00
|
|
|
}
|
|
|
|
|
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
|
2022-10-27 09:56:40 +00:00
|
|
|
c *cron.Cron
|
2022-10-23 06:20:48 +00:00
|
|
|
groupSvc GroupService
|
|
|
|
monitorSvc MonitorService
|
2022-10-23 06:35:00 +00:00
|
|
|
choiceSvc ChoiceService
|
2022-10-29 10:44:11 +00:00
|
|
|
logger *zap.Logger
|
2022-10-02 06:29:23 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 10:44:11 +00:00
|
|
|
func NewBot(
|
|
|
|
token string,
|
|
|
|
groupSvc GroupService,
|
|
|
|
monitorSvc MonitorService,
|
|
|
|
client ChoiceService,
|
|
|
|
logger *zap.Logger,
|
|
|
|
) (*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-28 03:44:50 +00:00
|
|
|
s.Identify.Intents = discordgo.IntentsNone
|
|
|
|
|
2022-10-27 09:56:40 +00:00
|
|
|
b := &Bot{
|
|
|
|
s: s,
|
|
|
|
c: cron.New(
|
|
|
|
cron.WithLocation(time.UTC),
|
|
|
|
cron.WithChain(
|
|
|
|
cron.SkipIfStillRunning(cron.DiscardLogger),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
groupSvc: groupSvc,
|
|
|
|
monitorSvc: monitorSvc,
|
|
|
|
choiceSvc: client,
|
2022-10-29 10:44:11 +00:00
|
|
|
logger: logger,
|
2022-10-02 06:29:23 +00:00
|
|
|
}
|
2022-10-23 06:20:48 +00:00
|
|
|
|
2022-10-28 03:44:50 +00:00
|
|
|
b.s.AddHandler(b.handleSessionReady)
|
2022-10-29 10:44:11 +00:00
|
|
|
b.s.AddHandler(b.logCommands)
|
2022-10-28 03:44:50 +00:00
|
|
|
|
2022-10-27 09:56:40 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) Run() error {
|
|
|
|
if err := b.initCron(); err != nil {
|
|
|
|
return fmt.Errorf("initCron: %w", err)
|
2022-10-03 05:19:33 +00:00
|
|
|
}
|
2022-10-23 06:20:48 +00:00
|
|
|
|
2022-10-27 09:56:40 +00:00
|
|
|
if err := b.s.Open(); err != nil {
|
|
|
|
return fmt.Errorf("s.Open: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.registerCommands(); err != nil {
|
|
|
|
_ = b.s.Close()
|
|
|
|
return fmt.Errorf("couldn't register commands: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.c.Run()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type command interface {
|
|
|
|
name() string
|
|
|
|
register(s *discordgo.Session) error
|
2022-10-03 05:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-27 09:56:40 +00:00
|
|
|
func (b *Bot) initCron() error {
|
2022-10-31 05:52:20 +00:00
|
|
|
jobs := []struct {
|
|
|
|
spec string
|
|
|
|
job cron.Job
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
spec: "@every 1m",
|
|
|
|
job: &executeMonitorsJob{svc: b.monitorSvc, s: b.s, logger: b.logger},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
spec: "0 */8 * * *",
|
|
|
|
job: &cleanUpGroupsJob{svc: b.groupSvc, logger: b.logger},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, j := range jobs {
|
|
|
|
_, err := b.c.AddJob(j.spec, j.job)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-27 09:56:40 +00:00
|
|
|
}
|
2022-10-31 05:52:20 +00:00
|
|
|
|
2022-10-27 09:56:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-28 03:44:50 +00:00
|
|
|
func (b *Bot) handleSessionReady(s *discordgo.Session, _ *discordgo.Ready) {
|
|
|
|
_ = s.UpdateGameStatus(0, "Tribal Wars")
|
|
|
|
}
|
|
|
|
|
2022-10-29 10:44:11 +00:00
|
|
|
func (b *Bot) logCommands(_ *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
if i.Type != discordgo.InteractionApplicationCommand {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdData := i.ApplicationCommandData()
|
|
|
|
cmd := cmdData.Name
|
|
|
|
options := cmdData.Options
|
2022-10-30 04:20:43 +00:00
|
|
|
for len(options) > 0 && options[0].Type == discordgo.ApplicationCommandOptionSubCommand {
|
2022-10-29 10:44:11 +00:00
|
|
|
cmd += " " + options[0].Name
|
|
|
|
options = options[0].Options
|
|
|
|
}
|
|
|
|
|
|
|
|
userID := ""
|
|
|
|
if i.User != nil {
|
|
|
|
userID = i.User.ID
|
|
|
|
} else if i.Member != nil {
|
|
|
|
userID = i.Member.User.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
b.logger.Info(
|
|
|
|
"executing command",
|
|
|
|
zap.String("command", cmd),
|
|
|
|
zap.String("user", userID),
|
|
|
|
zap.String("serverID", i.GuildID),
|
|
|
|
zap.String("channelID", i.ChannelID),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-02 06:29:23 +00:00
|
|
|
func (b *Bot) Close() error {
|
2022-10-27 09:56:40 +00:00
|
|
|
<-b.c.Stop().Done()
|
2022-10-02 06:29:23 +00:00
|
|
|
return b.s.Close()
|
|
|
|
}
|