feat: add pagination embed in learnedDataList
This commit is contained in:
parent
f66b1cd404
commit
e8f4bc9c21
3 changed files with 44 additions and 22 deletions
|
@ -51,6 +51,9 @@ var LearnedDataListCommand *Command = &Command{
|
||||||
|
|
||||||
func getDescriptions(data *[]databases.Learn) (descriptions []string) {
|
func getDescriptions(data *[]databases.Learn) (descriptions []string) {
|
||||||
MAX_LENGTH := 100
|
MAX_LENGTH := 100
|
||||||
|
MAX_ITEM_LENGTH := 25
|
||||||
|
|
||||||
|
tempDesc := []string{}
|
||||||
|
|
||||||
for _, data := range *data {
|
for _, data := range *data {
|
||||||
command := data.Command
|
command := data.Command
|
||||||
|
@ -64,7 +67,22 @@ func getDescriptions(data *[]databases.Learn) (descriptions []string) {
|
||||||
result = string(runeResult[:MAX_LENGTH]) + "..."
|
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
|
return
|
||||||
}
|
}
|
||||||
|
@ -173,21 +191,10 @@ func learnedDataListRun(s *discordgo.Session, m any, args *[]string) {
|
||||||
embed := &discordgo.MessageEmbed{
|
embed := &discordgo.MessageEmbed{
|
||||||
Title: fmt.Sprintf("%s님이 알려주신 지식", globalName),
|
Title: fmt.Sprintf("%s님이 알려주신 지식", globalName),
|
||||||
Color: utils.EmbedDefault,
|
Color: utils.EmbedDefault,
|
||||||
// Description: utils.CodeBlock("md", fmt.Sprintf("# 총 %d개에요.\n%s", len(data), strings.Join(getDescriptions(&data), "\n"))),
|
|
||||||
Thumbnail: &discordgo.MessageEmbedThumbnail{
|
Thumbnail: &discordgo.MessageEmbedThumbnail{
|
||||||
URL: avatarUrl,
|
URL: avatarUrl,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// 실험용 데이터
|
utils.StartPaginationEmbed(s, m, embed, getDescriptions(&data), utils.CodeBlock("md", fmt.Sprintf("# 총 %d개에요.\n", len(data))+"%s"))
|
||||||
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},
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"git.wh64.net/muffin/goMuffin/utils"
|
"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]
|
var updatedString string = utils.Decimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0]
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ type PaginationEmbed struct {
|
||||||
Total int
|
Total int
|
||||||
id string
|
id string
|
||||||
s *discordgo.Session
|
s *discordgo.Session
|
||||||
m any
|
desc string
|
||||||
}
|
}
|
||||||
|
|
||||||
var PaginationEmbeds = make(map[string]*PaginationEmbed)
|
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
|
// 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
|
var userId string
|
||||||
|
|
||||||
switch m := m.(type) {
|
switch m := m.(type) {
|
||||||
|
@ -67,10 +78,10 @@ func StartPaginationEmbed(s *discordgo.Session, m any, e *discordgo.MessageEmbed
|
||||||
Total: len(data),
|
Total: len(data),
|
||||||
id: id,
|
id: id,
|
||||||
s: s,
|
s: s,
|
||||||
m: m,
|
desc: defaultDesc,
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Embed.Description = p.Data[0]
|
p.Embed.Description = makeDesc(p.desc, data[0])
|
||||||
|
|
||||||
switch m := m.(type) {
|
switch m := m.(type) {
|
||||||
case *discordgo.MessageCreate:
|
case *discordgo.MessageCreate:
|
||||||
|
@ -102,17 +113,19 @@ func (p *PaginationEmbed) Prev(i *InteractionCreate) {
|
||||||
Embeds: []*discordgo.MessageEmbed{
|
Embeds: []*discordgo.MessageEmbed{
|
||||||
{
|
{
|
||||||
Title: "❌ 오류",
|
Title: "❌ 오류",
|
||||||
Description: "해당 페이자가 처음ㅇ이에요.",
|
Description: "해당 페이지가 처음ㅇ이에요.",
|
||||||
Color: EmbedFail,
|
Color: EmbedFail,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Current -= 1
|
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{
|
i.Update(&discordgo.InteractionResponseData{
|
||||||
Embeds: []*discordgo.MessageEmbed{p.Embed},
|
Embeds: []*discordgo.MessageEmbed{p.Embed},
|
||||||
Components: *makeComponents(p.id, p.Current, p.Total),
|
Components: *makeComponents(p.id, p.Current, p.Total),
|
||||||
|
@ -125,17 +138,19 @@ func (p *PaginationEmbed) Next(i *InteractionCreate) {
|
||||||
Embeds: []*discordgo.MessageEmbed{
|
Embeds: []*discordgo.MessageEmbed{
|
||||||
{
|
{
|
||||||
Title: "❌ 오류",
|
Title: "❌ 오류",
|
||||||
Description: "해당 페이자가 마지막ㅇ이에요.",
|
Description: "해당 페이지가 마지막ㅇ이에요.",
|
||||||
Color: EmbedFail,
|
Color: EmbedFail,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Current += 1
|
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{
|
i.Update(&discordgo.InteractionResponseData{
|
||||||
Embeds: []*discordgo.MessageEmbed{p.Embed},
|
Embeds: []*discordgo.MessageEmbed{p.Embed},
|
||||||
Components: *makeComponents(p.id, p.Current, p.Total),
|
Components: *makeComponents(p.id, p.Current, p.Total),
|
||||||
|
|
Loading…
Reference in a new issue