bot

package
v0.13.4 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2022 License: Apache-2.0 Imports: 15 Imported by: 133

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MemberChunkingFilterAll

func MemberChunkingFilterAll(_ snowflake.ID) bool

MemberChunkingFilterAll is a MemberChunkingFilter which includes all guilds.

func MemberChunkingFilterDefault added in v0.12.0

func MemberChunkingFilterDefault(guildID snowflake.ID) bool

MemberChunkingFilterDefault is the default MemberChunkingFilter.

func MemberChunkingFilterNone

func MemberChunkingFilterNone(_ snowflake.ID) bool

MemberChunkingFilterNone is a MemberChunkingFilter which excludes all guilds.

func NewEventCollector

func NewEventCollector[E Event](client Client, filterFunc func(e E) bool) (<-chan E, func())

NewEventCollector returns a channel in which the events of type T gets sent which pass the passed filter and a function which can be used to stop the event collector. The close function needs to be called to stop the event collector.

func WaitForEvent added in v0.10.3

func WaitForEvent[E Event](client Client, ctx context.Context, filterFunc func(e E) bool, actionFunc func(e E), cancelFunc func())

WaitForEvent waits for an event passing the filterFunc and then calls the actionFunc. You can cancel this function with the passed context.Context and the cancelFunc gets called then.

Types

type Client

type Client interface {
	// Logger returns the logger for the client.
	Logger() log.Logger

	// Close will clean up all disgo internals and close the discord gracefully.
	Close(ctx context.Context)

	// Token returns the configured bot token.
	Token() string

	// ApplicationID returns the application id.
	ApplicationID() snowflake.ID

	// ID returns the bot id.
	ID() snowflake.ID

	// Caches returns the cache.Caches used by the Client.
	Caches() cache.Caches

	// Rest returns the rest.Rest used by the Client.
	Rest() rest.Rest

	// AddEventListeners adds one or more EventListener(s) to the EventManager.
	AddEventListeners(listeners ...EventListener)

	// RemoveEventListeners removes one or more EventListener(s) from the EventManager
	RemoveEventListeners(listeners ...EventListener)

	// EventManager returns the EventManager used by the Client.
	EventManager() EventManager

	// OpenGateway connects to the configured gateway.Gateway.
	OpenGateway(ctx context.Context) error

	// Gateway returns the gateway.Gateway used by the Client.
	Gateway() gateway.Gateway

	// HasGateway returns whether the Client has a configured gateway.Gateway.
	HasGateway() bool

	// OpenShardManager connects to the configured sharding.ShardManager.
	OpenShardManager(ctx context.Context) error

	// ShardManager returns the sharding.ShardManager used by the Client.
	ShardManager() sharding.ShardManager

	// HasShardManager returns whether the Client has a configured sharding.ShardManager.
	HasShardManager() bool

	// Shard returns the gateway.Gateway the specific guildID runs on.
	Shard(guildID snowflake.ID) (gateway.Gateway, error)

	// Connect sends a discord.MessageDataVoiceStateUpdate to the specific gateway.Gateway and connects the bot to the specified channel.
	Connect(ctx context.Context, guildID snowflake.ID, channelID snowflake.ID) error

	// Disconnect sends a discord.MessageDataVoiceStateUpdate to the specific gateway.Gateway and disconnects the bot from this guild.
	Disconnect(ctx context.Context, guildID snowflake.ID) error

	// RequestMembers sends a discord.MessageDataRequestGuildMembers to the specific gateway.Gateway and requests the Member(s) of the specified guild.
	//  guildID  : is the snowflake of the guild to request the members of.
	//  presence : Weather or not to include discord.Presence data.
	//  nonce	 : The nonce to return to the discord.EventGuildMembersChunk.
	//  userIDs  : The snowflakes of the users to request the members of.
	RequestMembers(ctx context.Context, guildID snowflake.ID, presence bool, nonce string, userIDs ...snowflake.ID) error

	// RequestMembersWithQuery sends a discord.MessageDataRequestGuildMembers to the specific gateway.Gateway and requests the Member(s) of the specified guild.
	//  guildID  : is the snowflake of the guild to request the members of.
	//  presence : Weather or not to include discord.Presence data.
	//  nonce    : The nonce to return to the discord.EventGuildMembersChunk.
	//  query    : The query to use for the request.
	//  limit    : The number of discord.Member(s) to return.
	RequestMembersWithQuery(ctx context.Context, guildID snowflake.ID, presence bool, nonce string, query string, limit int) error

	// SetPresence sends a discord.MessageDataPresenceUpdate to the gateway.Gateway.
	SetPresence(ctx context.Context, presenceUpdate gateway.MessageDataPresenceUpdate) error

	// SetPresenceForShard sends a discord.MessageDataPresenceUpdate to the specific gateway.Gateway.
	SetPresenceForShard(ctx context.Context, shardId int, presenceUpdate gateway.MessageDataPresenceUpdate) error

	// MemberChunkingManager returns the MemberChunkingManager used by the Client.
	MemberChunkingManager() MemberChunkingManager

	// OpenHTTPServer starts the configured HTTPServer used for interactions over webhooks.
	OpenHTTPServer() error

	// HTTPServer returns the configured HTTPServer used for interactions over webhooks.
	HTTPServer() httpserver.Server

	// HasHTTPServer returns whether the Client has a configured HTTPServer.
	HasHTTPServer() bool
}

Client is a high level interface for interacting with Discord's API. It combines the functionality of the rest, gateway/sharding, httpserver and cache into one easy to use interface. Create a new client with disgo.New.

func BuildClient

func BuildClient(token string, config Config, gatewayEventHandlerFunc func(client Client) gateway.EventHandlerFunc, httpServerEventHandlerFunc func(client Client) httpserver.EventHandlerFunc, os string, name string, github string, version string) (Client, error)

BuildClient creates a new Client instance with the given token, Config, gateway handlers, http handlers os, name, github & version.

type Config

type Config struct {
	Logger log.Logger

	RestClient           rest.Client
	RestClientConfigOpts []rest.ConfigOpt
	Rest                 rest.Rest

	EventManager           EventManager
	EventManagerConfigOpts []EventManagerConfigOpt

	Gateway           gateway.Gateway
	GatewayConfigOpts []gateway.ConfigOpt

	ShardManager           sharding.ShardManager
	ShardManagerConfigOpts []sharding.ConfigOpt

	HTTPServer           httpserver.Server
	PublicKey            string
	HTTPServerConfigOpts []httpserver.ConfigOpt

	Caches          cache.Caches
	CacheConfigOpts []cache.ConfigOpt

	MemberChunkingManager MemberChunkingManager
	MemberChunkingFilter  MemberChunkingFilter
}

Config lets you configure your Client instance.

func DefaultConfig

func DefaultConfig(gatewayHandlers map[gateway.EventType]GatewayEventHandler, httpHandler HTTPServerEventHandler) *Config

DefaultConfig returns a Config with sensible defaults.

func (*Config) Apply

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

Apply applies the given ConfigOpt(s) to the Config

type ConfigOpt

type ConfigOpt func(config *Config)

ConfigOpt is a type alias for a function that takes a Config and is used to configure your Client.

func WithCacheConfigOpts

func WithCacheConfigOpts(opts ...cache.ConfigOpt) ConfigOpt

WithCacheConfigOpts lets you configure the default cache.Caches.

func WithCaches

func WithCaches(caches cache.Caches) ConfigOpt

WithCaches lets you inject your own cache.Caches.

func WithDefaultGateway added in v0.13.3

func WithDefaultGateway() ConfigOpt

WithDefaultGateway creates a gateway.Gateway with sensible defaults.

func WithDefaultShardManager added in v0.13.3

func WithDefaultShardManager() ConfigOpt

WithDefaultShardManager creates a sharding.ShardManager with sensible defaults.

func WithEventListenerFunc added in v0.11.2

func WithEventListenerFunc[E Event](listenerFunc func(e E)) ConfigOpt

WithEventListenerFunc adds the given ListenerFunc(s) to the default EventManager.

func WithEventListeners

func WithEventListeners(eventListeners ...EventListener) ConfigOpt

WithEventListeners adds the given EventListener(s) to the default EventManager.

func WithEventManager

func WithEventManager(eventManager EventManager) ConfigOpt

WithEventManager lets you inject your own EventManager.

func WithEventManagerConfigOpts

func WithEventManagerConfigOpts(opts ...EventManagerConfigOpt) ConfigOpt

WithEventManagerConfigOpts lets you configure the default EventManager.

func WithGateway

func WithGateway(gateway gateway.Gateway) ConfigOpt

WithGateway lets you inject your own gateway.Gateway.

func WithGatewayConfigOpts

func WithGatewayConfigOpts(opts ...gateway.ConfigOpt) ConfigOpt

WithGatewayConfigOpts lets you configure the default gateway.Gateway.

func WithHTTPServer

func WithHTTPServer(httpServer httpserver.Server) ConfigOpt

WithHTTPServer lets you inject your own httpserver.Server.

func WithHTTPServerConfigOpts

func WithHTTPServerConfigOpts(publicKey string, opts ...httpserver.ConfigOpt) ConfigOpt

WithHTTPServerConfigOpts lets you configure the default httpserver.Server.

func WithLogger

func WithLogger(logger log.Logger) ConfigOpt

WithLogger lets you inject your own logger implementing log.Logger.

func WithMemberChunkingFilter

func WithMemberChunkingFilter(memberChunkingFilter MemberChunkingFilter) ConfigOpt

WithMemberChunkingFilter lets you configure the default MemberChunkingFilter.

func WithMemberChunkingManager

func WithMemberChunkingManager(memberChunkingManager MemberChunkingManager) ConfigOpt

WithMemberChunkingManager lets you inject your own MemberChunkingManager.

func WithRest

func WithRest(rest rest.Rest) ConfigOpt

WithRest lets you inject your own rest.Rest.

func WithRestClient

func WithRestClient(restClient rest.Client) ConfigOpt

WithRestClient lets you inject your own rest.Client.

func WithRestClientConfigOpts

func WithRestClientConfigOpts(opts ...rest.ConfigOpt) ConfigOpt

WithRestClientConfigOpts let's you configure the default rest.Client.

func WithShardManager

func WithShardManager(shardManager sharding.ShardManager) ConfigOpt

WithShardManager lets you inject your own sharding.ShardManager.

func WithShardManagerConfigOpts

func WithShardManagerConfigOpts(opts ...sharding.ConfigOpt) ConfigOpt

WithShardManagerConfigOpts lets you configure the default sharding.ShardManager.

type Event

type Event interface {
	Client() Client
	SequenceNumber() int
}

Event the basic interface each event implement

type EventListener

type EventListener interface {
	OnEvent(event Event)
}

EventListener is used to create new EventListener to listen to events

type EventManager

type EventManager interface {
	// AddEventListeners adds one or more EventListener(s) to the EventManager
	AddEventListeners(eventListeners ...EventListener)

	// RemoveEventListeners removes one or more EventListener(s) from the EventManager
	RemoveEventListeners(eventListeners ...EventListener)

	// HandleGatewayEvent calls the correct GatewayEventHandler for the payload
	HandleGatewayEvent(gatewayEventType gateway.EventType, sequenceNumber int, shardID int, event gateway.EventData)

	// HandleHTTPEvent calls the HTTPServerEventHandler for the payload
	HandleHTTPEvent(respondFunc httpserver.RespondFunc, event gateway.EventInteractionCreate)

	// DispatchEvent dispatches a new Event to the Client's EventListener(s)
	DispatchEvent(event Event)
}

EventManager lets you listen for specific events triggered by raw gateway events

func NewEventManager

func NewEventManager(client Client, opts ...EventManagerConfigOpt) EventManager

NewEventManager returns a new EventManager with the EventManagerConfigOpt(s) applied.

type EventManagerConfig

type EventManagerConfig struct {
	EventListeners     []EventListener
	AsyncEventsEnabled bool

	GatewayHandlers   map[gateway.EventType]GatewayEventHandler
	HTTPServerHandler HTTPServerEventHandler
}

EventManagerConfig can be used to configure the EventManager.

func DefaultEventManagerConfig

func DefaultEventManagerConfig() *EventManagerConfig

DefaultEventManagerConfig returns a new EventManagerConfig with all default values.

func (*EventManagerConfig) Apply

func (c *EventManagerConfig) Apply(opts []EventManagerConfigOpt)

Apply applies the given EventManagerConfigOpt(s) to the EventManagerConfig.

type EventManagerConfigOpt

type EventManagerConfigOpt func(config *EventManagerConfig)

EventManagerConfigOpt is a functional option for configuring an EventManager.

func WithAsyncEventsEnabled

func WithAsyncEventsEnabled() EventManagerConfigOpt

WithAsyncEventsEnabled enables/disables the async events.

func WithGatewayHandlers

func WithGatewayHandlers(handlers map[gateway.EventType]GatewayEventHandler) EventManagerConfigOpt

WithGatewayHandlers overrides the default GatewayEventHandler(s) in the EventManagerConfig.

func WithHTTPServerHandler

func WithHTTPServerHandler(handler HTTPServerEventHandler) EventManagerConfigOpt

WithHTTPServerHandler overrides the given HTTPServerEventHandler in the EventManagerConfig.

func WithListenerFunc added in v0.11.2

func WithListenerFunc[E Event](listenerFunc func(e E)) EventManagerConfigOpt

WithListenerFunc adds the given ListenerFunc(s) to the EventManagerConfig.

func WithListeners

func WithListeners(listeners ...EventListener) EventManagerConfigOpt

WithListeners adds the given EventListener(s) to the EventManagerConfig.

type GatewayEventHandler

type GatewayEventHandler interface {
	EventType() gateway.EventType
	HandleGatewayEvent(client Client, sequenceNumber int, shardID int, event gateway.EventData)
}

GatewayEventHandler is used to handle Gateway Event(s)

func NewGatewayEventHandler added in v0.13.0

func NewGatewayEventHandler[T gateway.EventData](eventType gateway.EventType, handleFunc func(client Client, sequenceNumber int, shardID int, event T)) GatewayEventHandler

NewGatewayEventHandler returns a new GatewayEventHandler for the given GatewayEventType and handler func

type HTTPServerEventHandler

type HTTPServerEventHandler interface {
	HandleHTTPEvent(client Client, respondFunc httpserver.RespondFunc, event gateway.EventInteractionCreate)
}

HTTPServerEventHandler is used to handle HTTP Event(s)

type ListenerFunc added in v0.11.2

type ListenerFunc[E Event] struct {
	F func(e E)
}

ListenerFunc is a wrapper for a func(e E) as functions are not comparable

func NewListenerFunc added in v0.11.2

func NewListenerFunc[E Event](f func(e E)) *ListenerFunc[E]

NewListenerFunc returns a new ListenerFunc for the given func(e E)

func (*ListenerFunc[E]) OnEvent added in v0.11.2

func (l *ListenerFunc[E]) OnEvent(e Event)

OnEvent calls the func(e E) if E is Event

type MemberChunkingFilter

type MemberChunkingFilter func(guildID snowflake.ID) bool

MemberChunkingFilter is a filter that can be used to filter from which guilds to request members from.

func AllMemberChunkingFilters added in v0.12.0

func AllMemberChunkingFilters[T any](filters ...MemberChunkingFilter) MemberChunkingFilter

AllMemberChunkingFilters is a shorthand for MemberChunkingFilter.And(MemberChunkingFilter).And(MemberChunkingFilter) etc.

func AnyMemberChunkingFilter added in v0.12.0

func AnyMemberChunkingFilter[T any](filters ...MemberChunkingFilter) MemberChunkingFilter

AnyMemberChunkingFilter is a shorthand for MemberChunkingFilter.Or(MemberChunkingFilter).Or(MemberChunkingFilter) etc.

func MemberChunkingFilterExcludeGuildIDs added in v0.11.0

func MemberChunkingFilterExcludeGuildIDs(guildIDs ...snowflake.ID) MemberChunkingFilter

MemberChunkingFilterExcludeGuildIDs returns a MemberChunkingFilter which excludes the given guildIDs.

func MemberChunkingFilterIncludeGuildIDs added in v0.11.0

func MemberChunkingFilterIncludeGuildIDs(guildIDs ...snowflake.ID) MemberChunkingFilter

MemberChunkingFilterIncludeGuildIDs returns a MemberChunkingFilter which includes the given guildIDs.

func (MemberChunkingFilter) And added in v0.11.0

And allows you to require both MemberChunkingFilter(s) to be true for the guild to be chunked

func (MemberChunkingFilter) Or added in v0.11.0

Or allows you to combine the MemberChunkingFilter with another, meaning either of them needs to be true for the guild to be chunked.

type MemberChunkingManager

type MemberChunkingManager interface {
	// MemberChunkingFilter returns the configured MemberChunkingFilter used by this MemberChunkingManager.
	MemberChunkingFilter() MemberChunkingFilter

	// HandleChunk handles the discord.EventGuildMembersChunk event payloads from the discord gateway.
	HandleChunk(payload gateway.EventGuildMembersChunk)

	// RequestMembers requests members from the given guildID and userIDs.
	// Notice: This action requires the gateway.IntentGuildMembers.
	RequestMembers(guildID snowflake.ID, userIDs ...snowflake.ID) ([]discord.Member, error)
	// RequestMembersWithQuery requests members from the given guildID and query.
	// query : string the username starts with
	// Notice: This action requires the gateway.IntentGuildMembers.
	RequestMembersWithQuery(guildID snowflake.ID, query string, limit int) ([]discord.Member, error)
	// RequestMembersWithFilter requests members from the given guildID and userIDs. memberFilterFunc is used to filter all returned members.
	// Notice: This action requires the gateway.IntentGuildMembers.
	RequestMembersWithFilter(guildID snowflake.ID, memberFilterFunc func(member discord.Member) bool) ([]discord.Member, error)

	// RequestMembersCtx requests members from the given guildID and userIDs.
	// Notice: This action requires the gateway.IntentGuildMembers.
	RequestMembersCtx(ctx context.Context, guildID snowflake.ID, userIDs ...snowflake.ID) ([]discord.Member, error)
	// RequestMembersWithQueryCtx requests members from the given guildID and query.
	// Notice: This action requires the gateway.IntentGuildMembers.
	RequestMembersWithQueryCtx(ctx context.Context, guildID snowflake.ID, query string, limit int) ([]discord.Member, error)
	// RequestMembersWithFilterCtx requests members from the given guildID and userIDs. memberFilterFunc is used to filter all returned members.
	// Notice: This action requires the gateway.IntentGuildMembers.
	RequestMembersWithFilterCtx(ctx context.Context, guildID snowflake.ID, memberFilterFunc func(member discord.Member) bool) ([]discord.Member, error)

	// RequestMembersChan requests members from the given guildID and userIDs.
	// Returns a channel which will receive the members.
	// Returns a function which can be used to cancel the request and close the channel.
	// Notice: This action requires the gateway.IntentGuildMembers.
	RequestMembersChan(guildID snowflake.ID, userIDs ...snowflake.ID) (<-chan discord.Member, func(), error)
	// RequestMembersWithQueryChan requests members from the given guildID and query.
	// Returns a channel which will receive the members.
	// Returns a function which can be used to cancel the request and close the channel.
	// Notice: This action requires the gateway.IntentGuildMembers.
	RequestMembersWithQueryChan(guildID snowflake.ID, query string, limit int) (<-chan discord.Member, func(), error)
	// RequestMembersWithFilterChan requests members from the given guildID and userIDs. memberFilterFunc is used to filter all returned members.
	// Returns a channel which will receive the members.
	// Returns a function which can be used to cancel the request and close the channel.
	// Notice: This action requires the gateway.IntentGuildMembers.
	RequestMembersWithFilterChan(guildID snowflake.ID, memberFilterFunc func(member discord.Member) bool) (<-chan discord.Member, func(), error)
}

MemberChunkingManager is used to request members for guilds from the discord gateway.

func NewMemberChunkingManager

func NewMemberChunkingManager(client Client, memberChunkingFilter MemberChunkingFilter) MemberChunkingManager

NewMemberChunkingManager returns a new MemberChunkingManager with the given MemberChunkingFilter.

Jump to

Keyboard shortcuts

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