ticket-pimp/discord/handler/handle_folder.go

101 lines
2.7 KiB
Go

package handler
import (
"context"
"fmt"
"log"
"strconv"
"github.com/bwmarrin/discordgo"
)
func (h *router) CreateFolderHandler(nameMinLenght int) route {
const (
nameOption string = "folder_name"
)
return route{
Command: discordgo.ApplicationCommand{
Name: "folder",
Description: "Command for cloud folder creation",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: nameOption,
Description: "Type the folder's name",
Required: false,
MinLength: &nameMinLenght,
},
},
},
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
initialResponse := discordgo.InteractionResponse{
// Ignore type for now, they will be discussed in "responses"
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "..folder is going to be created",
Title: "📂 Folder creation",
},
}
s.InteractionRespond(i.Interaction, &initialResponse)
var result string = "unexpected result"
options := i.ApplicationCommandData().Options
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
for _, opt := range options {
optionMap[opt.Name] = opt
}
var str string = ""
dchan, err := s.Channel(i.ChannelID)
if err != nil {
log.Printf("error while identifying channel: %v", err)
}
if dchan.ParentID == strconv.Itoa(1150719794853716028) {
log.Println("yes, channel is from `Projects`")
project, err := h.controller.GetProjectByChannelID(context.TODO(), i.ChannelID)
if err != nil {
result = fmt.Sprintf("unable to retrieve project from db, error: %v", err)
} else {
switch {
case project == nil:
if option, ok := optionMap[nameOption]; ok {
str = option.StringValue()
} else {
str = "Ты, либо в проекте директорию создавай, либо имя напиши, блет!"
}
default:
str = project.ShortName
}
resp := h.controller.ICloud.CreateFolder(str)
if resp.ErrMessage != nil {
result = fmt.Sprintf("Command executed w/ errors: ``` %v``` \n But check this link: %s\n", resp.ErrMessage, resp.Folder.PrivateURL)
} else {
result = fmt.Sprintf("📂 Folder was created: %s", resp.Folder.PrivateURL)
}
}
_, err = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Content: result,
})
if err != nil {
s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Content: fmt.Sprintf("Something went wrong: %v", err),
})
return
}
}
},
}
}