feat: pagination embed
I'll replace embed to ComponentsV2's container
This commit is contained in:
parent
7cf4008834
commit
f66b1cd404
6 changed files with 205 additions and 9 deletions
|
@ -179,12 +179,15 @@ func learnedDataListRun(s *discordgo.Session, m any, args *[]string) {
|
|||
},
|
||||
}
|
||||
|
||||
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, []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},
|
||||
// })
|
||||
// }
|
||||
}
|
||||
|
|
48
components/paginationEmbed.go
Normal file
48
components/paginationEmbed.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package components
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.wh64.net/muffin/goMuffin/commands"
|
||||
"git.wh64.net/muffin/goMuffin/utils"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
var PaginationEmbedComponent *commands.Component = &commands.Component{
|
||||
Parse: func(ctx *commands.ComponentContext) bool {
|
||||
i := ctx.Inter
|
||||
|
||||
if i.MessageComponentData().ComponentType == discordgo.ButtonComponent {
|
||||
customId := i.MessageComponentData().CustomID
|
||||
|
||||
if !strings.HasPrefix(customId, utils.PaginationEmbedPrev) && !strings.HasPrefix(customId, utils.PaginationEmbedNext) {
|
||||
return false
|
||||
}
|
||||
|
||||
id := utils.GetPaginationEmbedId(customId)
|
||||
userId := utils.GetPaginationEmbedUserId(id)
|
||||
|
||||
if i.Member.User.ID != userId {
|
||||
return false
|
||||
}
|
||||
|
||||
if utils.GetPaginationEmbed(id) == nil {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
},
|
||||
Run: func(ctx *commands.ComponentContext) {
|
||||
customId := ctx.Inter.MessageComponentData().CustomID
|
||||
id := utils.GetPaginationEmbedId(customId)
|
||||
p := utils.GetPaginationEmbed(id)
|
||||
|
||||
if strings.HasPrefix(customId, utils.PaginationEmbedPrev) {
|
||||
p.Prev(ctx.Inter)
|
||||
} else {
|
||||
p.Next(ctx.Inter)
|
||||
}
|
||||
},
|
||||
}
|
|
@ -7,7 +7,7 @@ import (
|
|||
"git.wh64.net/muffin/goMuffin/utils"
|
||||
)
|
||||
|
||||
const MUFFIN_VERSION = "5.1.0-gopher_dev.250512a"
|
||||
const MUFFIN_VERSION = "5.1.0-gopher_dev.250513a-paginated_embed"
|
||||
|
||||
var updatedString string = utils.Decimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0]
|
||||
|
||||
|
|
1
main.go
1
main.go
|
@ -75,6 +75,7 @@ func main() {
|
|||
go commands.Discommand.LoadCommand(commands.DeleteLearnedDataCommand)
|
||||
|
||||
go commands.Discommand.LoadComponent(components.DeleteLearnedDataComponent)
|
||||
go commands.Discommand.LoadComponent(components.PaginationEmbedComponent)
|
||||
|
||||
go dg.AddHandler(handler.MessageCreate)
|
||||
go dg.AddHandler(handler.InteractionCreate)
|
||||
|
|
143
utils/paginationEmbed.go
Normal file
143
utils/paginationEmbed.go
Normal file
|
@ -0,0 +1,143 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
// PaginationEmbed is embed with page
|
||||
type PaginationEmbed struct {
|
||||
Embed *discordgo.MessageEmbed
|
||||
Data []string
|
||||
Current int
|
||||
Total int
|
||||
id string
|
||||
s *discordgo.Session
|
||||
m any
|
||||
}
|
||||
|
||||
var PaginationEmbeds = make(map[string]*PaginationEmbed)
|
||||
|
||||
func makeComponents(id string, current, total int) *[]discordgo.MessageComponent {
|
||||
|
||||
return &[]discordgo.MessageComponent{
|
||||
discordgo.ActionsRow{
|
||||
Components: []discordgo.MessageComponent{
|
||||
discordgo.Button{
|
||||
Style: discordgo.PrimaryButton,
|
||||
Label: "이전",
|
||||
CustomID: MakePaginationEmbedPrev(id),
|
||||
Disabled: false,
|
||||
},
|
||||
discordgo.Button{
|
||||
Style: discordgo.SecondaryButton,
|
||||
Label: fmt.Sprintf("(%d/%d)", current, total),
|
||||
CustomID: MakePaginationEmbedPages(id, current, total),
|
||||
Disabled: true,
|
||||
},
|
||||
discordgo.Button{
|
||||
Style: discordgo.PrimaryButton,
|
||||
Label: "다음",
|
||||
CustomID: MakePaginationEmbedNext(id),
|
||||
Disabled: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// StartPaginationEmbed starts new PaginationEmbed struct
|
||||
func StartPaginationEmbed(s *discordgo.Session, m any, e *discordgo.MessageEmbed, data []string, length int) {
|
||||
var userId string
|
||||
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
userId = m.Author.ID
|
||||
case *InteractionCreate:
|
||||
userId = m.Member.User.ID
|
||||
}
|
||||
|
||||
id := fmt.Sprintf("%s/%d", userId, rand.Intn(12))
|
||||
p := &PaginationEmbed{
|
||||
Embed: e,
|
||||
Data: data,
|
||||
Current: 1,
|
||||
Total: len(data),
|
||||
id: id,
|
||||
s: s,
|
||||
m: m,
|
||||
}
|
||||
|
||||
p.Embed.Description = p.Data[0]
|
||||
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
s.ChannelMessageSendComplex(m.ChannelID, &discordgo.MessageSend{
|
||||
Reference: m.Reference(),
|
||||
Embeds: []*discordgo.MessageEmbed{p.Embed},
|
||||
Components: *makeComponents(id, 1, len(data)),
|
||||
})
|
||||
case *InteractionCreate:
|
||||
m.Reply(&discordgo.InteractionResponseData{
|
||||
Embeds: []*discordgo.MessageEmbed{p.Embed},
|
||||
Components: *makeComponents(id, 1, len(data)),
|
||||
})
|
||||
}
|
||||
|
||||
PaginationEmbeds[id] = p
|
||||
}
|
||||
|
||||
func GetPaginationEmbed(id string) *PaginationEmbed {
|
||||
if p, ok := PaginationEmbeds[id]; ok {
|
||||
return p
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PaginationEmbed) Prev(i *InteractionCreate) {
|
||||
if p.Current == 1 {
|
||||
i.Reply(&discordgo.InteractionResponseData{
|
||||
Embeds: []*discordgo.MessageEmbed{
|
||||
{
|
||||
Title: "❌ 오류",
|
||||
Description: "해당 페이자가 처음ㅇ이에요.",
|
||||
Color: EmbedFail,
|
||||
},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
p.Current -= 1
|
||||
|
||||
p.Embed.Description = p.Data[p.Current-1]
|
||||
i.Update(&discordgo.InteractionResponseData{
|
||||
Embeds: []*discordgo.MessageEmbed{p.Embed},
|
||||
Components: *makeComponents(p.id, p.Current, p.Total),
|
||||
})
|
||||
}
|
||||
|
||||
func (p *PaginationEmbed) Next(i *InteractionCreate) {
|
||||
if p.Current >= p.Total {
|
||||
i.Reply(&discordgo.InteractionResponseData{
|
||||
Embeds: []*discordgo.MessageEmbed{
|
||||
{
|
||||
Title: "❌ 오류",
|
||||
Description: "해당 페이자가 마지막ㅇ이에요.",
|
||||
Color: EmbedFail,
|
||||
},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
p.Current += 1
|
||||
|
||||
p.Embed.Description = p.Data[p.Current-1]
|
||||
i.Update(&discordgo.InteractionResponseData{
|
||||
Embeds: []*discordgo.MessageEmbed{p.Embed},
|
||||
Components: *makeComponents(p.id, p.Current, p.Total),
|
||||
})
|
||||
}
|
|
@ -9,4 +9,5 @@ var (
|
|||
EmojiRegexp = regexp.MustCompile(`<a?:\w+:\d+>`)
|
||||
LearnQueryCommand = regexp.MustCompile(`^단어:`)
|
||||
LearnQueryResult = regexp.MustCompile(`^대답:`)
|
||||
PaginationEmbedId = regexp.MustCompile(`^(\d+)/(\d+)$`)
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue