From 9c1884d6240a71b6576629d27d0391546a2c5313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Tue, 11 Oct 2022 05:29:42 +0000 Subject: [PATCH] feat: add a new command - group unset channel-losses/channel-gains (#14) Reviewed-on: https://gitea.dwysokinski.me/twhelp/dcbot/pulls/14 --- internal/bundb/group.go | 12 +++- internal/discord/bot.go | 5 ++ internal/discord/command.go | 9 --- internal/discord/command_group.go | 94 +++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 11 deletions(-) delete mode 100644 internal/discord/command.go diff --git a/internal/bundb/group.go b/internal/bundb/group.go index 3d5df3a..4b68665 100644 --- a/internal/bundb/group.go +++ b/internal/bundb/group.go @@ -90,11 +90,19 @@ type updateGroupsParamsApplier struct { func (u updateGroupsParamsApplier) apply(q *bun.UpdateQuery) *bun.UpdateQuery { if u.params.ChannelGains.Valid { - q = q.Set("channel_gains = ?", u.params.ChannelGains.String) + if u.params.ChannelGains.String != "" { + q = q.Set("channel_gains = ?", u.params.ChannelGains.String) + } else { + q = q.Set("channel_gains = NULL") + } } if u.params.ChannelLosses.Valid { - q = q.Set("channel_losses = ?", u.params.ChannelLosses.String) + if u.params.ChannelLosses.String != "" { + q = q.Set("channel_losses = ?", u.params.ChannelLosses.String) + } else { + q = q.Set("channel_losses = NULL") + } } if u.params.ServerKey.Valid { 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 {