idk... regulat stash
This commit is contained in:
parent
6f4824e628
commit
6752028e00
|
|
@ -39,32 +39,32 @@ func Run(conf domain.Config, opts DiscordOptions) error {
|
||||||
for _, handler := range router.Routes {
|
for _, handler := range router.Routes {
|
||||||
commandHandlers[handler.Command.Name] = handler.Handler
|
commandHandlers[handler.Command.Name] = handler.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
|
||||||
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
|
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
|
||||||
h(s, i)
|
h(s, i)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// for _, h := range router.Routes {
|
|
||||||
// session.AddHandler(h.Handler) // [ ] Как-то я неправильно хэндлеры добавляю, на проджект отрабатывает /repo
|
|
||||||
// }
|
|
||||||
|
|
||||||
if err := session.Open(); err != nil {
|
if err := session.Open(); err != nil {
|
||||||
return fmt.Errorf("cannot open the session: %v", err)
|
return fmt.Errorf("cannot open the session: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Adding commands...")
|
log.Println("Adding commands...")
|
||||||
var cmds []*discordgo.ApplicationCommand
|
var cmds []*discordgo.ApplicationCommand
|
||||||
|
var logString []string
|
||||||
for _, h := range router.Routes {
|
for _, h := range router.Routes {
|
||||||
cmd, err := session.ApplicationCommandCreate(session.State.User.ID, "", &h.Command)
|
cmd, err := session.ApplicationCommandCreate(session.State.User.ID, "", &h.Command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Cannot create '%v' command: %v", h.Command.Name, err)
|
log.Panicf("Cannot create '%v' command: %v", h.Command.Name, err)
|
||||||
}
|
}
|
||||||
cmds = append(cmds, cmd)
|
cmds = append(cmds, cmd)
|
||||||
|
logString = append(logString, cmd.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Following commands added:")
|
log.Println("Following commands added:")
|
||||||
log.Println(cmds)
|
log.Println(logString)
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
stop := make(chan os.Signal, 1)
|
stop := make(chan os.Signal, 1)
|
||||||
signal.Notify(stop, os.Interrupt)
|
signal.Notify(stop, os.Interrupt)
|
||||||
|
|
|
||||||
|
|
@ -21,9 +21,10 @@ func InitRouter(wc controller.WorkflowController) *router {
|
||||||
var r router
|
var r router
|
||||||
r.Routes = append(
|
r.Routes = append(
|
||||||
r.Routes,
|
r.Routes,
|
||||||
r.CreateRepoHandler(3),
|
// r.CreateRepoHandler(3),
|
||||||
r.CreateFolderHandler(3),
|
r.CreateFolderHandler(3),
|
||||||
r.CreateTicketHandler(3),
|
r.Ping(),
|
||||||
|
// r.CreateTicketHandler(3),
|
||||||
)
|
)
|
||||||
r.controller = wc
|
r.controller = wc
|
||||||
|
|
||||||
|
|
@ -35,6 +36,118 @@ type route struct {
|
||||||
Handler func(s *discordgo.Session, i *discordgo.InteractionCreate)
|
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 {
|
func (h *router) CreateRepoHandler(repoNameMinLength int) route {
|
||||||
const (
|
const (
|
||||||
projectRepo = "project_repo"
|
projectRepo = "project_repo"
|
||||||
|
|
@ -124,84 +237,14 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) route {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
discerr := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
resp := &discordgo.InteractionResponse{
|
||||||
// Ignore type for now, they will be discussed in "responses"
|
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &discordgo.InteractionResponseData{
|
Data: &discordgo.InteractionResponseData{
|
||||||
Content: result,
|
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 = ""
|
s.InteractionRespond(i.Interaction, resp)
|
||||||
|
|
||||||
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)
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue