89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
package handler
|
||
|
||
import (
|
||
"context"
|
||
"log"
|
||
"ticket-pimp/internal/controller"
|
||
|
||
"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{
|
||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||
Data: &discordgo.InteractionResponseData{
|
||
Flags: discordgo.MessageFlagsEphemeral,
|
||
Content: "👩🍳 Cooking your query..",
|
||
},
|
||
}
|
||
|
||
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
|
||
}
|
||
|
||
// Creating request:
|
||
var req controller.FolderRequest
|
||
name, insertedValueNotNil := optionMap[nameOption]
|
||
dchan, err := s.Channel(i.ChannelID)
|
||
|
||
if err != nil {
|
||
log.Printf("error while identifying channel: %v", err)
|
||
} else {
|
||
|
||
if dchan.ParentID == h.conf.IsProjectChannel {
|
||
req.ChannelID = dchan.ID
|
||
if insertedValueNotNil {
|
||
req.InsertedName = name.StringValue()
|
||
}
|
||
|
||
} else {
|
||
req.ChannelID = ""
|
||
if insertedValueNotNil {
|
||
req.InsertedName = name.StringValue()
|
||
}
|
||
}
|
||
}
|
||
|
||
// Making request:
|
||
resp := h.controller.CreateFolder(context.TODO(), req)
|
||
if resp.Project == nil {
|
||
result = "Надо написать имя для папки, или создать папку из проекта!"
|
||
} else {
|
||
result = resp.Project.DiscordString() + "Errors: " + resp.Message.Error()
|
||
}
|
||
|
||
h.defaultFollowUp(result, s, i)
|
||
},
|
||
}
|
||
}
|