feat: recreated delete learn data command
This commit is contained in:
parent
fbef008f4f
commit
d41e74bb55
6 changed files with 93 additions and 141 deletions
|
@ -34,21 +34,18 @@ var DeleteLearnedDataCommand *Command = &Command{
|
||||||
command := strings.Join(*ctx.Args, " ")
|
command := strings.Join(*ctx.Args, " ")
|
||||||
if command == "" {
|
if command == "" {
|
||||||
utils.NewMessageSender(ctx.Msg).
|
utils.NewMessageSender(ctx.Msg).
|
||||||
AddEmbeds(&discordgo.MessageEmbed{
|
AddComponents(utils.GetErrorContainer(
|
||||||
Title: "❌ 오류",
|
discordgo.TextDisplay{
|
||||||
Description: "올바르지 않ㅇ은 용법이에요.",
|
Content: "올바르지 않ㅇ은 용법이에요.",
|
||||||
Fields: []*discordgo.MessageEmbedField{
|
|
||||||
{
|
|
||||||
Name: "사용법",
|
|
||||||
Value: utils.InlineCode(ctx.Command.DetailedDescription.Usage),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "예시",
|
|
||||||
Value: utils.CodeBlock("md", strings.Join(utils.AddPrefix("- ", ctx.Command.DetailedDescription.Examples), "\n")),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Color: utils.EmbedFail,
|
discordgo.TextDisplay{
|
||||||
}).
|
Content: fmt.Sprintf("- **사용법**\n> %s", ctx.Command.DetailedDescription.Usage),
|
||||||
|
},
|
||||||
|
discordgo.TextDisplay{
|
||||||
|
Content: fmt.Sprintf("- **예시**\n%s", strings.Join(utils.AddPrefix("> ", ctx.Command.DetailedDescription.Examples), "\n")),
|
||||||
|
},
|
||||||
|
)).
|
||||||
|
SetComponentsV2(true).
|
||||||
SetReply(true).
|
SetReply(true).
|
||||||
Send()
|
Send()
|
||||||
}
|
}
|
||||||
|
@ -70,18 +67,15 @@ var DeleteLearnedDataCommand *Command = &Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteLearnedDataRun(m any, command, userId string) {
|
func deleteLearnedDataRun(m any, command, userId string) {
|
||||||
var description string
|
|
||||||
var data []databases.Learn
|
var data []databases.Learn
|
||||||
var options []discordgo.SelectMenuOption
|
var sections []discordgo.Section
|
||||||
|
var containers []*discordgo.Container
|
||||||
|
|
||||||
cur, err := databases.Database.Learns.Find(context.TODO(), bson.M{"user_id": userId, "command": command})
|
cur, err := databases.Database.Learns.Find(context.TODO(), bson.M{"user_id": userId, "command": command})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.NewMessageSender(m).
|
utils.NewMessageSender(m).
|
||||||
AddEmbeds(&discordgo.MessageEmbed{
|
AddComponents(utils.GetErrorContainer(discordgo.TextDisplay{Content: "데이터를 가져오는데 실패했어요."})).
|
||||||
Title: "❌ 오류",
|
SetComponentsV2(true).
|
||||||
Description: "데이터를 가져오는데 실패했어요.",
|
|
||||||
Color: utils.EmbedFail,
|
|
||||||
}).
|
|
||||||
SetReply(true).
|
SetReply(true).
|
||||||
Send()
|
Send()
|
||||||
return
|
return
|
||||||
|
@ -91,54 +85,46 @@ func deleteLearnedDataRun(m any, command, userId string) {
|
||||||
|
|
||||||
if len(data) < 1 {
|
if len(data) < 1 {
|
||||||
utils.NewMessageSender(m).
|
utils.NewMessageSender(m).
|
||||||
AddEmbeds(&discordgo.MessageEmbed{
|
AddComponents(discordgo.TextDisplay{Content: "해당 하는 지식ㅇ을 찾을 수 없어요."}).
|
||||||
Title: "❌ 오류",
|
SetComponentsV2(true).
|
||||||
Description: "해당 하는 지식ㅇ을 찾을 수 없어요.",
|
|
||||||
Color: utils.EmbedFail,
|
|
||||||
}).
|
|
||||||
SetReply(true).
|
SetReply(true).
|
||||||
Send()
|
Send()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range len(data) {
|
for i, data := range data {
|
||||||
data := data[i]
|
sections = append(sections, discordgo.Section{
|
||||||
|
Accessory: discordgo.Button{
|
||||||
options = append(options, discordgo.SelectMenuOption{
|
Label: "삭제",
|
||||||
Label: fmt.Sprintf("%d번 지식", i+1),
|
Style: discordgo.DangerButton,
|
||||||
Description: data.Result,
|
CustomID: utils.MakeDeleteLearnedData(data.Id.Hex(), i+1, userId),
|
||||||
Value: utils.MakeDeleteLearnedData(data.Id.Hex(), i+1),
|
},
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.TextDisplay{
|
||||||
|
Content: fmt.Sprintf("%d. %s\n", i+1, data.Result),
|
||||||
|
},
|
||||||
|
},
|
||||||
})
|
})
|
||||||
description += fmt.Sprintf("%d. %s\n", i+1, data.Result)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.NewMessageSender(m).
|
textDisplay := discordgo.TextDisplay{Content: fmt.Sprintf("### %s 삭제", command)}
|
||||||
AddEmbeds(&discordgo.MessageEmbed{
|
container := &discordgo.Container{Components: []discordgo.MessageComponent{textDisplay}}
|
||||||
Title: fmt.Sprintf("%s 삭제", command),
|
|
||||||
Description: utils.CodeBlock("md", fmt.Sprintf("# %s에 대한 대답 중 하나를 선ㅌ택하여 삭제해주세요.\n%s", command, description)),
|
for i, section := range sections {
|
||||||
Color: utils.EmbedDefault,
|
container.Components = append(container.Components, section, discordgo.Separator{})
|
||||||
}).
|
|
||||||
AddComponents(
|
if (i+1)%10 == 0 {
|
||||||
discordgo.ActionsRow{
|
containers = append(containers, container)
|
||||||
Components: []discordgo.MessageComponent{
|
container = &discordgo.Container{Components: []discordgo.MessageComponent{textDisplay}}
|
||||||
discordgo.SelectMenu{
|
continue
|
||||||
MenuType: discordgo.StringSelectMenu,
|
}
|
||||||
CustomID: utils.MakeDeleteLearnedDataUserId(userId),
|
}
|
||||||
Options: options,
|
|
||||||
Placeholder: "ㅈ지울 응답을 선택해주세요.",
|
if len(container.Components) > 1 {
|
||||||
},
|
containers = append(containers, container)
|
||||||
},
|
}
|
||||||
},
|
|
||||||
discordgo.ActionsRow{
|
utils.PaginationEmbedBuilder(m).
|
||||||
Components: []discordgo.MessageComponent{
|
AddContainers(containers...).
|
||||||
discordgo.Button{
|
Start()
|
||||||
CustomID: utils.MakeDeleteLearnedDataCancel(userId),
|
|
||||||
Label: "취소하기",
|
|
||||||
Style: discordgo.DangerButton,
|
|
||||||
Disabled: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
).
|
|
||||||
Send()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -197,33 +197,41 @@ func getDescriptions(data *[]databases.Learn, length int) (descriptions []string
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSections(accessory *discordgo.Thumbnail, defaultDesc string, data *[]databases.Learn, length int) []discordgo.MessageComponent {
|
func getContainers(accessory *discordgo.Thumbnail, defaultDesc string, data *[]databases.Learn, length int) []*discordgo.Container {
|
||||||
var sections []discordgo.MessageComponent
|
var containers []*discordgo.Container
|
||||||
|
|
||||||
descriptions := getDescriptions(data, length)
|
descriptions := getDescriptions(data, length)
|
||||||
|
|
||||||
if len(descriptions) <= 0 {
|
if len(descriptions) <= 0 {
|
||||||
sections = append(sections, discordgo.Section{
|
containers = append(containers, &discordgo.Container{
|
||||||
Accessory: accessory,
|
|
||||||
Components: []discordgo.MessageComponent{
|
Components: []discordgo.MessageComponent{
|
||||||
discordgo.TextDisplay{
|
discordgo.Section{
|
||||||
Content: utils.MakeDesc(defaultDesc, "없음"),
|
Accessory: accessory,
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.TextDisplay{
|
||||||
|
Content: utils.MakeDesc(defaultDesc, "없음"),
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, desc := range descriptions {
|
for _, desc := range descriptions {
|
||||||
sections = append(sections, discordgo.Section{
|
containers = append(containers, &discordgo.Container{
|
||||||
Accessory: accessory,
|
|
||||||
Components: []discordgo.MessageComponent{
|
Components: []discordgo.MessageComponent{
|
||||||
discordgo.TextDisplay{
|
discordgo.Section{
|
||||||
Content: utils.MakeDesc(defaultDesc, desc),
|
Accessory: accessory,
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.TextDisplay{
|
||||||
|
Content: utils.MakeDesc(defaultDesc, desc),
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return sections
|
return containers
|
||||||
}
|
}
|
||||||
|
|
||||||
func learnedDataListRun(m any, globalName, avatarUrl string, filter bson.D, length int) {
|
func learnedDataListRun(m any, globalName, avatarUrl string, filter bson.D, length int) {
|
||||||
|
@ -260,14 +268,13 @@ func learnedDataListRun(m any, globalName, avatarUrl string, filter bson.D, leng
|
||||||
|
|
||||||
cur.All(context.TODO(), &data)
|
cur.All(context.TODO(), &data)
|
||||||
|
|
||||||
sections := getSections(&discordgo.Thumbnail{
|
containers := getContainers(&discordgo.Thumbnail{
|
||||||
Media: discordgo.UnfurledMediaItem{
|
Media: discordgo.UnfurledMediaItem{
|
||||||
URL: avatarUrl,
|
URL: avatarUrl,
|
||||||
},
|
},
|
||||||
}, fmt.Sprintf("### %s님이 알려주신 지식\n%s", globalName, utils.CodeBlock("md", fmt.Sprintf("# 총 %d개에요.\n", len(data))+"%s")), &data, length)
|
}, fmt.Sprintf("### %s님이 알려주신 지식\n%s", globalName, utils.CodeBlock("md", fmt.Sprintf("# 총 %d개에요.\n", len(data))+"%s")), &data, length)
|
||||||
|
|
||||||
utils.NewPaginationEmbedBuilder(m).
|
utils.PaginationEmbedBuilder(m).
|
||||||
SetContainer(discordgo.Container{}).
|
AddContainers(containers...).
|
||||||
AddComponents(sections...).
|
|
||||||
Start()
|
Start()
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,47 +14,20 @@ import (
|
||||||
|
|
||||||
var DeleteLearnedDataComponent *commands.Component = &commands.Component{
|
var DeleteLearnedDataComponent *commands.Component = &commands.Component{
|
||||||
Parse: func(ctx *commands.ComponentContext) bool {
|
Parse: func(ctx *commands.ComponentContext) bool {
|
||||||
var userId string
|
|
||||||
i := ctx.Inter
|
i := ctx.Inter
|
||||||
customId := i.MessageComponentData().CustomID
|
customId := i.MessageComponentData().CustomID
|
||||||
|
|
||||||
if i.MessageComponentData().ComponentType == discordgo.ButtonComponent {
|
if !strings.HasPrefix(customId, utils.DeleteLearnedData) {
|
||||||
if !strings.HasPrefix(customId, utils.DeleteLearnedDataCancel) {
|
return false
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
userId = utils.GetDeleteLearnedDataUserId(customId)
|
|
||||||
if i.Member.User.ID == userId {
|
|
||||||
i.Update(&discordgo.InteractionResponseData{
|
|
||||||
Embeds: []*discordgo.MessageEmbed{
|
|
||||||
{
|
|
||||||
Title: "❌ 취소",
|
|
||||||
Description: "지식 삭제 작업ㅇ을 취소했어요.",
|
|
||||||
Color: utils.EmbedFail,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if !strings.HasPrefix(customId, utils.DeleteLearnedDataUserId) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
userId = utils.GetDeleteLearnedDataUserId(customId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
userId := utils.GetDeleteLearnedDataUserId(customId)
|
||||||
if i.Member.User.ID != userId {
|
if i.Member.User.ID != userId {
|
||||||
i.Reply(&discordgo.InteractionResponseData{
|
i.Reply(&discordgo.InteractionResponseData{
|
||||||
Flags: discordgo.MessageFlagsEphemeral,
|
Flags: discordgo.MessageFlagsEphemeral | discordgo.MessageFlagsIsComponentsV2,
|
||||||
Embeds: []*discordgo.MessageEmbed{
|
Components: []discordgo.MessageComponent{
|
||||||
{
|
utils.GetErrorContainer(discordgo.TextDisplay{Content: "당신은 해당 권한이 없ㅇ어요."}),
|
||||||
Title: "❌ 오류",
|
|
||||||
Description: "당신은 해당 권한이 없ㅇ어요.",
|
|
||||||
Color: utils.EmbedFail,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Components: []discordgo.MessageComponent{},
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
return false
|
return false
|
||||||
|
@ -66,19 +39,17 @@ var DeleteLearnedDataComponent *commands.Component = &commands.Component{
|
||||||
|
|
||||||
i.DeferUpdate()
|
i.DeferUpdate()
|
||||||
|
|
||||||
id, itemId := utils.GetDeleteLearnedDataId(i.MessageComponentData().Values[0])
|
id, itemId := utils.GetDeleteLearnedDataId(i.MessageComponentData().CustomID)
|
||||||
|
fmt.Println(id, itemId)
|
||||||
|
|
||||||
databases.Database.Learns.DeleteOne(context.TODO(), bson.D{{Key: "_id", Value: id}})
|
databases.Database.Learns.DeleteOne(context.TODO(), bson.D{{Key: "_id", Value: id}})
|
||||||
|
|
||||||
|
flags := discordgo.MessageFlagsIsComponentsV2
|
||||||
i.EditReply(&utils.InteractionEdit{
|
i.EditReply(&utils.InteractionEdit{
|
||||||
Embeds: &[]*discordgo.MessageEmbed{
|
Flags: &flags,
|
||||||
{
|
Components: &[]discordgo.MessageComponent{
|
||||||
Title: "✅ 삭제 완료",
|
utils.GetSuccessContainer(discordgo.TextDisplay{Content: fmt.Sprintf("%d번을 삭ㅈ제했어요.", itemId)}),
|
||||||
Description: fmt.Sprintf("%d번을 삭ㅈ제했어요.", itemId),
|
|
||||||
Color: utils.EmbedSuccess,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Components: &[]discordgo.MessageComponent{},
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"git.wh64.net/muffin/goMuffin/utils"
|
"git.wh64.net/muffin/goMuffin/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MUFFIN_VERSION = "0.0.0-souffle_canary.250524a-componentsv2"
|
const MUFFIN_VERSION = "0.0.0-souffle_canary.250524b-componentsv2"
|
||||||
|
|
||||||
var updatedString string = utils.RegexpDecimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0]
|
var updatedString string = utils.RegexpDecimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0]
|
||||||
|
|
||||||
|
|
|
@ -9,9 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DeleteLearnedData = "#muffin/deleteLearnedData@"
|
DeleteLearnedData = "#muffin/deleteLearnedData@"
|
||||||
DeleteLearnedDataUserId = "#muffin/deleteLearnedData@"
|
|
||||||
DeleteLearnedDataCancel = "#muffin/deleteLearnedData/cancel@"
|
|
||||||
|
|
||||||
PaginationEmbedPrev = "#muffin-pages/prev$"
|
PaginationEmbedPrev = "#muffin-pages/prev$"
|
||||||
PaginationEmbedPages = "#muffin-pages/pages$"
|
PaginationEmbedPages = "#muffin-pages/pages$"
|
||||||
|
@ -20,31 +18,19 @@ const (
|
||||||
PaginationEmbedSetPage = "#muffin-pages/modal/set$"
|
PaginationEmbedSetPage = "#muffin-pages/modal/set$"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MakeDeleteLearnedData(id string, number int) string {
|
func MakeDeleteLearnedData(id string, number int, userId string) string {
|
||||||
return fmt.Sprintf("%s%s&No.%d", DeleteLearnedData, id, number)
|
return fmt.Sprintf("%sid=%s&no=%d&user_id=%s", DeleteLearnedData, id, number, userId)
|
||||||
}
|
|
||||||
|
|
||||||
func MakeDeleteLearnedDataUserId(userId string) string {
|
|
||||||
return fmt.Sprintf("%s%s", DeleteLearnedDataUserId, userId)
|
|
||||||
}
|
|
||||||
|
|
||||||
func MakeDeleteLearnedDataCancel(id string) string {
|
|
||||||
return fmt.Sprintf("%s%s", DeleteLearnedDataCancel, id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDeleteLearnedDataId(customId string) (id bson.ObjectID, itemId int) {
|
func GetDeleteLearnedDataId(customId string) (id bson.ObjectID, itemId int) {
|
||||||
id, _ = bson.ObjectIDFromHex(strings.ReplaceAll(RegexpItemId.ReplaceAllString(customId[len(DeleteLearnedData):], ""), "&", ""))
|
id, _ = bson.ObjectIDFromHex(strings.ReplaceAll(RegexpDLDId.FindAllString(customId, 1)[0], "id=", ""))
|
||||||
stringItemId := strings.ReplaceAll(RegexpItemId.FindAllString(customId, 1)[0], "No.", "")
|
stringItemId := strings.ReplaceAll(RegexpDLDItemId.FindAllString(customId, 1)[0], "no=", "")
|
||||||
itemId, _ = strconv.Atoi(stringItemId)
|
itemId, _ = strconv.Atoi(stringItemId)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDeleteLearnedDataUserId(customId string) string {
|
func GetDeleteLearnedDataUserId(customId string) string {
|
||||||
if strings.HasPrefix(customId, DeleteLearnedDataCancel) {
|
return strings.ReplaceAll(RegexpDLDUserId.FindAllString(customId, 1)[0], "user_id=", "")
|
||||||
return customId[len(DeleteLearnedDataCancel):]
|
|
||||||
} else {
|
|
||||||
return customId[len(DeleteLearnedDataUserId):]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakePaginationEmbedPrev(id string) string {
|
func MakePaginationEmbedPrev(id string) string {
|
||||||
|
|
|
@ -5,7 +5,9 @@ import "regexp"
|
||||||
var (
|
var (
|
||||||
RegexpFlexibleString = regexp.MustCompile(`[^\s"'「」«»]+|"([^"]*)"|'([^']*)'|「([^」]*)」|«([^»]*)»`)
|
RegexpFlexibleString = regexp.MustCompile(`[^\s"'「」«»]+|"([^"]*)"|'([^']*)'|「([^」]*)」|«([^»]*)»`)
|
||||||
RegexpDecimals = regexp.MustCompile(`\d+`)
|
RegexpDecimals = regexp.MustCompile(`\d+`)
|
||||||
RegexpItemId = regexp.MustCompile(`No.\d+`)
|
RegexpDLDItemId = regexp.MustCompile(`no=\d+`)
|
||||||
|
RegexpDLDUserId = regexp.MustCompile(`user_id=\d+`)
|
||||||
|
RegexpDLDId = regexp.MustCompile(`id=[^&]*`)
|
||||||
RegexpEmoji = regexp.MustCompile(`<a?:\w+:\d+>`)
|
RegexpEmoji = regexp.MustCompile(`<a?:\w+:\d+>`)
|
||||||
RegexpLearnQueryCommand = regexp.MustCompile(`단어:([^\n대답개수:]*)`)
|
RegexpLearnQueryCommand = regexp.MustCompile(`단어:([^\n대답개수:]*)`)
|
||||||
RegexpLearnQueryResult = regexp.MustCompile(`대답:([^\n단어개수:]*)`)
|
RegexpLearnQueryResult = regexp.MustCompile(`대답:([^\n단어개수:]*)`)
|
||||||
|
|
Loading…
Reference in a new issue