All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #2
87 lines
2.1 KiB
Go
87 lines
2.1 KiB
Go
package discord
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
type groupCommand struct {
|
|
svc GroupService
|
|
}
|
|
|
|
func (c *groupCommand) name() string {
|
|
return "group"
|
|
}
|
|
|
|
func (c *groupCommand) register(s *discordgo.Session) error {
|
|
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: "add",
|
|
Description: "Adds a new monitor group",
|
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("s.ApplicationCommandCreate: %w", err)
|
|
}
|
|
s.AddHandler(c.handle)
|
|
return 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 "add":
|
|
c.handleAdd(s, i)
|
|
return
|
|
default:
|
|
}
|
|
}
|
|
|
|
func (c *groupCommand) handleAdd(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
ctx := context.Background()
|
|
|
|
params, err := domain.NewCreateGroupParams(i.GuildID)
|
|
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 added (id=" + group.ID + ")",
|
|
},
|
|
})
|
|
}
|