139 lines
3.3 KiB
Go
139 lines
3.3 KiB
Go
package handler
|
||
|
||
import (
|
||
"log"
|
||
|
||
"github.com/bwmarrin/discordgo"
|
||
)
|
||
|
||
func (h *router) setFlag(s *discordgo.Session, i *discordgo.InteractionCreate, tag *discordgo.ForumTag) error {
|
||
|
||
th, err := s.Channel(i.ChannelID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
forum, err := s.Channel(th.ParentID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
// Проверка на существование тега в списке тегов:
|
||
if len(forum.AvailableTags) != 0 {
|
||
for _, some := range forum.AvailableTags {
|
||
if some.Name == tag.Name {
|
||
log.Print(tag.Name)
|
||
thE, err := s.ChannelEditComplex(i.ChannelID, &discordgo.ChannelEdit{
|
||
AppliedTags: &[]string{some.ID},
|
||
})
|
||
_, _ = thE, err
|
||
}
|
||
}
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
func (h *router) CreateExternalTask() CommandRoute {
|
||
return CommandRoute{
|
||
|
||
Command: discordgo.ApplicationCommand{
|
||
Name: "test",
|
||
Description: "Buttons test",
|
||
},
|
||
|
||
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)
|
||
|
||
// go func() {
|
||
// h.controller.InitTask("something like a default description")
|
||
// }()
|
||
},
|
||
}
|
||
}
|
||
|
||
func (h *router) StartTask() ComponentRoute {
|
||
return ComponentRoute{
|
||
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||
|
||
user := i.Member.User.Mention()
|
||
|
||
convertable, err := h.controller.UpdateTask(i.Message.ID, 0, user)
|
||
if err != nil {
|
||
|
||
}
|
||
|
||
newContent := convertable.ExtractDomain().StartedMessage()
|
||
if err != nil {
|
||
newContent += "\n `In progress` action produced with error:" + err.Error()
|
||
}
|
||
|
||
newMsg := discordgo.MessageEdit{
|
||
Content: &newContent,
|
||
Channel: i.ChannelID,
|
||
ID: i.Message.ID,
|
||
Components: []discordgo.MessageComponent{
|
||
discordgo.ActionsRow{
|
||
Components: []discordgo.MessageComponent{
|
||
discordgo.Button{
|
||
Label: "Close",
|
||
Style: discordgo.DangerButton,
|
||
Disabled: false,
|
||
CustomID: "task_close",
|
||
},
|
||
},
|
||
},
|
||
},
|
||
}
|
||
|
||
h.setFlag(s, i, &h.Tags[0])
|
||
|
||
_, err = s.ChannelMessageEditComplex(&newMsg)
|
||
if err != nil {
|
||
log.Println("edition NOT complete, ", err)
|
||
}
|
||
},
|
||
}
|
||
}
|
||
|
||
func (h *router) CloseTask() ComponentRoute {
|
||
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])
|
||
|
||
},
|
||
}
|
||
}
|