78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
package components
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.wh64.net/muffin/goMuffin/commands"
|
|
"git.wh64.net/muffin/goMuffin/databases"
|
|
"git.wh64.net/muffin/goMuffin/utils"
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
var DeleteChatComponent = &commands.Component{
|
|
Parse: func(ctx *commands.ComponentContext) bool {
|
|
i := ctx.Inter
|
|
customId := i.MessageComponentData().CustomID
|
|
|
|
if !strings.HasPrefix(customId, utils.DeleteChat) {
|
|
return false
|
|
}
|
|
|
|
userId := utils.GetChatUserId(customId)
|
|
if i.Member.User.ID != userId {
|
|
i.Reply(&discordgo.InteractionResponseData{
|
|
Flags: discordgo.MessageFlagsEphemeral | discordgo.MessageFlagsIsComponentsV2,
|
|
Components: []discordgo.MessageComponent{
|
|
utils.GetDeclineContainer(discordgo.TextDisplay{Content: "당신은 해당 권한이 없ㅇ어요."}),
|
|
},
|
|
})
|
|
return false
|
|
}
|
|
return true
|
|
},
|
|
Run: func(ctx *commands.ComponentContext) error {
|
|
i := ctx.Inter
|
|
customId := i.MessageComponentData().CustomID
|
|
|
|
switch {
|
|
case strings.HasPrefix(customId, utils.DeleteChatCancel):
|
|
return i.Update(&discordgo.InteractionResponseData{
|
|
Flags: discordgo.MessageFlagsIsComponentsV2,
|
|
Components: []discordgo.MessageComponent{
|
|
utils.GetCanceledContainer(discordgo.TextDisplay{Content: "아무 채팅방을 삭제하지 않았어요."}),
|
|
},
|
|
})
|
|
case strings.HasPrefix(customId, utils.DeleteChat):
|
|
id, itemId := utils.GetDeleteLearnedDataId(i.MessageComponentData().CustomID)
|
|
_, err := databases.Database.Chats.DeleteOne(context.TODO(), databases.Chat{Id: id})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = databases.Database.Memory.DeleteMany(context.TODO(), databases.Memory{ChatId: id})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
flags := discordgo.MessageFlagsIsComponentsV2
|
|
if itemId == 0 {
|
|
return i.EditReply(&utils.InteractionEdit{
|
|
Flags: &flags,
|
|
Components: &[]discordgo.MessageComponent{
|
|
utils.GetSuccessContainer(discordgo.TextDisplay{Content: "해당 채팅을 삭제했어요."}),
|
|
},
|
|
})
|
|
}
|
|
|
|
return i.EditReply(&utils.InteractionEdit{
|
|
Flags: &flags,
|
|
Components: &[]discordgo.MessageComponent{
|
|
utils.GetSuccessContainer(discordgo.TextDisplay{Content: fmt.Sprintf("%d번을 삭제했어요.", itemId)}),
|
|
},
|
|
})
|
|
}
|
|
return nil
|
|
},
|
|
}
|