- middleware for whole interactions array

- Create Project handler method refactoring
This commit is contained in:
naudachu 2023-11-24 14:56:22 +05:00
parent b9614e4c45
commit bbe678fa46
2 changed files with 101 additions and 114 deletions

View File

@ -21,35 +21,6 @@ func (h *Handler) defaultFollowUp(answer string, s *discordgo.Session, i *discor
}
}
func (h *Handler) AllInteractions(s *discordgo.Session, i *discordgo.InteractionCreate) {
dchan, err := s.Channel(i.ChannelID)
if err != nil {
return
}
if dchan.Type == discordgo.ChannelTypeDM {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Yo, fella! I'm not working in private!",
},
})
return
}
// Моментальный ответ для избежания столкновения с протуханием токена
initialResponse := discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "👩‍🍳 Cooking your query..",
},
}
s.InteractionRespond(i.Interaction, &initialResponse)
}
// setFlag
// sets tag with In progress and Done text to discords channel;
func (h *Handler) setFlag(s *discordgo.Session, i *discordgo.InteractionCreate, tag *discordgo.ForumTag) error {

View File

@ -7,6 +7,7 @@ import (
"ticket-pimp/adapters"
"ticket-pimp/internal/controller"
"ticket-pimp/internal/domain"
"ticket-pimp/internal/helpers"
"github.com/bwmarrin/discordgo"
)
@ -22,23 +23,30 @@ func New(
controller *controller.WorkflowController,
conf *domain.DiscordConfig,
tg adapters.IDummyTelegram,
tags []discordgo.ForumTag,
) *Handler {
return &Handler{
controller: controller,
conf: conf,
telegramDummyClient: tg,
tags: tags,
}
}
func (h *Handler) SetAvailableTags(tags []discordgo.ForumTag) {
h.tags = append(h.tags, tags...)
}
func (h *Handler) Ping(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Pong to: " + i.Member.User.Mention(),
},
var content string = fmt.Sprintf(
"**Pong to:** %s\n**App ID:** %s\n**Guild ID:** %s\nC**hannelID:** %s",
i.Member.User.Mention(),
i.AppID,
i.GuildID,
i.ChannelID,
)
_, err := s.FollowupMessageCreate(i.Interaction, false, &discordgo.WebhookParams{
Content: content,
})
if err != nil {
log.Println(err)
@ -46,8 +54,10 @@ func (h *Handler) Ping(s *discordgo.Session, i *discordgo.InteractionCreate) {
}
// ListenPosts
// ..listens to new posts in specific channel
// to act them like a task
/*
..listens to new posts in specific channel
to act them like a task
*/
func (h *Handler) ListenPosts(s *discordgo.Session, th *discordgo.ThreadCreate) {
// Check if thread starter is not a bot, and thread started at the tasks channel;
@ -77,7 +87,7 @@ func (h *Handler) ListenPosts(s *discordgo.Session, th *discordgo.ThreadCreate)
return
}
// [x] -- Отредактировать Thread name как для задачи
// Отредактировать Thread name как для задачи
_, err = s.ChannelEditComplex(th.ID, &discordgo.ChannelEdit{
Name: fmt.Sprintf("Task ID: %d, by %s", t.ID, t.Creator),
})
@ -118,16 +128,10 @@ func (h *Handler) ListenPosts(s *discordgo.Session, th *discordgo.ThreadCreate)
}
}
// handleTaskBurrons
// handleTaskButtons
// .. handler function to work with the Action Buttons over a task
func (h *Handler) HandleTaskButtons(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Send an empty interaction response; ----------------------------------------------------------------
// s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
// Type: discordgo.InteractionResponseUpdateMessage,
// Data: &discordgo.InteractionResponseData{},
// })
// Get assignee value; ---------------------------------------------------------------------------------
user := i.Member.User.Mention()
@ -212,6 +216,11 @@ func (h *Handler) HandleTaskButtons(s *discordgo.Session, i *discordgo.Interacti
}
}
// CreateFolder
/*
- creates project's cloud folder;
- writed folder link to db;
*/
func (h *Handler) CreateFolder(s *discordgo.Session, i *discordgo.InteractionCreate) {
const (
nameOption string = "folder_name"
@ -276,6 +285,11 @@ func (h *Handler) CreateFolder(s *discordgo.Session, i *discordgo.InteractionCre
h.defaultFollowUp(result, s, i)
}
// CreateGit
/*
-creates project's git repository;
- writed git link to db;
*/
func (h *Handler) CreateGit(s *discordgo.Session, i *discordgo.InteractionCreate) {
const (
repoType = "repo_type"
@ -352,20 +366,12 @@ func (h *Handler) CreateGit(s *discordgo.Session, i *discordgo.InteractionCreate
h.defaultFollowUp(result, s, i)
}
// PROJECT
// ProjectInfo
/*
Message in chat with related project information
*/
func (h *Handler) ProjectInfo(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Моментальный ответ для избежания столкновения с протуханием токена
initialResponse := discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "👩‍🍳 Cooking your query..",
},
}
s.InteractionRespond(i.Interaction, &initialResponse)
var result string
// Get channel from the request
@ -392,19 +398,12 @@ func (h *Handler) ProjectInfo(s *discordgo.Session, i *discordgo.InteractionCrea
h.defaultFollowUp(result, s, i)
}
// InitChannelAsProject
/*
- makes channel-project raw in the db storage;
*/
func (h *Handler) InitChannelAsProject(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Моментальный ответ для избежания столкновения с протуханием токена
initialResponse := discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "👩‍🍳 Cooking your query..",
},
}
s.InteractionRespond(i.Interaction, &initialResponse)
var result string
// Get channel from the request
@ -454,18 +453,12 @@ func (h *Handler) InitChannelAsProject(s *discordgo.Session, i *discordgo.Intera
h.defaultFollowUp(result, s, i)
}
func (h *Handler) CreateTicket(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Моментальный ответ для избежания столкновения с протуханием токена
initialResponse := discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "👩‍🍳 Cooking your query..",
},
}
s.InteractionRespond(i.Interaction, &initialResponse)
// CreateProject
/*
- creates new proejct in the db;
- creates new channel and writes it to db;
*/
func (h *Handler) CreateProject(s *discordgo.Session, i *discordgo.InteractionCreate) {
var result string
// Access options in the order provided by the user.
@ -477,40 +470,63 @@ func (h *Handler) CreateTicket(s *discordgo.Session, i *discordgo.InteractionCre
optionMap[opt.Name] = opt
}
if option, ok := optionMap["project_name"]; ok {
dchan, err := s.GuildChannelCreate(i.GuildID, option.StringValue(), discordgo.ChannelTypeGuildText)
// Get Project's title from the request:
var projectTitle string
if option, ok := optionMap["project_name"]; !ok {
return
} else {
projectTitle = option.StringValue()
}
// Create channel with specified title
dchan, err := s.GuildChannelCreate(i.GuildID, projectTitle, discordgo.ChannelTypeGuildText)
if err != nil {
result = fmt.Sprintf("chan creation problem: %v\n", err)
} else {
h.defaultFollowUp(result, s, i)
return
}
// Create DB raw with new project:
p, err := h.controller.ProjectCreate(context.TODO(), domain.Project{
ChannelID: dchan.ID,
Name: projectTitle,
})
if err != nil {
result = fmt.Sprintf("unable to create project: %v\n", err)
// Revert channel creation:
_, err := s.ChannelDelete(dchan.ID)
if err != nil {
result += fmt.Sprintf("\nunable to clean channel: %v\n", err)
}
} else {
h.defaultFollowUp(result, s, i)
return
}
// Edit created channel:
edit := discordgo.ChannelEdit{
Name: p.ShortName,
ParentID: "1150719794853716028",
Name: p.Key + "-" + helpers.Cut(projectTitle),
ParentID: h.conf.IsProjectChannel,
}
dchan, err = s.ChannelEdit(dchan.ID, &edit)
if err != nil {
result = fmt.Sprintf("channel %s created, but unable to edit follow up message: %v\n", p.ShortName, err)
result = fmt.Sprintf("channel %s created, but unable to edit follow up message: %v\n", p.Key, err)
h.defaultFollowUp(result, s, i)
return
} else {
_, err = s.ChannelMessageSend(dchan.ID, "Hello!")
}
// Отправить сообщение о создании проекта:
_, err = s.ChannelMessageSend(dchan.ID, p.DiscordString())
if err != nil {
log.Printf("message send problem: %v\n", err)
result = "Project was created, but there is some problem with init channel message"
h.defaultFollowUp(result, s, i)
}
result = "Project " + p.ShortName + "Was created"
}
}
}
}
result = fmt.Sprintf("Project was created: https://discord.com/channels/%s/%s", i.GuildID, dchan.ID)
h.defaultFollowUp(result, s, i)
}