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