From 6752028e00b2f603d39e32701658eb434e1ea352 Mon Sep 17 00:00:00 2001 From: naudachu Date: Tue, 7 Nov 2023 16:36:58 +0500 Subject: [PATCH] idk... regulat stash --- discord/discord.go | 10 +- discord/handler/handler.go | 191 +++++++++++++++++++++++-------------- 2 files changed, 122 insertions(+), 79 deletions(-) diff --git a/discord/discord.go b/discord/discord.go index 459aa6a..c2db60a 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -39,32 +39,32 @@ func Run(conf domain.Config, opts DiscordOptions) error { for _, handler := range router.Routes { commandHandlers[handler.Command.Name] = handler.Handler } + session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { + if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok { h(s, i) } }) - // for _, h := range router.Routes { - // session.AddHandler(h.Handler) // [ ] Как-то я неправильно хэндлеры добавляю, на проджект отрабатывает /repo - // } - if err := session.Open(); err != nil { return fmt.Errorf("cannot open the session: %v", err) } log.Println("Adding commands...") var cmds []*discordgo.ApplicationCommand + var logString []string for _, h := range router.Routes { cmd, err := session.ApplicationCommandCreate(session.State.User.ID, "", &h.Command) if err != nil { log.Panicf("Cannot create '%v' command: %v", h.Command.Name, err) } cmds = append(cmds, cmd) + logString = append(logString, cmd.Name) } log.Println("Following commands added:") - log.Println(cmds) + log.Println(logString) defer session.Close() stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt) diff --git a/discord/handler/handler.go b/discord/handler/handler.go index 08b77f0..b954eee 100644 --- a/discord/handler/handler.go +++ b/discord/handler/handler.go @@ -21,9 +21,10 @@ func InitRouter(wc controller.WorkflowController) *router { var r router r.Routes = append( r.Routes, - r.CreateRepoHandler(3), + // r.CreateRepoHandler(3), r.CreateFolderHandler(3), - r.CreateTicketHandler(3), + r.Ping(), + // r.CreateTicketHandler(3), ) r.controller = wc @@ -35,6 +36,118 @@ type route struct { Handler func(s *discordgo.Session, i *discordgo.InteractionCreate) } +func (h *router) Ping() route { + return route{ + Command: discordgo.ApplicationCommand{ + Name: "ping", + Description: "pongs in a reply", + }, + Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + log.Println("ok, I'm here..") + + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseDeferredMessageUpdate, + Data: &discordgo.InteractionResponseData{ + Content: "`pong`", + Title: "Pong reply", + }, + }) + }, + } +} + +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) { + var result string + + resp := discordgo.InteractionResponse{ + + Type: discordgo.InteractionResponseUpdateMessage, + Data: &discordgo.InteractionResponseData{ + Content: "Folder is going to be created..", + }, + } + err := s.InteractionRespond(i.Interaction, &resp) + if err != nil { + log.Println(err) + return + } + + options := i.ApplicationCommandData().Options + + optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options)) + for _, opt := range options { + optionMap[opt.Name] = opt + } + + var str string = "" + + 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 { + if project == nil { + if option, ok := optionMap[nameOption]; ok { + str = option.StringValue() + } else { + str = "" + } + } else { + str = project.ShortName + } + + if str == "" { + result = "Ты, либо в проекте директорию создавай, либо имя напиши, блет!" + } else { + + f, err := h.controller.ICloud.CreateFolder(str) + if err != nil { + result = fmt.Sprintf("error while cloud folder creation: %v", err) + } else { + result = fmt.Sprint(f.PrivateURL) + } + } + } + + // resp = discordgo.InteractionResponse{ + // // Ignore type for now, they will be discussed in "responses" + // Type: discordgo.InteractionResponseUpdateMessage, + // Data: &discordgo.InteractionResponseData{ + // Content: result, + // Title: "📂 Folder was created", + // }, + // } + + webhookEdit := discordgo.WebhookEdit{ + Content: &result, + } + s.InteractionResponseEdit(i.Interaction, &webhookEdit) + + // discerr := s.InteractionRespond(i.Interaction, &resp) + // if discerr != nil { + // log.Println(discerr) + // } + }, + } +} + func (h *router) CreateRepoHandler(repoNameMinLength int) route { const ( projectRepo = "project_repo" @@ -124,84 +237,14 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) route { } } - discerr := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - // Ignore type for now, they will be discussed in "responses" + resp := &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ Content: result, }, - }) - log.Println(discerr) - }, - } -} - -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 repository's name", - Required: false, - MinLength: &nameMinLenght, - }, - }, - }, - Handler: func(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 } - var str string = "" - - 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 { - if project == nil { - if option, ok := optionMap[nameOption]; ok { - str = option.StringValue() - } else { - str = "" - } - } else { - str = project.ShortName - } - - if str == "" { - result = "Ты, либо в проекте директорию создавай, либо имя напиши, блет!" - } else { - - f, err := h.controller.ICloud.CreateFolder(str) - if err != nil { - result = fmt.Sprintf("error while cloud folder creation: %v", err) - } else { - result = "📂 Folder: " + f.PrivateURL + " was created" - } - } - } - - discerr := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - // Ignore type for now, they will be discussed in "responses" - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: result, - }, - }) - log.Println(discerr) + s.InteractionRespond(i.Interaction, resp) }, } }