feat: recreated delete learn data command

This commit is contained in:
Siwoo Jeon 2025-05-24 23:34:17 +09:00
parent fbef008f4f
commit d41e74bb55
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
6 changed files with 93 additions and 141 deletions

View file

@ -34,21 +34,18 @@ var DeleteLearnedDataCommand *Command = &Command{
command := strings.Join(*ctx.Args, " ")
if command == "" {
utils.NewMessageSender(ctx.Msg).
AddEmbeds(&discordgo.MessageEmbed{
Title: "❌ 오류",
Description: "올바르지 않ㅇ은 용법이에요.",
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")),
},
AddComponents(utils.GetErrorContainer(
discordgo.TextDisplay{
Content: "올바르지 않ㅇ은 용법이에요.",
},
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).
Send()
}
@ -70,18 +67,15 @@ var DeleteLearnedDataCommand *Command = &Command{
}
func deleteLearnedDataRun(m any, command, userId string) {
var description string
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})
if err != nil {
utils.NewMessageSender(m).
AddEmbeds(&discordgo.MessageEmbed{
Title: "❌ 오류",
Description: "데이터를 가져오는데 실패했어요.",
Color: utils.EmbedFail,
}).
AddComponents(utils.GetErrorContainer(discordgo.TextDisplay{Content: "데이터를 가져오는데 실패했어요."})).
SetComponentsV2(true).
SetReply(true).
Send()
return
@ -91,54 +85,46 @@ func deleteLearnedDataRun(m any, command, userId string) {
if len(data) < 1 {
utils.NewMessageSender(m).
AddEmbeds(&discordgo.MessageEmbed{
Title: "❌ 오류",
Description: "해당 하는 지식ㅇ을 찾을 수 없어요.",
Color: utils.EmbedFail,
}).
AddComponents(discordgo.TextDisplay{Content: "해당 하는 지식ㅇ을 찾을 수 없어요."}).
SetComponentsV2(true).
SetReply(true).
Send()
return
}
for i := range len(data) {
data := data[i]
options = append(options, discordgo.SelectMenuOption{
Label: fmt.Sprintf("%d번 지식", i+1),
Description: data.Result,
Value: utils.MakeDeleteLearnedData(data.Id.Hex(), i+1),
for i, data := range data {
sections = append(sections, discordgo.Section{
Accessory: discordgo.Button{
Label: "삭제",
Style: discordgo.DangerButton,
CustomID: utils.MakeDeleteLearnedData(data.Id.Hex(), i+1, userId),
},
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).
AddEmbeds(&discordgo.MessageEmbed{
Title: fmt.Sprintf("%s 삭제", command),
Description: utils.CodeBlock("md", fmt.Sprintf("# %s에 대한 대답 중 하나를 선ㅌ택하여 삭제해주세요.\n%s", command, description)),
Color: utils.EmbedDefault,
}).
AddComponents(
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.SelectMenu{
MenuType: discordgo.StringSelectMenu,
CustomID: utils.MakeDeleteLearnedDataUserId(userId),
Options: options,
Placeholder: "ㅈ지울 응답을 선택해주세요.",
},
},
},
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
CustomID: utils.MakeDeleteLearnedDataCancel(userId),
Label: "취소하기",
Style: discordgo.DangerButton,
Disabled: false,
},
},
},
).
Send()
textDisplay := discordgo.TextDisplay{Content: fmt.Sprintf("### %s 삭제", command)}
container := &discordgo.Container{Components: []discordgo.MessageComponent{textDisplay}}
for i, section := range sections {
container.Components = append(container.Components, section, discordgo.Separator{})
if (i+1)%10 == 0 {
containers = append(containers, container)
container = &discordgo.Container{Components: []discordgo.MessageComponent{textDisplay}}
continue
}
}
if len(container.Components) > 1 {
containers = append(containers, container)
}
utils.PaginationEmbedBuilder(m).
AddContainers(containers...).
Start()
}

View file

@ -197,33 +197,41 @@ func getDescriptions(data *[]databases.Learn, length int) (descriptions []string
return
}
func getSections(accessory *discordgo.Thumbnail, defaultDesc string, data *[]databases.Learn, length int) []discordgo.MessageComponent {
var sections []discordgo.MessageComponent
func getContainers(accessory *discordgo.Thumbnail, defaultDesc string, data *[]databases.Learn, length int) []*discordgo.Container {
var containers []*discordgo.Container
descriptions := getDescriptions(data, length)
if len(descriptions) <= 0 {
sections = append(sections, discordgo.Section{
Accessory: accessory,
containers = append(containers, &discordgo.Container{
Components: []discordgo.MessageComponent{
discordgo.TextDisplay{
Content: utils.MakeDesc(defaultDesc, "없음"),
discordgo.Section{
Accessory: accessory,
Components: []discordgo.MessageComponent{
discordgo.TextDisplay{
Content: utils.MakeDesc(defaultDesc, "없음"),
},
},
},
},
})
}
for _, desc := range descriptions {
sections = append(sections, discordgo.Section{
Accessory: accessory,
containers = append(containers, &discordgo.Container{
Components: []discordgo.MessageComponent{
discordgo.TextDisplay{
Content: utils.MakeDesc(defaultDesc, desc),
discordgo.Section{
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) {
@ -260,14 +268,13 @@ func learnedDataListRun(m any, globalName, avatarUrl string, filter bson.D, leng
cur.All(context.TODO(), &data)
sections := getSections(&discordgo.Thumbnail{
containers := getContainers(&discordgo.Thumbnail{
Media: discordgo.UnfurledMediaItem{
URL: avatarUrl,
},
}, fmt.Sprintf("### %s님이 알려주신 지식\n%s", globalName, utils.CodeBlock("md", fmt.Sprintf("# 총 %d개에요.\n", len(data))+"%s")), &data, length)
utils.NewPaginationEmbedBuilder(m).
SetContainer(discordgo.Container{}).
AddComponents(sections...).
utils.PaginationEmbedBuilder(m).
AddContainers(containers...).
Start()
}

View file

@ -14,47 +14,20 @@ import (
var DeleteLearnedDataComponent *commands.Component = &commands.Component{
Parse: func(ctx *commands.ComponentContext) bool {
var userId string
i := ctx.Inter
customId := i.MessageComponentData().CustomID
if i.MessageComponentData().ComponentType == discordgo.ButtonComponent {
if !strings.HasPrefix(customId, utils.DeleteLearnedDataCancel) {
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)
if !strings.HasPrefix(customId, utils.DeleteLearnedData) {
return false
}
userId := utils.GetDeleteLearnedDataUserId(customId)
if i.Member.User.ID != userId {
i.Reply(&discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Embeds: []*discordgo.MessageEmbed{
{
Title: "❌ 오류",
Description: "당신은 해당 권한이 없ㅇ어요.",
Color: utils.EmbedFail,
},
Flags: discordgo.MessageFlagsEphemeral | discordgo.MessageFlagsIsComponentsV2,
Components: []discordgo.MessageComponent{
utils.GetErrorContainer(discordgo.TextDisplay{Content: "당신은 해당 권한이 없ㅇ어요."}),
},
Components: []discordgo.MessageComponent{},
},
)
return false
@ -66,19 +39,17 @@ var DeleteLearnedDataComponent *commands.Component = &commands.Component{
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}})
flags := discordgo.MessageFlagsIsComponentsV2
i.EditReply(&utils.InteractionEdit{
Embeds: &[]*discordgo.MessageEmbed{
{
Title: "✅ 삭제 완료",
Description: fmt.Sprintf("%d번을 삭ㅈ제했어요.", itemId),
Color: utils.EmbedSuccess,
},
Flags: &flags,
Components: &[]discordgo.MessageComponent{
utils.GetSuccessContainer(discordgo.TextDisplay{Content: fmt.Sprintf("%d번을 삭ㅈ제했어요.", itemId)}),
},
Components: &[]discordgo.MessageComponent{},
})
},
}

View file

@ -7,7 +7,7 @@ import (
"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]

View file

@ -9,9 +9,7 @@ import (
)
const (
DeleteLearnedData = "#muffin/deleteLearnedData@"
DeleteLearnedDataUserId = "#muffin/deleteLearnedData@"
DeleteLearnedDataCancel = "#muffin/deleteLearnedData/cancel@"
DeleteLearnedData = "#muffin/deleteLearnedData@"
PaginationEmbedPrev = "#muffin-pages/prev$"
PaginationEmbedPages = "#muffin-pages/pages$"
@ -20,31 +18,19 @@ const (
PaginationEmbedSetPage = "#muffin-pages/modal/set$"
)
func MakeDeleteLearnedData(id string, number int) string {
return fmt.Sprintf("%s%s&No.%d", DeleteLearnedData, id, number)
}
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 MakeDeleteLearnedData(id string, number int, userId string) string {
return fmt.Sprintf("%sid=%s&no=%d&user_id=%s", DeleteLearnedData, id, number, userId)
}
func GetDeleteLearnedDataId(customId string) (id bson.ObjectID, itemId int) {
id, _ = bson.ObjectIDFromHex(strings.ReplaceAll(RegexpItemId.ReplaceAllString(customId[len(DeleteLearnedData):], ""), "&", ""))
stringItemId := strings.ReplaceAll(RegexpItemId.FindAllString(customId, 1)[0], "No.", "")
id, _ = bson.ObjectIDFromHex(strings.ReplaceAll(RegexpDLDId.FindAllString(customId, 1)[0], "id=", ""))
stringItemId := strings.ReplaceAll(RegexpDLDItemId.FindAllString(customId, 1)[0], "no=", "")
itemId, _ = strconv.Atoi(stringItemId)
return
}
func GetDeleteLearnedDataUserId(customId string) string {
if strings.HasPrefix(customId, DeleteLearnedDataCancel) {
return customId[len(DeleteLearnedDataCancel):]
} else {
return customId[len(DeleteLearnedDataUserId):]
}
return strings.ReplaceAll(RegexpDLDUserId.FindAllString(customId, 1)[0], "user_id=", "")
}
func MakePaginationEmbedPrev(id string) string {

View file

@ -5,7 +5,9 @@ import "regexp"
var (
RegexpFlexibleString = regexp.MustCompile(`[^\s"'「」«»]+|"([^"]*)"|'([^']*)'|「([^」]*)」|«([^»]*)»`)
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+>`)
RegexpLearnQueryCommand = regexp.MustCompile(`단어:([^\n대답개수:]*)`)
RegexpLearnQueryResult = regexp.MustCompile(`대답:([^\n단어개수:]*)`)