- 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"
)
func (h *router) CreateFolderHandler(nameMinLenght int) CommandRoute {
func (c *client) CreateFolderHandler(nameMinLenght int) Command {
const (
nameOption string = "folder_name"
)
return CommandRoute{
return Command{
Command: discordgo.ApplicationCommand{
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{
Type: discordgo.InteractionResponseChannelMessageWithSource,
@ -60,7 +68,7 @@ func (h *router) CreateFolderHandler(nameMinLenght int) CommandRoute {
log.Printf("error while identifying channel: %v", err)
} else {
if dchan.ParentID == h.conf.IsProjectChannel {
if dchan.ParentID == c.conf.IsProjectChannel {
req.ChannelID = dchan.ID
if insertedValueNotNil {
req.InsertedName = name.StringValue()
@ -75,7 +83,7 @@ func (h *router) CreateFolderHandler(nameMinLenght int) CommandRoute {
}
// Making request:
resp := h.controller.CreateFolder(context.TODO(), req)
resp := c.controller.CreateFolder(context.TODO(), req)
if resp.Project == nil {
result = "Надо написать имя для папки, или создать папку из проекта!"
} 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"
)
func (h *router) CreateRepoHandler(repoNameMinLength int) CommandRoute {
func (c *client) CreateRepoHandler(repoNameMinLength int) Command {
const (
repoType = "repo_type"
projectRepo = "project_repo"
@ -16,10 +16,10 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) CommandRoute {
nameOption = "repo_name"
)
return CommandRoute{
return Command{
Command: discordgo.ApplicationCommand{
Name: "repo",
Description: "Command for repository creation",
Description: "Creates repository of selected type. Name used for projects channels only",
Options: []*discordgo.ApplicationCommandOption{
{
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{
Type: discordgo.InteractionResponseChannelMessageWithSource,
@ -86,7 +96,7 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) CommandRoute {
log.Printf("error while identifying channel: %v", err)
} else {
if dchan.ParentID == h.conf.IsProjectChannel {
if dchan.ParentID == c.conf.IsProjectChannel {
req.ChannelID = dchan.ID
if insertedValueNotNil {
req.InsertedName = name.StringValue()
@ -100,13 +110,11 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) CommandRoute {
}
// Making request:
resp := h.controller.CreateGit(context.TODO(), req)
resp := c.controller.CreateGit(context.TODO(), req)
if resp.Project == nil {
result = resp.Message.Error()
} else {
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"
)
func (h *router) Ping() CommandRoute {
return CommandRoute{
func (c *client) Ping() Command {
return Command{
Command: discordgo.ApplicationCommand{
Name: "ping",
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{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
TTS: false,
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 {
log.Println(err)
}
},
}
}

View File

@ -9,13 +9,17 @@ import (
"github.com/bwmarrin/discordgo"
)
func (h *router) GetInfo() CommandRoute {
return CommandRoute{
func (c *client) GetInfo() Command {
return Command{
Command: discordgo.ApplicationCommand{
Name: "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{
@ -35,7 +39,7 @@ func (h *router) GetInfo() CommandRoute {
if err != nil {
result = "unable to get channel from the message"
} else {
project, err := h.controller.GetProjectByChannelID(context.TODO(), dchan.ID)
project, err := c.controller.GetProjectByChannelID(context.TODO(), dchan.ID)
if err != nil {
result = err.Error()
} else {
@ -44,33 +48,31 @@ func (h *router) GetInfo() CommandRoute {
result += "Errors: " + err.Error()
}
}
}
h.defaultFollowUp(result, s, i)
},
}
c.defaultFollowUp(result, s, i)
}
func (h *router) InitProjectFromChannel(minLength int) CommandRoute {
const (
keyOption = "key"
)
return CommandRoute{
func (c *client) InitProjectFromChannel(minLength int) Command {
return Command{
Command: discordgo.ApplicationCommand{
Name: "init_project",
Description: "Connect project with Coda ID",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: keyOption,
Name: "key",
Description: "Project's key from Coda.io",
Required: true,
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{
@ -90,7 +92,7 @@ func (h *router) InitProjectFromChannel(minLength int) CommandRoute {
if err != nil {
result = "unable to get channel from the message"
} else {
if dchan.ParentID != h.conf.IsProjectChannel {
if dchan.ParentID != c.conf.IsProjectChannel {
// Sending result:
_, err := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
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
}
if option, ok := optionMap[keyOption]; ok {
if option, ok := optionMap["key"]; ok {
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 {
result = fmt.Sprintf("unable to init project: %v", err)
} 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 {
return CommandRoute{
func (c *client) CreateTicketHandler(repoNameMinLength int) Command {
return Command{
Command: discordgo.ApplicationCommand{
Name: "project",
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{
@ -177,7 +181,7 @@ func (h *router) CreateTicketHandler(repoNameMinLength int) CommandRoute {
if err != nil {
result = fmt.Sprintf("chan creation problem: %v\n", err)
} else {
p, err := h.controller.ProjectCreate(context.TODO(), domain.Project{
p, err := c.controller.ProjectCreate(context.TODO(), domain.Project{
ChannelID: dchan.ID,
})
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.Text(" was created"),
),
tg.HTML.Line(
"Заходи в наш",
tg.HTML.Link("discord", "https://discord.gg/RHdzK3kUr7"),
"чтобы отслеживать статус по задаче",
),
)
if mu.Caption != "" {
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.Line(
tg.HTML.Italic(
tg.HTML.Bold("Task ID: "),
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"),
"чтобы отслеживать статус по задаче",
),
)
}