dcbot/internal/discord/command_group.go
Dawid Wysokiński 1660003260
All checks were successful
continuous-integration/drone/push Build is passing
feat: add a new command - group list (#11)
Reviewed-on: #11
2022-10-10 03:59:46 +00:00

225 lines
5.8 KiB
Go

package discord
import (
"context"
"fmt"
"time"
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
"github.com/bwmarrin/discordgo"
)
type groupCommand struct {
svc GroupService
client TWHelpClient
}
func (c *groupCommand) name() string {
return "group"
}
func (c *groupCommand) register(s *discordgo.Session) error {
if err := c.create(s); err != nil {
return err
}
s.AddHandler(c.handle)
return nil
}
func (c *groupCommand) create(s *discordgo.Session) error {
versionChoices, err := c.getVersionChoices()
if err != nil {
return err
}
var perm int64 = discordgo.PermissionAdministrator
_, err = s.ApplicationCommandCreate(s.State.User.ID, "", &discordgo.ApplicationCommand{
Name: c.name(),
Description: "Manages monitor groups on this server",
DefaultMemberPermissions: &perm,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "create",
Description: "Creates a new monitor group",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "version",
Description: "e.g. www.tribalwars.net, www.plemiona.pl",
Type: discordgo.ApplicationCommandOptionString,
Choices: versionChoices,
Required: true,
},
{
Name: "server",
Description: "Tribal Wars server (e.g. en115, pl170)",
Type: discordgo.ApplicationCommandOptionString,
Required: true,
},
{
Name: "channel-gained-villages",
Description: "Specifies on which channel notifications of gained villages will appear",
Type: discordgo.ApplicationCommandOptionChannel,
ChannelTypes: []discordgo.ChannelType{
discordgo.ChannelTypeGuildText,
},
Required: false,
},
{
Name: "channel-lost-villages",
Description: "Specifies on which channel notifications of lost villages will appear",
Type: discordgo.ApplicationCommandOptionChannel,
ChannelTypes: []discordgo.ChannelType{
discordgo.ChannelTypeGuildText,
},
Required: false,
},
},
},
{
Name: "list",
Description: "Lists all created groups",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
},
})
if err != nil {
return fmt.Errorf("s.ApplicationCommandCreate: %w", err)
}
return nil
}
func (c *groupCommand) getVersionChoices() ([]*discordgo.ApplicationCommandOptionChoice, error) {
versions, err := c.client.ListVersions(context.Background())
if err != nil {
return nil, fmt.Errorf("TWHelpClient.ListVersions: %w", err)
}
versionChoices := make([]*discordgo.ApplicationCommandOptionChoice, 0, len(versions))
for _, v := range versions {
versionChoices = append(versionChoices, &discordgo.ApplicationCommandOptionChoice{
Name: v.Host,
Value: v.Code,
})
}
return versionChoices, nil
}
func (c *groupCommand) handle(s *discordgo.Session, i *discordgo.InteractionCreate) {
cmdData := i.ApplicationCommandData()
if cmdData.Name != c.name() {
return
}
switch cmdData.Options[0].Name {
case "create":
c.handleCreate(s, i)
return
case "list":
c.handleList(s, i)
return
default:
}
}
func (c *groupCommand) handleCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
ctx := context.Background()
version := ""
server := ""
channelGainedVillages := ""
channelLostVillages := ""
for _, opt := range i.ApplicationCommandData().Options[0].Options {
if opt == nil {
continue
}
switch opt.Name {
case "version":
version = opt.StringValue()
case "server":
server = opt.StringValue()
case "channel-gained-villages":
channelGainedVillages = opt.ChannelValue(s).ID
case "channel-lost-villages":
channelLostVillages = opt.ChannelValue(s).ID
}
}
params, err := domain.NewCreateGroupParams(
i.GuildID,
version,
server,
channelGainedVillages,
channelLostVillages,
)
if err != nil {
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: messageFromError(err),
},
})
return
}
group, err := c.svc.Create(ctx, params)
if err != nil {
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: messageFromError(err),
},
})
return
}
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "group has been successfully created (ID=" + group.ID + ")",
},
})
}
func (c *groupCommand) handleList(s *discordgo.Session, i *discordgo.InteractionCreate) {
ctx := context.Background()
groups, err := c.svc.List(ctx, domain.ListGroupsParams{
ServerIDs: []string{i.GuildID},
})
if err != nil {
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: messageFromError(err),
},
})
return
}
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
Type: discordgo.EmbedTypeRich,
Title: "Group list",
Description: buildGroupListDescription(groups),
Timestamp: time.Now().Format(time.RFC3339),
},
},
},
})
}
func buildGroupListDescription(groups []domain.Group) string {
description := "**ID** - **Version** - **Server**"
for i, g := range groups {
description += fmt.Sprintf("\n%d. %s - %s - %s", i+1, g.ID, g.VersionCode, g.ServerKey)
}
return description
}