diff --git a/internal/discord/bot.go b/internal/discord/bot.go index 06735b0..e1ea27d 100644 --- a/internal/discord/bot.go +++ b/internal/discord/bot.go @@ -50,6 +50,11 @@ func (b *Bot) registerCommands() error { return nil } +type command interface { + name() string + register(s *discordgo.Session) error +} + 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) diff --git a/internal/discord/command.go b/internal/discord/command.go deleted file mode 100644 index c0e86ab..0000000 --- a/internal/discord/command.go +++ /dev/null @@ -1,9 +0,0 @@ -package discord - -import "github.com/bwmarrin/discordgo" - -type command interface { - name() string - register(s *discordgo.Session) error - handle(s *discordgo.Session, i *discordgo.InteractionCreate) -} diff --git a/internal/discord/command_group.go b/internal/discord/command_group.go index c97f260..aae9d72 100644 --- a/internal/discord/command_group.go +++ b/internal/discord/command_group.go @@ -160,6 +160,39 @@ func (c *groupCommand) create(s *discordgo.Session) error { }, }, }, + { + Name: "unset", + Description: "Unsets various properties in a group configuration", + Type: discordgo.ApplicationCommandOptionSubCommandGroup, + Options: []*discordgo.ApplicationCommandOption{ + { + Name: "channel-gains", + Description: "Disables notifications of gained villages", + Type: discordgo.ApplicationCommandOptionSubCommand, + Options: []*discordgo.ApplicationCommandOption{ + { + Name: "group", + Description: "Group ID", + Type: discordgo.ApplicationCommandOptionString, + Required: true, + }, + }, + }, + { + Name: "channel-losses", + Description: "Disables notifications of lost villages", + Type: discordgo.ApplicationCommandOptionSubCommand, + Options: []*discordgo.ApplicationCommandOption{ + { + Name: "group", + Description: "Group ID", + Type: discordgo.ApplicationCommandOptionString, + Required: true, + }, + }, + }, + }, + }, }, }) if err != nil { @@ -201,6 +234,9 @@ func (c *groupCommand) handle(s *discordgo.Session, i *discordgo.InteractionCrea case "set": c.handleSet(s, i) return + case "unset": + c.handleUnset(s, i) + return default: } } @@ -421,6 +457,64 @@ func (c *groupCommand) handleSetChannelLosses(s *discordgo.Session, i *discordgo }) } +func (c *groupCommand) handleUnset(s *discordgo.Session, i *discordgo.InteractionCreate) { + switch i.ApplicationCommandData().Options[0].Options[0].Name { + case "channel-gains": + c.handleUnsetChannelGains(s, i) + return + case "channel-losses": + c.handleUnsetChannelLosses(s, i) + return + default: + } +} + +func (c *groupCommand) handleUnsetChannelGains(s *discordgo.Session, i *discordgo.InteractionCreate) { + ctx := context.Background() + + group := i.ApplicationCommandData().Options[0].Options[0].Options[0].StringValue() + _, err := c.svc.SetChannelGains(ctx, group, "") + 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 updated", + }, + }) +} + +func (c *groupCommand) handleUnsetChannelLosses(s *discordgo.Session, i *discordgo.InteractionCreate) { + ctx := context.Background() + + group := i.ApplicationCommandData().Options[0].Options[0].Options[0].StringValue() + _, err := c.svc.SetChannelLosses(ctx, group, "") + 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 updated", + }, + }) +} + func buildGroupListDescription(groups []domain.Group) string { description := "**ID** - **Version** - **Server**" for i, g := range groups {