tools

package
v1.10.21 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2025 License: Apache-2.0 Imports: 12 Imported by: 1

README

Disgo Tools

The Disgo Tools package contains utility tools that help you create a Disgo Bot.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ReconnectOnUnexpectedDisconnectionHandlers represents default handlers the user can pass to
	// ReconnectOnUnexpectedDisconnection() to reconnect the bot.
	//
	// Read https://github.com/switchupcb/disgo/discussions/82 for more information about each handler.
	ReconnectOnUnexpectedDisconnectionHandlers = []func(error) bool{

		func(err error) bool {
			return strings.Contains(err.Error(), "failed to get reader: failed to read frame header: EOF")
		},

		func(err error) bool {
			closeErr := new(websocket.CloseError)

			return errors.As(err, closeErr) && closeErr.Code == 1001
		},
	}

	// ReconnectOnUnexpectedDisconnectionWait represents the amount of time to wait before reconnecting
	// to the Discord Gateway in ReconnectOnUnexpectedDisconnection() [default: time.Second * 5].
	ReconnectOnUnexpectedDisconnectionWait = time.Second * 5
)
View Source
var (
	// Signals represents common termination signals used to terminate the program.
	//
	// https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
	Signals = []os.Signal{
		syscall.SIGTERM,
		syscall.SIGINT,
		syscall.SIGQUIT,
		syscall.SIGHUP,
	}
)

Functions

func DataURI

func DataURI(contentType, base64EncodedData string) string

DataURI returns a Data URI from the given HTTP Content Type Header and base64 encoded data.

https://en.wikipedia.org/wiki/Data_URI_scheme

func ImageDataURI

func ImageDataURI(img []byte) string

ImageDataURI returns a Data URI from the given image data.

func InterceptSignal

func InterceptSignal(signals []os.Signal, sessions ...*disgo.Session) error

InterceptSignal blocks until the provided signals are intercepted.

Upon receiving a signal, the given sessions are gracefully disconnected.

func NumOptions

func NumOptions(options []*disgo.ApplicationCommandInteractionDataOption) int

NumOptions determines the amount of options (and suboptions) in a given array of options.

func OptionsToMap

OptionsToMap parses an array of options and suboptions into an OptionMap.

func ReconnectOnUnexpectedDisconnection added in v1.10.21

func ReconnectOnUnexpectedDisconnection(bot *disgo.Client, s *disgo.Session, reconnectOnErr ...func(error) bool)

ReconnectOnUnexpectedDisconnection reconnects a session when it disconnects unexpectedly.

Use this function with `tools.ReconnectOnUnexpectedDisconnectionHandlers` or custom handlers (`func(error) bool`): The bot is reconnected when a single handler returns true.

WARNING: You are recommended to run this function on a goroutine.

Calling this function blocks the thread until the Session exits the connection loop.

Types

This section is empty.

Jump to

Keyboard shortcuts

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