181 lines
4.7 KiB
Go
181 lines
4.7 KiB
Go
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: "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) {
|
|
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 {
|
|
log.Printf("chan creation problem: %v\n", err)
|
|
}
|
|
|
|
// permissions := discordgo.PermissionOverwrite{
|
|
// ID: i.User.ID,
|
|
// Type: 1,
|
|
// //Deny: 0,
|
|
// Allow: 1,
|
|
// }
|
|
edit := discordgo.ChannelEdit{
|
|
// PermissionOverwrites: []*discordgo.PermissionOverwrite{&permissions}, // [ ] Как сделать приватный канал то???
|
|
ParentID: "1150719794853716028",
|
|
}
|
|
dchan, err = s.ChannelEdit(dchan.ID, &edit)
|
|
if err != nil {
|
|
log.Printf("chan editing problem: %v\n", err)
|
|
}
|
|
_, err = s.ChannelMessageSend(dchan.ID, "Hello!")
|
|
if err != nil {
|
|
log.Printf("message send problem: %v\n", err)
|
|
}
|
|
result = dchan.ID
|
|
}
|
|
|
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
// Ignore type for now, they will be discussed in "responses"
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
Data: &discordgo.InteractionResponseData{
|
|
Content: result,
|
|
},
|
|
})
|
|
},
|
|
}
|
|
}
|