paginator

package module
v0.0.0-...-1bdf780 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2024 License: Apache-2.0 Imports: 8 Imported by: 11

README

Go Reference Go Report Go Version License Paginator Version DisGo Discord

Paginator

Paginator is a simple embed pagination library for DisGo using buttons. It supports both interactions and normal messages.

Getting Started

Installation
$ go get github.com/disgoorg/paginator
Usage

Create a new paginator and add it as EventHandler to your Client.

manager := paginator.New()
client, err := disgo.New(token,
    bot.WithDefaultGateway(),
    bot.WithEventListeners(manager),
)
Interactions
// your data to paginate through
pData := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}

// create a new paginator.Pages
err := manager.Create(event.Respond, paginator.Pages{
    // A unique ID for this paginator
    ID: event.ID().String(), 
	// This is the function that will be called to create the embed for each page when the page is displayed
    PageFunc: func(page int, embed *discord.EmbedBuilder) {
        embed.SetTitle("Data")
        description := ""
        for i := 0; i < 5; i++ {
            if page*5+i >= len(pData) {
                break
            }
            description += pData[page*5+i] + "\n"
        }
        embed.SetDescription(description)
    },
	// The total number of pages
    Pages:      int(math.Ceil(float64(len(pData)) / 5)),
	// Optional: If the paginator should only be accessible by the user who created it
    Creator:    event.User().ID,
	// Optional: If the paginator should be deleted after x time after the last interaction
    ExpireMode: paginator.ExpireModeAfterLastUsage,
}, false)
Normal Messages
// your data to paginate through
pData := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}
channelID := 123454545
// create a new paginator.Pages
err := manager.CreateMessage(event.Client(), channelID, paginator.Pages{
    // A unique ID for this paginator
    ID: event.Message.ID.String(), 
	// This is the function that will be called to create the embed for each page when the page is displayed
    PageFunc: func(page int, embed *discord.EmbedBuilder) {
        embed.SetTitle("Data")
        description := ""
        for i := 0; i < 5; i++ {
            if page*5+i >= len(pData) {
                break
            }
            description += pData[page*5+i] + "\n"
        }
        embed.SetDescription(description)
    },
	// The total number of pages
    Pages:      int(math.Ceil(float64(len(pData)) / 5)),
	// Optional: If the paginator should only be accessible by the user who created it
    Creator:    event.User().ID,
	// Optional: If the paginator should be deleted after x time after the last interaction
    ExpireMode: paginator.ExpireModeAfterLastUsage,
}, false)

Documentation

Documentation is wip and can be found under

  • Go Reference

Examples

You can find examples here

Troubleshooting

For help feel free to open an issue or reach out on Discord

Contributing

Contributions are welcomed but for bigger changes we recommend first reaching out via Discord or create an issue to discuss your problems, intentions and ideas.

License

Distributed under the License. See LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ButtonsConfig

type ButtonsConfig struct {
	First *ComponentOptions
	Back  *ComponentOptions
	Stop  *ComponentOptions
	Next  *ComponentOptions
	Last  *ComponentOptions
}

type ComponentOptions

type ComponentOptions struct {
	Emoji discord.ComponentEmoji
	Label string
	Style discord.ButtonStyle
}

type Config

type Config struct {
	ButtonsConfig       ButtonsConfig
	NoPermissionMessage string
	CustomIDPrefix      string
	EmbedColor          int
	CleanupInterval     time.Duration
	ExpireTime          time.Duration
}

func DefaultConfig

func DefaultConfig() *Config

func (*Config) Apply

func (c *Config) Apply(opts []ConfigOpt)

type ConfigOpt

type ConfigOpt func(config *Config)

func WithButtonsConfig

func WithButtonsConfig(buttonsConfig ButtonsConfig) ConfigOpt

func WithCleanupInterval

func WithCleanupInterval(cleanupInterval time.Duration) ConfigOpt

func WithCustomIDPrefix

func WithCustomIDPrefix(prefix string) ConfigOpt

func WithEmbedColor

func WithEmbedColor(color int) ConfigOpt

func WithNoPermissionMessage

func WithNoPermissionMessage(noPermissionMessage string) ConfigOpt

func WithTimeout

func WithTimeout(timeout time.Duration) ConfigOpt

type ExpireMode

type ExpireMode int
const (
	ExpireModeAfterCreation ExpireMode = iota
	ExpireModeAfterLastUsage
)

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...ConfigOpt) *Manager

func (*Manager) Create

func (m *Manager) Create(responderFunc events.InteractionResponderFunc, pages Pages, ephemeral bool) error

func (*Manager) CreateMessage

func (m *Manager) CreateMessage(client bot.Client, channelID snowflake.ID, pages Pages, ephemeral bool) (*discord.Message, error)

func (*Manager) OnEvent

func (m *Manager) OnEvent(event bot.Event)

func (*Manager) Update

func (m *Manager) Update(responderFunc events.InteractionResponderFunc, pages Pages) error

func (*Manager) UpdateMessage

func (m *Manager) UpdateMessage(client bot.Client, channelID snowflake.ID, messageID snowflake.ID, pages Pages) (*discord.Message, error)

type Pages

type Pages struct {
	ID         string
	PageFunc   func(page int, embed *discord.EmbedBuilder)
	Pages      int
	Creator    snowflake.ID
	ExpireMode ExpireMode
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL