dgwidgets

package module
v0.0.0-...-56c8c1c Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2019 License: BSD-3-Clause Imports: 6 Imported by: 9 modules, 9 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

dg-widgets

Make widgets with embeds and reactions

img

Example usage

func (s *discordgo.Session, m *discordgo.Message) {
	p := dgwidgets.NewPaginator(s, m.ChannelID)

	// Add embed pages to paginator
	p.Add(&discordgo.MessageEmbed{Description: "Page one"}, 
		  &discordgo.MessageEmbed{Description: "Page two"},
		  &discordgo.MessageEmbed{Description: "Page three"})

	// Sets the footers of all added pages to their page numbers.
	p.SetPageFooters()

	// When the paginator is done listening set the colour to yellow
	p.ColourWhenDone = 0xffff

	// Stop listening for reaction events after five minutes
	p.Widget.Timeout = time.Minute * 5

	// Add a custom handler for the gun reaction.
	p.Widget.Handle("🔫", func(w *dgwidgets.Widget, r *discordgo.MessageReaction) {
		s.ChannelMessageSend(m.ChannelID, "Bang!")
	})

	p.Spawn()
}

Documentation

Index

Constants

View Source
const (
	NavPlus        = "➕"
	NavPlay        = "▶"
	NavPause       = "⏸"
	NavStop        = "⏹"
	NavRight       = "➡"
	NavLeft        = "⬅"
	NavUp          = "⬆"
	NavDown        = "⬇"
	NavEnd         = "⏩"
	NavBeginning   = "⏪"
	NavNumbers     = "🔢"
	NavInformation = "ℹ"
	NavSave        = "💾"
)

emoji constants

Variables

View Source
var (
	ErrAlreadyRunning   = errors.New("err: Widget already running")
	ErrIndexOutOfBounds = errors.New("err: Index is out of bounds")
	ErrNilMessage       = errors.New("err: Message is nil")
	ErrNilEmbed         = errors.New("err: embed is nil")
	ErrNotRunning       = errors.New("err: not running")
)

error vars

Functions

func EmbedsFromString

func EmbedsFromString(txt string, chunklen int) []*discordgo.MessageEmbed

EmbedsFromString splits a string into a slice of MessageEmbeds.

txt     : text to split
chunklen: How long the text in each embed should be
          (if set to 0 or less, it defaults to 2048)

Types

type Paginator

type Paginator struct {
	sync.Mutex
	Pages []*discordgo.MessageEmbed
	Index int

	// Loop back to the beginning or end when on the first or last page.
	Loop   bool
	Widget *Widget

	Ses *discordgo.Session

	DeleteMessageWhenDone   bool
	DeleteReactionsWhenDone bool
	ColourWhenDone          int
	// contains filtered or unexported fields
}

Paginator provides a method for creating a navigatable embed

func NewPaginator

func NewPaginator(ses *discordgo.Session, channelID string) *Paginator

NewPaginator returns a new Paginator

ses      : discordgo session
channelID: channelID to spawn the paginator on

func (*Paginator) Add

func (p *Paginator) Add(embeds ...*discordgo.MessageEmbed)

Add a page to the paginator

embed: embed page to add.

func (*Paginator) Goto

func (p *Paginator) Goto(index int) error

Goto jumps to the requested page index

index: The index of the page to go to

func (*Paginator) NextPage

func (p *Paginator) NextPage() error

NextPage sets the page index to the next page

func (*Paginator) Page

func (p *Paginator) Page() (*discordgo.MessageEmbed, error)

Page returns the page of the current index

func (*Paginator) PreviousPage

func (p *Paginator) PreviousPage() error

PreviousPage sets the current page index to the previous page.

func (*Paginator) Running

func (p *Paginator) Running() bool

Running returns the running status of the paginator

func (*Paginator) SetPageFooters

func (p *Paginator) SetPageFooters()

SetPageFooters sets the footer of each embed to Be its page number out of the total length of the embeds.

func (*Paginator) Spawn

func (p *Paginator) Spawn() error

Spawn spawns the paginator in channel p.ChannelID

func (*Paginator) Update

func (p *Paginator) Update() error

Update updates the message with the current state of the paginator

type Widget

type Widget struct {
	sync.Mutex
	Embed     *discordgo.MessageEmbed
	Message   *discordgo.Message
	Ses       *discordgo.Session
	ChannelID string
	Timeout   time.Duration
	Close     chan bool

	// Handlers binds emoji names to functions
	Handlers map[string]WidgetHandler
	// keys stores the handlers keys in the order they were added
	Keys []string

	// Delete reactions after they are added
	DeleteReactions bool
	// Only allow listed users to use reactions.
	UserWhitelist []string
	// contains filtered or unexported fields
}

Widget is a message embed with reactions for buttons. Accepts custom handlers for reactions.

func NewWidget

func NewWidget(ses *discordgo.Session, channelID string, embed *discordgo.MessageEmbed) *Widget

NewWidget returns a pointer to a Widget object

ses      : discordgo session
channelID: channelID to spawn the widget on

func (*Widget) Handle

func (w *Widget) Handle(emojiName string, handler WidgetHandler) error

Handle adds a handler for the given emoji name

emojiName: The unicode value of the emoji
handler  : handler function to call when the emoji is clicked
           func(*Widget, *discordgo.MessageReaction)

func (*Widget) QueryInput

func (w *Widget) QueryInput(prompt string, userID string, timeout time.Duration) (*discordgo.Message, error)

QueryInput querys the user with ID `id` for input

prompt : Question prompt
userID : UserID to get message from
timeout: How long to wait for the user's response

func (*Widget) Running

func (w *Widget) Running() bool

Running returns w.running

func (*Widget) Spawn

func (w *Widget) Spawn() error

Spawn spawns the widget in channel w.ChannelID

func (*Widget) UpdateEmbed

func (w *Widget) UpdateEmbed(embed *discordgo.MessageEmbed) (*discordgo.Message, error)

UpdateEmbed updates the embed object and edits the original message

embed: New embed object to replace w.Embed

type WidgetHandler

type WidgetHandler func(*Widget, *discordgo.MessageReaction)

WidgetHandler ...

Jump to

Keyboard shortcuts

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