package handler import ( "fmt" "log" "ticket-pimp/internal/domain" "ticket-pimp/internal/services" "github.com/bwmarrin/discordgo" ) type router struct { Routes []route git services.IGit } // Подключение роутов к Discord боту func InitRouter(gitService services.IGit) *router { var r router r.Routes = append( r.Routes, r.CreateRepoHandler(3), // r.CreateTicketHandler(3), ) r.git = gitService return &r } type route struct { Command discordgo.ApplicationCommand Handler func(s *discordgo.Session, i *discordgo.InteractionCreate) } func (h *router) CreateRepoHandler(repoNameMinLength int) route { const ( projectRepo = "project_repo" buildRepo = "build_repo" ) return route{ Command: discordgo.ApplicationCommand{ Name: "repo", Description: "Command for repository creation", Options: []*discordgo.ApplicationCommandOption{ { Type: discordgo.ApplicationCommandOptionString, Name: "repo_type", Description: "The type of repo", Required: true, Choices: []*discordgo.ApplicationCommandOptionChoice{ { Name: "Unity project repo", Value: projectRepo, }, { Name: "XCode build repo", Value: buildRepo, }, }, }, { Type: discordgo.ApplicationCommandOptionString, Name: "repo_name", Description: "Type the repository's name", Required: true, MinLength: &repoNameMinLength, }, }, }, Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) { // 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 = "" if option, ok := optionMap["repo_name"]; ok { str = option.StringValue() if option, ok := optionMap["repo_type"]; ok { switch option.Value { case projectRepo: case buildRepo: str += "-build" } } } var g *domain.Git var result string g, err := h.git.CreateRepo(str) if err != nil { result = fmt.Sprintf("error while repo creation: %v", err) } else { result = "🚀 " + g.HtmlUrl + " 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) }, } } func (h *router) CreateTicketHandler(repoNameMinLength int) route { return route{ Command: discordgo.ApplicationCommand{ Name: "new", 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) { // 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 } // This example stores the provided arguments in an []interface{} // which will be used to format the bot's response margs := make([]interface{}, 0, len(options)) msgformat := "You learned how to use command options! " + "Take a look at the value(s) you entered:\n" if option, ok := optionMap["project_name"]; ok { // Option values must be type asserted from interface{}. // Discordgo provides utility functions to make this simple. margs = append(margs, option.StringValue()) msgformat += "> string-option: %s\n" } s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ // Ignore type for now, they will be discussed in "responses" Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ Content: fmt.Sprintf( msgformat, margs..., ), }, }) }, } }