feat: add pagination embed in learnedDataList

This commit is contained in:
Siwoo Jeon 2025-05-13 22:05:43 +09:00
parent f66b1cd404
commit e8f4bc9c21
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
3 changed files with 44 additions and 22 deletions

View file

@ -51,6 +51,9 @@ var LearnedDataListCommand *Command = &Command{
func getDescriptions(data *[]databases.Learn) (descriptions []string) {
MAX_LENGTH := 100
MAX_ITEM_LENGTH := 25
tempDesc := []string{}
for _, data := range *data {
command := data.Command
@ -64,7 +67,22 @@ func getDescriptions(data *[]databases.Learn) (descriptions []string) {
result = string(runeResult[:MAX_LENGTH]) + "..."
}
descriptions = append(descriptions, fmt.Sprintf("- %s: %s", command, result))
tempDesc = append(tempDesc, fmt.Sprintf("- %s: %s\n", command, result))
}
var builder strings.Builder
for i, s := range tempDesc {
builder.WriteString(s)
if (i+1)%MAX_ITEM_LENGTH == 0 {
descriptions = append(descriptions, builder.String())
builder.Reset()
}
}
if builder.Len() > 0 {
descriptions = append(descriptions, builder.String())
}
return
}
@ -173,21 +191,10 @@ func learnedDataListRun(s *discordgo.Session, m any, args *[]string) {
embed := &discordgo.MessageEmbed{
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,
},
}
// 실험용 데이터
utils.StartPaginationEmbed(s, m, embed, []string{"asdf", "fdsa"}, 10)
// switch m := m.(type) {
// case *discordgo.MessageCreate:
// s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
// case *utils.InteractionCreate:
// m.EditReply(&discordgo.WebhookEdit{
// Embeds: &[]*discordgo.MessageEmbed{embed},
// })
// }
utils.StartPaginationEmbed(s, m, embed, getDescriptions(&data), utils.CodeBlock("md", fmt.Sprintf("# 총 %d개에요.\n", len(data))+"%s"))
}

View file

@ -7,7 +7,7 @@ import (
"git.wh64.net/muffin/goMuffin/utils"
)
const MUFFIN_VERSION = "5.1.0-gopher_dev.250513a-paginated_embed"
const MUFFIN_VERSION = "5.1.0-gopher_dev.250513b-paginated_embed"
var updatedString string = utils.Decimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0]

View file

@ -15,7 +15,7 @@ type PaginationEmbed struct {
Total int
id string
s *discordgo.Session
m any
desc string
}
var PaginationEmbeds = make(map[string]*PaginationEmbed)
@ -48,8 +48,19 @@ func makeComponents(id string, current, total int) *[]discordgo.MessageComponent
}
}
func makeDesc(desc, item string) string {
var newDesc string
if desc == "" {
newDesc = item
} else {
newDesc = fmt.Sprintf(desc, item)
}
return newDesc
}
// StartPaginationEmbed starts new PaginationEmbed struct
func StartPaginationEmbed(s *discordgo.Session, m any, e *discordgo.MessageEmbed, data []string, length int) {
func StartPaginationEmbed(s *discordgo.Session, m any, e *discordgo.MessageEmbed, data []string, defaultDesc string) {
var userId string
switch m := m.(type) {
@ -67,10 +78,10 @@ func StartPaginationEmbed(s *discordgo.Session, m any, e *discordgo.MessageEmbed
Total: len(data),
id: id,
s: s,
m: m,
desc: defaultDesc,
}
p.Embed.Description = p.Data[0]
p.Embed.Description = makeDesc(p.desc, data[0])
switch m := m.(type) {
case *discordgo.MessageCreate:
@ -102,17 +113,19 @@ func (p *PaginationEmbed) Prev(i *InteractionCreate) {
Embeds: []*discordgo.MessageEmbed{
{
Title: "❌ 오류",
Description: "해당 페이가 처음ㅇ이에요.",
Description: "해당 페이가 처음ㅇ이에요.",
Color: EmbedFail,
},
},
Flags: discordgo.MessageFlagsEphemeral,
})
return
}
p.Current -= 1
p.Embed.Description = p.Data[p.Current-1]
p.Embed.Description = makeDesc(p.desc, p.Data[p.Current-1])
i.Update(&discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{p.Embed},
Components: *makeComponents(p.id, p.Current, p.Total),
@ -125,17 +138,19 @@ func (p *PaginationEmbed) Next(i *InteractionCreate) {
Embeds: []*discordgo.MessageEmbed{
{
Title: "❌ 오류",
Description: "해당 페이가 마지막ㅇ이에요.",
Description: "해당 페이가 마지막ㅇ이에요.",
Color: EmbedFail,
},
},
Flags: discordgo.MessageFlagsEphemeral,
})
return
}
p.Current += 1
p.Embed.Description = p.Data[p.Current-1]
p.Embed.Description = makeDesc(p.desc, p.Data[p.Current-1])
i.Update(&discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{p.Embed},
Components: *makeComponents(p.id, p.Current, p.Total),