finished farm tasks; added new post listener;
This commit is contained in:
parent
c0e7a3918a
commit
972e6564ea
|
|
@ -1,12 +1,15 @@
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"ticket-pimp/internal/domain"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *router) setFlag(s *discordgo.Session, i *discordgo.InteractionCreate, tag *discordgo.ForumTag) error {
|
func (c *client) setFlag(s *discordgo.Session, i *discordgo.InteractionCreate, tag *discordgo.ForumTag) error {
|
||||||
|
|
||||||
th, err := s.Channel(i.ChannelID)
|
th, err := s.Channel(i.ChannelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -22,11 +25,12 @@ func (h *router) setFlag(s *discordgo.Session, i *discordgo.InteractionCreate, t
|
||||||
if len(forum.AvailableTags) != 0 {
|
if len(forum.AvailableTags) != 0 {
|
||||||
for _, some := range forum.AvailableTags {
|
for _, some := range forum.AvailableTags {
|
||||||
if some.Name == tag.Name {
|
if some.Name == tag.Name {
|
||||||
log.Print(tag.Name)
|
_, err := s.ChannelEditComplex(i.ChannelID, &discordgo.ChannelEdit{
|
||||||
thE, err := s.ChannelEditComplex(i.ChannelID, &discordgo.ChannelEdit{
|
|
||||||
AppliedTags: &[]string{some.ID},
|
AppliedTags: &[]string{some.ID},
|
||||||
})
|
})
|
||||||
_, _ = thE, err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -34,52 +38,133 @@ func (h *router) setFlag(s *discordgo.Session, i *discordgo.InteractionCreate, t
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *router) CreateExternalTask() CommandRoute {
|
func (c *client) ListenPosts(s *discordgo.Session, th *discordgo.ThreadCreate) {
|
||||||
return CommandRoute{
|
|
||||||
|
|
||||||
Command: discordgo.ApplicationCommand{
|
// Check if thread starter is not a bot, and thread started at the tasks channel;
|
||||||
Name: "test",
|
if th.ParentID != c.conf.IsTaskForum || th.OwnerID == s.State.User.ID {
|
||||||
Description: "Buttons test",
|
return
|
||||||
},
|
|
||||||
|
|
||||||
Handler: func(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)
|
msgs, _ := s.ChannelMessages(th.ID, 1, "", "", "")
|
||||||
|
|
||||||
// go func() {
|
msg, _ := s.ChannelMessage(th.ID, msgs[0].ID)
|
||||||
// h.controller.InitTask("something like a default description")
|
|
||||||
// }()
|
if msg.Author.ID == s.State.User.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content := th.Name
|
||||||
|
content += "\n" + msg.Content
|
||||||
|
|
||||||
|
user, _ := s.GuildMember(th.GuildID, msg.Author.ID)
|
||||||
|
|
||||||
|
t, err := c.controller.WriteTaskToDB(&domain.Task{
|
||||||
|
Description: content,
|
||||||
|
Creator: user.User.Mention(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
s.ChannelMessageSend(th.ID, fmt.Sprintf("unable to write task to db, %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// [x] -- Отредактировать Thread name как для задачи
|
||||||
|
_, err = s.ChannelEditComplex(th.ID, &discordgo.ChannelEdit{
|
||||||
|
Name: fmt.Sprintf("Task ID: %d, by %s", t.ID, t.Creator),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("th edition is not complete: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fix the original task message:
|
||||||
|
taskMessage, err := s.ChannelMessageSendComplex(th.ID, &discordgo.MessageSend{
|
||||||
|
Content: content,
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.Button{
|
||||||
|
Label: "Start",
|
||||||
|
Style: discordgo.SuccessButton,
|
||||||
|
Disabled: false,
|
||||||
|
CustomID: "task_start",
|
||||||
},
|
},
|
||||||
|
discordgo.Button{
|
||||||
|
Label: "Close",
|
||||||
|
Style: discordgo.DangerButton,
|
||||||
|
Disabled: true,
|
||||||
|
CustomID: "task_close",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("th start message edition is not complete: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = c.controller.UpdateTasksMessageID(context.TODO(), taskMessage.ID, t.ID)
|
||||||
|
if err != nil {
|
||||||
|
s.ChannelMessageSend(th.ID, fmt.Sprintf("unable to update task at the db, %v", err))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *router) StartTask() ComponentRoute {
|
func (c *client) HandleTaskButtons() Component {
|
||||||
return ComponentRoute{
|
return Component{
|
||||||
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
Handler: c.handleTaskButton,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *client) handleTaskButton(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()
|
user := i.Member.User.Mention()
|
||||||
|
|
||||||
convertable, err := h.controller.UpdateTask(i.Message.ID, 0, user)
|
var (
|
||||||
if err != nil {
|
opt int = -1
|
||||||
|
doneButtonIsDisabled bool = false
|
||||||
|
state domain.TaskState = domain.NewTaskState()
|
||||||
|
)
|
||||||
|
|
||||||
|
// Check what flow was touched: -------------------------------------------------------------------------
|
||||||
|
switch i.Interaction.MessageComponentData().CustomID {
|
||||||
|
case "task_start":
|
||||||
|
opt = 0
|
||||||
|
doneButtonIsDisabled = false
|
||||||
|
state = domain.InrpogressTaskState()
|
||||||
|
case "task_close":
|
||||||
|
opt = 1
|
||||||
|
doneButtonIsDisabled = true
|
||||||
|
state = domain.DoneTaskState()
|
||||||
}
|
}
|
||||||
|
|
||||||
newContent := convertable.ExtractDomain().StartedMessage()
|
// Send the task update to db --------------------------------------------------------------------------
|
||||||
|
convertable, err := c.controller.UpdateTask(i.Message.ID, opt, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
newContent += "\n `In progress` action produced with error:" + err.Error()
|
s.ChannelMessageSend(i.ChannelID, fmt.Sprintf("Unable to update task at the db w/ error: %v", err))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newMsg := discordgo.MessageEdit{
|
// Map DB's response to domain.Task: -------------------------------------------------------------------
|
||||||
Content: &newContent,
|
newContent := convertable.
|
||||||
|
ExtractDomain().
|
||||||
|
DiscordMessage(state)
|
||||||
|
|
||||||
|
// Send a message to the thread about the task was started: ---------------------------------------------
|
||||||
|
_, err = s.ChannelMessageSendComplex(i.ChannelID, &discordgo.MessageSend{
|
||||||
|
Content: newContent,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error while sending start task message: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fix the original task message: ----------------------------------------------------------------------
|
||||||
|
_, err = s.ChannelMessageEditComplex(&discordgo.MessageEdit{
|
||||||
Channel: i.ChannelID,
|
Channel: i.ChannelID,
|
||||||
ID: i.Message.ID,
|
ID: i.Message.ID,
|
||||||
Components: []discordgo.MessageComponent{
|
Components: []discordgo.MessageComponent{
|
||||||
|
|
@ -88,51 +173,20 @@ func (h *router) StartTask() ComponentRoute {
|
||||||
discordgo.Button{
|
discordgo.Button{
|
||||||
Label: "Close",
|
Label: "Close",
|
||||||
Style: discordgo.DangerButton,
|
Style: discordgo.DangerButton,
|
||||||
Disabled: false,
|
Disabled: doneButtonIsDisabled,
|
||||||
CustomID: "task_close",
|
CustomID: "task_close",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
|
|
||||||
h.setFlag(s, i, &h.Tags[0])
|
|
||||||
|
|
||||||
_, err = s.ChannelMessageEditComplex(&newMsg)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("edition NOT complete, ", err)
|
log.Printf("th start message edition is not complete: %v", err)
|
||||||
}
|
}
|
||||||
},
|
|
||||||
}
|
// Устанавливаем тэги статуса на тред ---------------------------------------------------------------------
|
||||||
}
|
err = c.setFlag(s, i, &c.Tags[opt])
|
||||||
|
if err != nil {
|
||||||
func (h *router) CloseTask() ComponentRoute {
|
log.Printf("error while `start` tag setting: %v", err)
|
||||||
return ComponentRoute{
|
|
||||||
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
||||||
|
|
||||||
user := i.Member.User.Mention()
|
|
||||||
convertable, err := h.controller.UpdateTask(i.Message.ID, 1, user)
|
|
||||||
|
|
||||||
newContent := convertable.ExtractDomain().ClosedMessage()
|
|
||||||
if err != nil {
|
|
||||||
newContent += "\n `Close` action produced with error:" + err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
newMsg := discordgo.MessageEdit{
|
|
||||||
Content: &newContent,
|
|
||||||
Channel: i.ChannelID,
|
|
||||||
ID: i.Message.ID,
|
|
||||||
Components: []discordgo.MessageComponent{},
|
|
||||||
}
|
|
||||||
|
|
||||||
msgE, err := s.ChannelMessageEditComplex(&newMsg)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("edition NOT complete, ", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = msgE
|
|
||||||
h.setFlag(s, i, &h.Tags[1])
|
|
||||||
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue