package handler import ( "context" "fmt" "log" "ticket-pimp/internal/domain" "github.com/bwmarrin/discordgo" ) func (h *router) InitProjectFromChannel(minLength int) route { const ( keyOption = "key" ) return route{ Command: discordgo.ApplicationCommand{ Name: "init_project", Description: "Connect project with Coda ID", Options: []*discordgo.ApplicationCommandOption{ { Type: discordgo.ApplicationCommandOptionString, Name: keyOption, Description: "Project's key from Coda.io", Required: true, MinLength: &minLength, }, }, }, 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 // 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 != h.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[keyOption]; ok { var errMsg error = nil project, err := h.controller.InitProjectInChannel(context.TODO(), i.ChannelID, option.StringValue()) if err != nil { errMsg = err } else { result = fmt.Sprintf( "## Project info:\n🔑 key: %s\n📂 folder: %s\n👾 project git: %s\n🚀 build git: %s\n\nErrors: %v", project.ShortName, project.Cloud, project.ProjectGit, project.BuildGit, errMsg, ) } } } // Sending result: _, 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 } }, } } func (h *router) CreateTicketHandler(repoNameMinLength int) route { return route{ 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: 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 // 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 := h.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, ParentID: "1150719794853716028", } dchan, err = s.ChannelEdit(dchan.ID, &edit) if err != nil { result = fmt.Sprintf("channel created, but unable to edit: %v\n", err) } else { _, err = s.ChannelMessageSend(dchan.ID, "Hello!") if err != nil { log.Printf("message send problem: %v\n", err) } result = dchan.ID } } } } // Sending result: _, 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 } }, } }