chore: edit customIds

This commit is contained in:
Siwoo Jeon 2025-05-13 19:23:06 +09:00
parent 351bf86908
commit 7cf4008834
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
4 changed files with 76 additions and 12 deletions

View file

@ -119,7 +119,7 @@ func deleteLearnedDataRun(c *Command, s *discordgo.Session, m any, args *[]strin
options = append(options, discordgo.SelectMenuOption{
Label: fmt.Sprintf("%d번 지식", i+1),
Description: data.Result,
Value: fmt.Sprintf("%s%s&No.%d", utils.DeleteLearnedData, data.Id.Hex(), i+1),
Value: utils.MakeDeleteLearnedData(data.Id.Hex(), i+1),
})
description += fmt.Sprintf("%d. %s\n", i+1, data.Result)
}
@ -135,7 +135,7 @@ func deleteLearnedDataRun(c *Command, s *discordgo.Session, m any, args *[]strin
Components: []discordgo.MessageComponent{
discordgo.SelectMenu{
MenuType: discordgo.StringSelectMenu,
CustomID: utils.DeleteLearnedDataUserId + userId,
CustomID: utils.MakeDeleteLearnedDataUserId(userId),
Options: options,
Placeholder: "ㅈ지울 응답을 선택해주세요.",
},
@ -144,7 +144,7 @@ func deleteLearnedDataRun(c *Command, s *discordgo.Session, m any, args *[]strin
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
CustomID: utils.DeleteLearnedDataCancel + userId,
CustomID: utils.MakeDeleteLearnedDataCancel(userId),
Label: "취소하기",
Style: discordgo.DangerButton,
Disabled: false,

View file

@ -171,9 +171,9 @@ func learnedDataListRun(s *discordgo.Session, m any, args *[]string) {
cur.All(context.TODO(), &data)
embed := &discordgo.MessageEmbed{
Title: fmt.Sprintf("%s님이 알려주신 지식", globalName),
Description: utils.CodeBlock("md", fmt.Sprintf("# 총 %d개에요.\n%s", len(data), strings.Join(getDescriptions(&data), "\n"))),
Color: utils.EmbedDefault,
Title: fmt.Sprintf("%s님이 알려주신 지식", globalName),
Color: utils.EmbedDefault,
// Description: utils.CodeBlock("md", fmt.Sprintf("# 총 %d개에요.\n%s", len(data), strings.Join(getDescriptions(&data), "\n"))),
Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: avatarUrl,
},

View file

@ -23,7 +23,7 @@ var DeleteLearnedDataComponent *commands.Component = &commands.Component{
return false
}
userId = customId[len(utils.DeleteLearnedDataCancel):]
userId = utils.GetDeleteLearnedDataUserId(customId)
if i.Member.User.ID == userId {
i.Update(&discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
@ -41,7 +41,7 @@ var DeleteLearnedDataComponent *commands.Component = &commands.Component{
return false
}
userId = customId[len(utils.DeleteLearnedDataUserId):]
userId = utils.GetDeleteLearnedDataUserId(customId)
}
if i.Member.User.ID != userId {
@ -66,8 +66,7 @@ var DeleteLearnedDataComponent *commands.Component = &commands.Component{
i.DeferUpdate()
id, _ := bson.ObjectIDFromHex(strings.ReplaceAll(utils.ItemIdRegexp.ReplaceAllString(i.MessageComponentData().Values[0][len(utils.DeleteLearnedData):], ""), "&", ""))
itemId := strings.ReplaceAll(utils.ItemIdRegexp.FindAllString(i.MessageComponentData().Values[0], 1)[0], "No.", "")
id, itemId := utils.GetDeleteLearnedDataId(i.MessageComponentData().Values[0])
databases.Database.Learns.DeleteOne(context.TODO(), bson.D{{Key: "_id", Value: id}})
@ -75,7 +74,7 @@ var DeleteLearnedDataComponent *commands.Component = &commands.Component{
Embeds: &[]*discordgo.MessageEmbed{
{
Title: "✅ 삭제 완료",
Description: fmt.Sprintf("%s번을 삭ㅈ제했어요.", itemId),
Description: fmt.Sprintf("%d번을 삭ㅈ제했어요.", itemId),
Color: utils.EmbedSuccess,
},
},

View file

@ -1,7 +1,72 @@
package utils
import (
"fmt"
"strconv"
"strings"
"go.mongodb.org/mongo-driver/v2/bson"
)
const (
DeleteLearnedData = "#muffin/deleteLearnedData$"
DeleteLearnedData = "#muffin/deleteLearnedData@"
DeleteLearnedDataUserId = "#muffin/deleteLearnedData@"
DeleteLearnedDataCancel = "#muffin/deleteLearnedData/cancel@"
PaginationEmbedPrev = "#muffin-pages/prev$"
PaginationEmbedPages = "#muffin-pages/pages$"
PaginationEmbedNext = "#muffin-pages/next$"
)
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 GetDeleteLearnedDataId(customId string) (id bson.ObjectID, itemId int) {
id, _ = bson.ObjectIDFromHex(strings.ReplaceAll(ItemIdRegexp.ReplaceAllString(customId[len(DeleteLearnedData):], ""), "&", ""))
stringItemId := strings.ReplaceAll(ItemIdRegexp.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):]
}
}
func MakePaginationEmbedPrev(id string) string {
return fmt.Sprintf("%s%s", PaginationEmbedPrev, id)
}
func MakePaginationEmbedPages(id string, total, current int) string {
return fmt.Sprintf("%s%s/%d/%d", PaginationEmbedPages, id, total, current)
}
func MakePaginationEmbedNext(id string) string {
return fmt.Sprintf("%s%s", PaginationEmbedNext, id)
}
func GetPaginationEmbedId(customId string) string {
if strings.HasPrefix(customId, PaginationEmbedPrev) {
return customId[len(PaginationEmbedPrev):]
} else if strings.HasPrefix(customId, PaginationEmbedPages) {
return customId[len(PaginationEmbedPages):]
} else {
return customId[len(PaginationEmbedNext):]
}
}
func GetPaginationEmbedUserId(id string) string {
return PaginationEmbedId.FindAllStringSubmatch(id, 1)[0][1]
}