- added separate functions as handlers;

This commit is contained in:
naudachu 2023-11-17 13:47:47 +05:00
parent 972e6564ea
commit f677ec5986
5 changed files with 338 additions and 316 deletions

View File

@ -8,11 +8,11 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
func (h *router) CreateFolderHandler(nameMinLenght int) CommandRoute { func (c *client) CreateFolderHandler(nameMinLenght int) Command {
const ( const (
nameOption string = "folder_name" nameOption string = "folder_name"
) )
return CommandRoute{ return Command{
Command: discordgo.ApplicationCommand{ Command: discordgo.ApplicationCommand{
Name: "folder", Name: "folder",
@ -28,7 +28,15 @@ func (h *router) CreateFolderHandler(nameMinLenght int) CommandRoute {
}, },
}, },
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) { Handler: c.createFolderHandler,
}
}
func (c *client) createFolderHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
const (
nameOption string = "folder_name"
)
// Моментальный ответ для избежания столкновения с протуханием токена // Моментальный ответ для избежания столкновения с протуханием токена
initialResponse := discordgo.InteractionResponse{ initialResponse := discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
@ -60,7 +68,7 @@ func (h *router) CreateFolderHandler(nameMinLenght int) CommandRoute {
log.Printf("error while identifying channel: %v", err) log.Printf("error while identifying channel: %v", err)
} else { } else {
if dchan.ParentID == h.conf.IsProjectChannel { if dchan.ParentID == c.conf.IsProjectChannel {
req.ChannelID = dchan.ID req.ChannelID = dchan.ID
if insertedValueNotNil { if insertedValueNotNil {
req.InsertedName = name.StringValue() req.InsertedName = name.StringValue()
@ -75,7 +83,7 @@ func (h *router) CreateFolderHandler(nameMinLenght int) CommandRoute {
} }
// Making request: // Making request:
resp := h.controller.CreateFolder(context.TODO(), req) resp := c.controller.CreateFolder(context.TODO(), req)
if resp.Project == nil { if resp.Project == nil {
result = "Надо написать имя для папки, или создать папку из проекта!" result = "Надо написать имя для папки, или создать папку из проекта!"
} else { } else {
@ -85,7 +93,5 @@ func (h *router) CreateFolderHandler(nameMinLenght int) CommandRoute {
} }
} }
h.defaultFollowUp(result, s, i) c.defaultFollowUp(result, s, i)
},
}
} }

View File

@ -8,7 +8,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
func (h *router) CreateRepoHandler(repoNameMinLength int) CommandRoute { func (c *client) CreateRepoHandler(repoNameMinLength int) Command {
const ( const (
repoType = "repo_type" repoType = "repo_type"
projectRepo = "project_repo" projectRepo = "project_repo"
@ -16,10 +16,10 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) CommandRoute {
nameOption = "repo_name" nameOption = "repo_name"
) )
return CommandRoute{ return Command{
Command: discordgo.ApplicationCommand{ Command: discordgo.ApplicationCommand{
Name: "repo", Name: "repo",
Description: "Command for repository creation", Description: "Creates repository of selected type. Name used for projects channels only",
Options: []*discordgo.ApplicationCommandOption{ Options: []*discordgo.ApplicationCommandOption{
{ {
Type: discordgo.ApplicationCommandOptionString, Type: discordgo.ApplicationCommandOptionString,
@ -46,7 +46,17 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) CommandRoute {
}, },
}, },
}, },
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) { Handler: c.createRepoHandler,
}
}
func (c *client) createRepoHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
const (
repoType = "repo_type"
projectRepo = "project_repo"
buildRepo = "build_repo"
nameOption = "repo_name"
)
// Моментальный ответ для избежания столкновения с протуханием токена // Моментальный ответ для избежания столкновения с протуханием токена
initialResponse := discordgo.InteractionResponse{ initialResponse := discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
@ -86,7 +96,7 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) CommandRoute {
log.Printf("error while identifying channel: %v", err) log.Printf("error while identifying channel: %v", err)
} else { } else {
if dchan.ParentID == h.conf.IsProjectChannel { if dchan.ParentID == c.conf.IsProjectChannel {
req.ChannelID = dchan.ID req.ChannelID = dchan.ID
if insertedValueNotNil { if insertedValueNotNil {
req.InsertedName = name.StringValue() req.InsertedName = name.StringValue()
@ -100,13 +110,11 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) CommandRoute {
} }
// Making request: // Making request:
resp := h.controller.CreateGit(context.TODO(), req) resp := c.controller.CreateGit(context.TODO(), req)
if resp.Project == nil { if resp.Project == nil {
result = resp.Message.Error() result = resp.Message.Error()
} else { } else {
result = resp.Project.DiscordString() + "Errors: " + resp.Message.Error() result = resp.Project.DiscordString() + "Errors: " + resp.Message.Error()
} }
h.defaultFollowUp(result, s, i) c.defaultFollowUp(result, s, i)
},
}
} }

View File

@ -6,31 +6,25 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
func (h *router) Ping() CommandRoute { func (c *client) Ping() Command {
return CommandRoute{ return Command{
Command: discordgo.ApplicationCommand{ Command: discordgo.ApplicationCommand{
Name: "ping", Name: "ping",
Description: "pongs in a reply", Description: "pongs in a reply",
}, },
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) { Handler: c.ping,
}
}
func (c *client) ping(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
TTS: false,
Content: "Pong to: " + i.Member.User.Mention(), Content: "Pong to: " + i.Member.User.Mention(),
Components: []discordgo.MessageComponent{},
Embeds: []*discordgo.MessageEmbed{},
Files: []*discordgo.File{},
Flags: 0,
Choices: []*discordgo.ApplicationCommandOptionChoice{},
CustomID: "",
Title: "",
}, },
}) })
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
},
}
} }

View File

@ -9,13 +9,17 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
func (h *router) GetInfo() CommandRoute { func (c *client) GetInfo() Command {
return CommandRoute{ return Command{
Command: discordgo.ApplicationCommand{ Command: discordgo.ApplicationCommand{
Name: "info", Name: "info",
Description: "Get project's info", Description: "Get project's info",
}, },
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) { Handler: c.getInfo,
}
}
func (c *client) getInfo(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Моментальный ответ для избежания столкновения с протуханием токена // Моментальный ответ для избежания столкновения с протуханием токена
initialResponse := discordgo.InteractionResponse{ initialResponse := discordgo.InteractionResponse{
@ -35,7 +39,7 @@ func (h *router) GetInfo() CommandRoute {
if err != nil { if err != nil {
result = "unable to get channel from the message" result = "unable to get channel from the message"
} else { } else {
project, err := h.controller.GetProjectByChannelID(context.TODO(), dchan.ID) project, err := c.controller.GetProjectByChannelID(context.TODO(), dchan.ID)
if err != nil { if err != nil {
result = err.Error() result = err.Error()
} else { } else {
@ -44,33 +48,31 @@ func (h *router) GetInfo() CommandRoute {
result += "Errors: " + err.Error() result += "Errors: " + err.Error()
} }
} }
} }
h.defaultFollowUp(result, s, i) c.defaultFollowUp(result, s, i)
},
}
} }
func (h *router) InitProjectFromChannel(minLength int) CommandRoute { func (c *client) InitProjectFromChannel(minLength int) Command {
const ( return Command{
keyOption = "key"
)
return CommandRoute{
Command: discordgo.ApplicationCommand{ Command: discordgo.ApplicationCommand{
Name: "init_project", Name: "init_project",
Description: "Connect project with Coda ID", Description: "Connect project with Coda ID",
Options: []*discordgo.ApplicationCommandOption{ Options: []*discordgo.ApplicationCommandOption{
{ {
Type: discordgo.ApplicationCommandOptionString, Type: discordgo.ApplicationCommandOptionString,
Name: keyOption, Name: "key",
Description: "Project's key from Coda.io", Description: "Project's key from Coda.io",
Required: true, Required: true,
MinLength: &minLength, MinLength: &minLength,
}, },
}, },
}, },
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) { Handler: c.initProjectFromChannel,
}
}
func (c *client) initProjectFromChannel(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Моментальный ответ для избежания столкновения с протуханием токена // Моментальный ответ для избежания столкновения с протуханием токена
initialResponse := discordgo.InteractionResponse{ initialResponse := discordgo.InteractionResponse{
@ -90,7 +92,7 @@ func (h *router) InitProjectFromChannel(minLength int) CommandRoute {
if err != nil { if err != nil {
result = "unable to get channel from the message" result = "unable to get channel from the message"
} else { } else {
if dchan.ParentID != h.conf.IsProjectChannel { if dchan.ParentID != c.conf.IsProjectChannel {
// Sending result: // Sending result:
_, err := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{ _, err := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Content: "This channel is not at the project's group", Content: "This channel is not at the project's group",
@ -114,10 +116,10 @@ func (h *router) InitProjectFromChannel(minLength int) CommandRoute {
optionMap[opt.Name] = opt optionMap[opt.Name] = opt
} }
if option, ok := optionMap[keyOption]; ok { if option, ok := optionMap["key"]; ok {
var errMsg error = nil var errMsg error = nil
project, err := h.controller.InitProjectInChannel(context.TODO(), i.ChannelID, option.StringValue()) project, err := c.controller.InitProjectInChannel(context.TODO(), i.ChannelID, option.StringValue())
if err != nil { if err != nil {
result = fmt.Sprintf("unable to init project: %v", err) result = fmt.Sprintf("unable to init project: %v", err)
} else { } else {
@ -129,13 +131,11 @@ func (h *router) InitProjectFromChannel(minLength int) CommandRoute {
} }
} }
h.defaultFollowUp(result, s, i) c.defaultFollowUp(result, s, i)
},
}
} }
func (h *router) CreateTicketHandler(repoNameMinLength int) CommandRoute { func (c *client) CreateTicketHandler(repoNameMinLength int) Command {
return CommandRoute{ return Command{
Command: discordgo.ApplicationCommand{ Command: discordgo.ApplicationCommand{
Name: "project", Name: "project",
Description: "Create new development ticket", Description: "Create new development ticket",
@ -149,7 +149,11 @@ func (h *router) CreateTicketHandler(repoNameMinLength int) CommandRoute {
}, },
}, },
}, },
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) { Handler: c.createTicketHandler,
}
}
func (c *client) createTicketHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Моментальный ответ для избежания столкновения с протуханием токена // Моментальный ответ для избежания столкновения с протуханием токена
initialResponse := discordgo.InteractionResponse{ initialResponse := discordgo.InteractionResponse{
@ -177,7 +181,7 @@ func (h *router) CreateTicketHandler(repoNameMinLength int) CommandRoute {
if err != nil { if err != nil {
result = fmt.Sprintf("chan creation problem: %v\n", err) result = fmt.Sprintf("chan creation problem: %v\n", err)
} else { } else {
p, err := h.controller.ProjectCreate(context.TODO(), domain.Project{ p, err := c.controller.ProjectCreate(context.TODO(), domain.Project{
ChannelID: dchan.ID, ChannelID: dchan.ID,
}) })
if err != nil { if err != nil {
@ -207,7 +211,5 @@ func (h *router) CreateTicketHandler(repoNameMinLength int) CommandRoute {
} }
} }
h.defaultFollowUp(result, s, i) c.defaultFollowUp(result, s, i)
},
}
} }

View File

@ -60,6 +60,11 @@ func (h *Handler) FarmTaskHandler(ctx context.Context, mu *tgb.MessageUpdate) er
tg.HTML.Code(i), tg.HTML.Code(i),
tg.HTML.Text(" was created"), tg.HTML.Text(" was created"),
), ),
tg.HTML.Line(
"Заходи в наш",
tg.HTML.Link("discord", "https://discord.gg/RHdzK3kUr7"),
"чтобы отслеживать статус по задаче",
),
) )
if mu.Caption != "" { if mu.Caption != "" {
answer = tg.HTML.Text( answer = tg.HTML.Text(
@ -67,9 +72,16 @@ func (h *Handler) FarmTaskHandler(ctx context.Context, mu *tgb.MessageUpdate) er
tg.HTML.Bold("I'm unable to work with files, but"), tg.HTML.Bold("I'm unable to work with files, but"),
), ),
tg.HTML.Line( tg.HTML.Line(
tg.HTML.Italic(
tg.HTML.Bold("Task ID: "), tg.HTML.Bold("Task ID: "),
tg.HTML.Code(i), tg.HTML.Code(i),
tg.HTML.Text(" was created"), tg.HTML.Text(" was created")),
),
tg.HTML.Line(
"Заходи в наш",
tg.HTML.Link("discord", "https://discord.gg/RHdzK3kUr7"),
"чтобы отслеживать статус по задаче",
), ),
) )
} }