discutils

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2023 License: MIT Imports: 1 Imported by: 0

README

Discutils

Go Reference Go Report License

A package that provides some nice utils for discordgo.

Features

Events

This package provides some wrappers for events. Several places can register a handler. The advantage of this over the integrated solution is that these handlers can prevent further handlers from starting.

Commands

Commands are handled a bit differentially - they are parsed into an argument -> value map before being passed down to the command handler. The same is done for modals, autocomplete, etc.

This package (or rather, the events sub-package) also provides utils for registering commands both locally and to discord.

Interactions

There are abstractions for interactions - just some utils I find useful to work with. Check docs for those.

Getters

There are getters for channels, messages and users - the idea being to fetch from cache (state) first, and if that fails, fetch from the api.

How to use

go get -u github.com/shadiestgoat/discutils

Then, when you're initializing your project, remember to use InitEmbed() to customize same basics

Documentation

Index

Constants

View Source
const (
	CHAR_CROSS = "❌"
	CHAR_TICK  = "✅"
	// Empty character, used commonly for embed field names/values
	CHAR_ZWS = "\u200b"
)

Common characters used in many bots

Variables

View Source
var (
	COLOR_PRIMARY = 0xad6bee
	COLOR_SUCCESS = 0x08dd7e
	COLOR_DANGER  = 0xA51D2A
)
View Source
var BaseEmbed = discordgo.MessageEmbed{
	Title: CHAR_ZWS,
	Color: COLOR_PRIMARY,
}
View Source
var EmptyField = &discordgo.MessageEmbedField{
	Name:   CHAR_ZWS,
	Value:  CHAR_ZWS,
	Inline: true,
}

A fully empty field that is inline

View Source
var EmptyFieldNotInline = &discordgo.MessageEmbedField{
	Name:   CHAR_ZWS,
	Value:  CHAR_ZWS,
	Inline: false,
}

A fully empty field that is not inline

View Source
var ErrEmbed func(err string) *discordgo.MessageEmbed

Functions

func BitMask

func BitMask[T ~int](opts, req T) bool

Returns true if `req` is in `opts`

func DM

func DM(s *discordgo.Session, userID string, msg *discordgo.MessageSend) (resp *discordgo.Message, err error)

Shortcut to DMing a user (by also calling UserChannelCreate)

func EMessageURL

func EMessageURL(msg *discordgo.Message) string

func GEmbed

func GEmbed(title, desc string) *discordgo.MessageEmbed

Generate an embed using the base embed

func GetChannel

func GetChannel(s *discordgo.Session, channelID string) *discordgo.Channel

func GetMember

func GetMember(s *discordgo.Session, guildID, userID string) *discordgo.Member

func GetMessage

func GetMessage(s *discordgo.Session, channelID, msgID string) *discordgo.Message

func HasAllRoles

func HasAllRoles(m *discordgo.Member, roles []string) bool

Returns true if the member has all the roles mentioned in argument "roles"

func HasAnyRoles

func HasAnyRoles(m *discordgo.Member, roles []string) bool

Returns true if the member has any of the roles mentioned in argument "roles"

func HasRole

func HasRole(m *discordgo.Member, roleNeeded string) bool

Returns true if the member has the role with id roleNeeded

func IAuthor

func IAuthor(i *discordgo.Interaction) *discordgo.User

Get the author of an interaction

func IAutocomplete

Shortcut for autocomplete response

func IDefer

Respond to an interaction by deferring it

func IEmbed

Respond to an interaction with a single embed. Supports OPT_REMOVE_COMPONENTS - include it to remove components

func IError

func IError(s *discordgo.Session, i *discordgo.Interaction, errMsg string, opts ...InteractionOpt) error

Respond to an interaction with an error embed. This method calls IEmbed under the hood, so OPT_REMOVE_COMPONENTS works here too. The embed is formatted using ErrEmbed

WARNING: IS_EPHEMERAL is reversed in here! That is, by default (no opts) it has IS_EPHEMERAL, but, if IS_EPHEMERAL is present, it will make it **not** ephemeral!

func IErrorComponents

func IErrorComponents(s *discordgo.Session, i *discordgo.Interaction, errMsg string, components []discordgo.MessageComponent, opts ...InteractionOpt) error

Respond with an error embed and components. WARNING: IS_EPHEMERAL is reversed in here! That is, by default (no opts) it has IS_EPHEMERAL, but, if IS_EPHEMERAL is present, it will make it **not** ephemeral!

func IMessageAuthor

func IMessageAuthor(i *discordgo.Interaction) *discordgo.User

Returns the author of the original interaction message Use only for buttons!

func IModal

func IModal(s *discordgo.Session, i *discordgo.Interaction, customID, title string, components []discordgo.MessageComponent) error

Shortcut for modal response

func IOwnInteraction

func IOwnInteraction(msg *discordgo.Message, userID string) bool

Returns true if userID is the author of the message's interaction This can be used with regular messages or, if its a button interaction, i.Message

func IResp

Respond to an interaction, the 'lowest level' of the utils that this package provides. Can auto resolve components into Action Row (so you can directly pass buttons, without the need to nest them) This supports OPT_REMOVE_COMPONENTS. if len(conf.Comps) == 0 && OPT_REMOVE_COMPONENTS && I_EDIT_RESP, then components will be removed. Argument opts is a bit mask - eg. I_EDIT_RESP | OPT_REMOVE_COMPONENTS

func InitEmbed

func InitEmbed(opts *Options)

Configure global embed options (and colors)

func IsMemberPremium

func IsMemberPremium(mem *discordgo.Member) bool

func MemberName

func MemberName(mem *discordgo.Member) string

Returns the name that should be used for a member

If a nickname exists, it returns that. If not, it returns the

func MessageURL

func MessageURL(Guild, Channel, Message string) string

Use Guild == "@me" for DM

func PurgeChannel

func PurgeChannel(s *discordgo.Session, chanID string) error

func SendMessage

func SendMessage(s *discordgo.Session, channelID string, msg *discordgo.MessageSend) (resp *discordgo.Message, err error)

Send a message to a channel. If the channel is a news channel, auto-crosspost it.

func UpdateMessageWithSelf

func UpdateMessageWithSelf(s *discordgo.Session, i *discordgo.Interaction)

Updates a message with itself. Used to fake-acknowledge an interaction.

func Username

func Username(u *discordgo.User) string

Return the display name for the user, with a fallback to the username

Types

type IRespOpts

type IRespOpts struct {
	Embeds  []*discordgo.MessageEmbed
	Comps   []discordgo.MessageComponent
	Content *string
}

type InteractionOpt

type InteractionOpt int
const (
	// For first-time responses, updates a message. Usually used for buttons
	I_UPDATE InteractionOpt = 1 << iota
	// For editing after a response (eg. for a defer)
	I_EDIT_RESP

	// Only applicable to some operations. Should be used in conjunction with I_EDIT to remove components
	OPT_REMOVE_COMPONENTS
	// Make the message ephemeral. Should be used with I_UPDATE
	OPT_EPHEMERAL
)

type Options

type Options struct {
	// This function will be called after setting all the colors.
	// This is a function so that you can use that to your advantage.
	Base       func() discordgo.MessageEmbed
	ErrorEmbed func(err string) *discordgo.MessageEmbed

	ColorPrimary *int
	ColorSuccess *int
	ColorDanger  *int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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