dgutil

package module
v0.0.0-...-3423ab7 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2024 License: MIT Imports: 11 Imported by: 0

README

dgutil

dgutil is a utility package for simplifying the creation of Discord bots. This primarily exists for personal use, so its approaches to certain things are highly opinionated, such as always pulling the auth token for Discord from an environment variable.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoToken = errors.New("DISCORD_TOKEN environment variable not set")

ErrNoToken is returned by Run if the token could not be found.

Functions

func AddHandler

func AddHandler[T any](ctx context.Context, dg *discordgo.Session, h func(context.Context, *discordgo.Session, T)) func()

AddHandler adds an event handler to a discordgo.Session, but includes an extra context argument and panic protection. The context provided to the handler itself will be a child of ctx that will be canceled when the handler returns.

func Run

func Run(ctx context.Context, setup func(*Setup) error) error

Run runs a Discord bot. It pulls the auth token from the $DISCORD_TOKEN environment variable, connects to Discord's API, then calls the provided setup function. When the provided context is canceled, it will exit, cleaning up whatever it did while setting up.

func SetupResponse

func SetupResponse(dg *discordgo.Session, i *discordgo.Interaction) error

SetupResponse sets up the interaction for a deferred response.

func UpdateResponse

func UpdateResponse(dg *discordgo.Session, i *discordgo.Interaction, content string) error

UpdateResponse sets the content of a deferred interaction response as set up by [setupResponse].

Types

type Setup

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

Setup is a wrapper that helps with setting up the configuration for a bot.

func (*Setup) RegisterCommands

func (s *Setup) RegisterCommands(commands iter.Seq[*discordgo.ApplicationCommand])

RegisterCommands registers a set of commands with the underlying discordgo.Session for every guild that the bot is in. When the bot exits, the commands will be automatically unregistered.

func (*Setup) Session

func (s *Setup) Session() *discordgo.Session

Session returns the underlying discordgo.Session.

Jump to

Keyboard shortcuts

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