-
Notifications
You must be signed in to change notification settings - Fork 4
/
embeds_paginator.go
114 lines (99 loc) · 3.29 KB
/
embeds_paginator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package gommand
import (
"context"
"errors"
"strconv"
"github.com/andersfylling/disgord"
)
// EmbedsPaginatorWithLifetime is used to paginate together several embeds, but with an optional *EmbedLifetimeOptions parameter to control the embed lifetime.
// Passing nil to this parameter indicates no maximum lifetime.
func EmbedsPaginatorWithLifetime(ctx *Context, Pages []*disgord.Embed, InitialPage uint, NoButtonTextContent string, Lifetime *EmbedLifetimeOptions) (err error) {
// Check the permissions which the bot has permission to use embed menus in this channel.
c, err := ctx.Channel()
if err != nil {
return err
}
m, err := ctx.BotMember()
if err != nil {
return err
}
perms, err := c.GetPermissions(context.TODO(), ctx.Session, m)
if err != nil {
return err
}
UseEmbedMenus := (perms&disgord.PermissionAdministrator) == disgord.PermissionAdministrator || ((perms&disgord.PermissionManageMessages) == disgord.PermissionManageMessages && (perms&disgord.PermissionAddReactions) == disgord.PermissionAddReactions)
// Get the pages length.
PagesLen := len(Pages)
// If initial page is greater than the length of pages, make it 0.
if int(InitialPage) > PagesLen || InitialPage == 0 {
InitialPage = 1
}
// If length of pages is 0, throw an error.
if PagesLen == 0 {
return errors.New("pages length is 0")
}
// Defines the current page.
CurrentPage := 1
// Modify the embed to be ready.
PrepareEmbed := func(em *disgord.Embed) *disgord.Embed {
em = disgord.DeepCopy(em).(*disgord.Embed)
em.Footer = &disgord.EmbedFooter{
Text: "Page " + strconv.Itoa(CurrentPage) + "/" + strconv.Itoa(PagesLen),
}
return em
}
// Defines the display and last page.
var DisplayPage *EmbedMenu
var DisplayEmbed *disgord.Embed
var LastPage *EmbedMenu
// Iterate through the pages.
for i, em := range Pages {
// Prepare the embed.
em = PrepareEmbed(em)
// Make the embed menu if we are using this method.
if UseEmbedMenus {
if LastPage == nil {
LastPage = NewEmbedMenu(em, ctx)
} else {
PageBefore := LastPage
LastPage = LastPage.NewChildMenu(&ChildMenuOptions{
Embed: em,
Button: &MenuButton{
Emoji: "▶️",
Name: "Forward",
Description: "Goes forward a page.",
},
})
LastPage.Reactions.Add(MenuReaction{
Button: &MenuButton{
Emoji: "◀️",
Name: "Back",
Description: "Goes back a page.",
},
Function: func(ChannelID, MessageID disgord.Snowflake, _ *EmbedMenu, client disgord.Session) {
_ = PageBefore.Display(ChannelID, MessageID, client)
},
})
}
if uint(i)+1 == InitialPage {
DisplayPage = LastPage
}
} else if uint(i)+1 == InitialPage {
DisplayEmbed = em
}
// Add to the page.
CurrentPage++
}
// Return displaying the embed.
if UseEmbedMenus {
if Lifetime == nil {
return ctx.DisplayEmbedMenu(DisplayPage)
}
return ctx.DisplayEmbedMenuWithLifetime(DisplayPage, Lifetime)
}
return func() (err error) { _, err = ctx.Reply(NoButtonTextContent, DisplayEmbed); return }()
}
// EmbedsPaginator is used to paginate together several embeds.
func EmbedsPaginator(ctx *Context, Pages []*disgord.Embed, InitialPage uint, NoButtonTextContent string) error {
return EmbedsPaginatorWithLifetime(ctx, Pages, InitialPage, NoButtonTextContent, nil)
}