fix: pagination embed

This commit is contained in:
Siwoo Jeon 2025-05-24 23:26:16 +09:00
parent 4cf4b42989
commit fbef008f4f
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA

View file

@ -10,7 +10,7 @@ import (
// PaginationEmbed is embed with page // PaginationEmbed is embed with page
type PaginationEmbed struct { type PaginationEmbed struct {
Container *discordgo.Container Container *discordgo.Container
Components []discordgo.MessageComponent Containers []*discordgo.Container
Current int Current int
Total int Total int
Id string Id string
@ -19,7 +19,7 @@ type PaginationEmbed struct {
var PaginationEmbeds = make(map[string]*PaginationEmbed) var PaginationEmbeds = make(map[string]*PaginationEmbed)
func NewPaginationEmbedBuilder(m any) *PaginationEmbed { func PaginationEmbedBuilder(m any) *PaginationEmbed {
var userId string var userId string
switch m := m.(type) { switch m := m.(type) {
@ -42,14 +42,14 @@ func (p *PaginationEmbed) SetContainer(container discordgo.Container) *Paginatio
return p return p
} }
func (p *PaginationEmbed) AddComponents(components ...discordgo.MessageComponent) *PaginationEmbed { func (p *PaginationEmbed) AddContainers(container ...*discordgo.Container) *PaginationEmbed {
p.Total += len(components) p.Total += len(container)
p.Components = append(p.Components, components...) p.Containers = append(p.Containers, container...)
return p return p
} }
func (p *PaginationEmbed) Start() { func (p *PaginationEmbed) Start() error {
startPaginationEmbed(p) return startPaginationEmbed(p)
} }
func makeComponents(id string, current, total int) *discordgo.ActionsRow { func makeComponents(id string, current, total int) *discordgo.ActionsRow {
@ -95,12 +95,13 @@ func MakeDesc(desc, item string) string {
} }
func startPaginationEmbed(p *PaginationEmbed) error { func startPaginationEmbed(p *PaginationEmbed) error {
p.Container.Components = append(p.Container.Components, p.Components[0], makeComponents(p.Id, p.Current, p.Total)) container := *p.Containers[0]
container.Components = append(container.Components, makeComponents(p.Id, p.Current, p.Total))
PaginationEmbeds[p.Id] = p PaginationEmbeds[p.Id] = p
err := NewMessageSender(p.m). err := NewMessageSender(p.m).
AddComponents(p.Container). AddComponents(container).
SetReply(true). SetReply(true).
SetEphemeral(true). SetEphemeral(true).
SetComponentsV2(true). SetComponentsV2(true).
@ -118,14 +119,10 @@ func GetPaginationEmbed(id string) *PaginationEmbed {
func (p *PaginationEmbed) Prev(i *InteractionCreate) { func (p *PaginationEmbed) Prev(i *InteractionCreate) {
if p.Current == 1 { if p.Current == 1 {
i.Reply(&discordgo.InteractionResponseData{ i.Reply(&discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{ Components: []discordgo.MessageComponent{
{ GetErrorContainer(discordgo.TextDisplay{Content: "해당 페이지가 처음ㅇ이에요."}),
Title: "❌ 오류",
Description: "해당 페이지가 처음ㅇ이에요.",
Color: EmbedFail,
},
}, },
Flags: discordgo.MessageFlagsEphemeral, Flags: discordgo.MessageFlagsEphemeral | discordgo.MessageFlagsIsComponentsV2,
}) })
return return
} }
@ -138,14 +135,10 @@ func (p *PaginationEmbed) Prev(i *InteractionCreate) {
func (p *PaginationEmbed) Next(i *InteractionCreate) { func (p *PaginationEmbed) Next(i *InteractionCreate) {
if p.Current >= p.Total { if p.Current >= p.Total {
i.Reply(&discordgo.InteractionResponseData{ i.Reply(&discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{ Components: []discordgo.MessageComponent{
{ GetErrorContainer(discordgo.TextDisplay{Content: "해당 페이지가 마지막ㅇ이에요."}),
Title: "❌ 오류",
Description: "해당 페이지가 마지막ㅇ이에요.",
Color: EmbedFail,
},
}, },
Flags: discordgo.MessageFlagsEphemeral, Flags: discordgo.MessageFlagsEphemeral | discordgo.MessageFlagsIsComponentsV2,
}) })
return return
} }
@ -158,39 +151,32 @@ func (p *PaginationEmbed) Next(i *InteractionCreate) {
func (p *PaginationEmbed) Set(i *InteractionCreate, page int) error { func (p *PaginationEmbed) Set(i *InteractionCreate, page int) error {
if page <= 0 { if page <= 0 {
i.Reply(&discordgo.InteractionResponseData{ i.Reply(&discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{ Components: []discordgo.MessageComponent{
{ GetErrorContainer(discordgo.TextDisplay{Content: "해당 값은 0보다 커야해요."}),
Title: "❌ 오류",
Description: "해당 값은 0보다 커야해요.",
Color: EmbedFail,
},
}, },
Flags: discordgo.MessageFlagsEphemeral, Flags: discordgo.MessageFlagsEphemeral | discordgo.MessageFlagsIsComponentsV2,
}) })
return nil return nil
} }
if page > p.Total { if page > p.Total {
i.Reply(&discordgo.InteractionResponseData{ i.Reply(&discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{ Components: []discordgo.MessageComponent{
{ GetErrorContainer(discordgo.TextDisplay{Content: "해당 값은 총 페이지의 수보다 작아야해요."}),
Title: "❌ 오류",
Description: "해당 값은 총 페이지의 수보다 작아야해요.",
Color: EmbedFail,
},
}, },
Flags: discordgo.MessageFlagsEphemeral, Flags: discordgo.MessageFlagsEphemeral | discordgo.MessageFlagsIsComponentsV2,
}) })
return nil return nil
} }
p.Current = page p.Current = page
p.Container.Components = []discordgo.MessageComponent{p.Components[p.Current-1], makeComponents(p.Id, p.Current, p.Total)} container := *p.Containers[p.Current-1]
container.Components = append(container.Components, makeComponents(p.Id, p.Current, p.Total))
err := i.Update(&discordgo.InteractionResponseData{ err := i.Update(&discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsIsComponentsV2, Flags: discordgo.MessageFlagsIsComponentsV2,
Components: []discordgo.MessageComponent{p.Container}, Components: []discordgo.MessageComponent{container},
}) })
return err return err
} }