ticket-pimp/client/discord/router/handle_ticket.go

189 lines
4.9 KiB
Go

package router
import (
"context"
"fmt"
"log"
"ticket-pimp/internal/domain"
"ticket-pimp/internal/helpers"
"github.com/bwmarrin/discordgo"
)
func (c *client) GetInfo() Command {
return Command{
Command: discordgo.ApplicationCommand{
Name: "info",
Description: "Get project's info",
},
Handler: c.getInfo,
}
}
func (c *client) getInfo(s *discordgo.Session, i *discordgo.InteractionCreate) {
var result string
// Get channel from the request
dchan, err := s.Channel(i.ChannelID)
if err != nil {
result = "unable to get channel from the message"
} else {
project, err := c.controller.GetProjectByChannelID(context.TODO(), dchan.ID)
if err != nil {
result = err.Error()
} else {
if project != nil {
result = project.DiscordString()
if err != nil {
result += "Errors: " + err.Error()
}
} else {
result = "Something wrong with retrieving project from db"
}
}
}
c.defaultFollowUp(result, s, i)
}
func (c *client) InitProjectFromChannel(minLength int) Command {
return Command{
Command: discordgo.ApplicationCommand{
Name: "init_project",
Description: "Connect project with Coda ID",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "key",
Description: "Project's key from Coda.io",
Required: true,
MinLength: &minLength,
},
},
},
Handler: c.initProjectFromChannel,
}
}
func (c *client) initProjectFromChannel(s *discordgo.Session, i *discordgo.InteractionCreate) {
var result string
// Get channel from the request
dchan, err := s.Channel(i.ChannelID)
if err != nil {
result = "unable to get channel from the message"
} else {
if dchan.ParentID != c.conf.IsProjectChannel {
// Sending result:
_, err := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Content: "This channel is not at the project's group",
})
if err != nil {
s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Content: fmt.Sprintf("Something went wrong: %v", err),
})
return
}
return
}
// Access options in the order provided by the user.
options := i.ApplicationCommandData().Options
// Or convert the slice into a map
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
for _, opt := range options {
optionMap[opt.Name] = opt
}
if option, ok := optionMap["key"]; ok {
var errMsg error = nil
project, err := c.controller.InitProjectInChannel(context.TODO(), i.ChannelID, option.StringValue())
if err != nil {
result = fmt.Sprintf("unable to init project: %v", err)
} else {
result = project.DiscordString()
if errMsg != nil {
result += "Errors: " + errMsg.Error()
}
}
}
}
c.defaultFollowUp(result, s, i)
}
func (c *client) CreateTicketHandler(repoNameMinLength int) Command {
return Command{
Command: discordgo.ApplicationCommand{
Name: "project",
Description: "Create new development ticket",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "project_name",
Description: "Temporary project name",
Required: true,
MinLength: &repoNameMinLength,
},
},
},
Handler: c.createTicketHandler,
}
}
func (c *client) createTicketHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
var result string
// Access options in the order provided by the user.
options := i.ApplicationCommandData().Options
// Or convert the slice into a map
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
for _, opt := range options {
optionMap[opt.Name] = opt
}
if option, ok := optionMap["project_name"]; ok {
dchan, err := s.GuildChannelCreate(i.GuildID, option.StringValue(), discordgo.ChannelTypeGuildText)
if err != nil {
result = fmt.Sprintf("chan creation problem: %v\n", err)
} else {
p, err := c.controller.ProjectCreate(context.TODO(), domain.Project{
ChannelID: dchan.ID,
})
if err != nil {
result = fmt.Sprintf("unable to create project: %v\n", err)
_, err := s.ChannelDelete(dchan.ID)
if err != nil {
result += fmt.Sprintf("\nunable to clean channel: %v\n", err)
}
} else {
edit := discordgo.ChannelEdit{
Name: p.ShortName + "-" + helpers.Cut(option.StringValue()),
ParentID: c.conf.IsProjectChannel,
}
dchan, err = s.ChannelEdit(dchan.ID, &edit)
if err != nil {
result = fmt.Sprintf("channel %s created, but unable to edit follow up message: %v\n", p.ShortName, err)
} else {
_, err = s.ChannelMessageSend(dchan.ID, "Hello!")
if err != nil {
log.Printf("message send problem: %v\n", err)
}
result = "Project " + p.ShortName + " was created"
}
}
}
}
c.defaultFollowUp(result, s, i)
}