feat: add a new command - group unset channel-losses/channel-gains (#14)
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #14
This commit is contained in:
Dawid Wysokiński 2022-10-11 05:29:42 +00:00
parent aec3a3736d
commit 9c1884d624
4 changed files with 109 additions and 11 deletions

View File

@ -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 {

View File

@ -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)

View File

@ -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)
}

View File

@ -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 {