gateway

package
v0.12.2 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2022 License: Apache-2.0 Imports: 15 Imported by: 71

Documentation

Index

Constants

View Source
const Version = 10

Version defines which discord API version disgo should use to connect to discord.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloseHandlerFunc added in v0.11.0

type CloseHandlerFunc func(gateway Gateway, err error)

CloseHandlerFunc is a function that is called when the Gateway is closed.

type Config

type Config struct {
	Logger                    log.Logger
	Dialer                    *websocket.Dialer
	LargeThreshold            int
	GatewayIntents            discord.GatewayIntents
	Compress                  bool
	GatewayURL                string
	ShardID                   int
	ShardCount                int
	SessionID                 *string
	LastSequenceReceived      *int
	AutoReconnect             bool
	MaxReconnectTries         int
	RateLimiter               RateLimiter
	RateRateLimiterConfigOpts []RateLimiterConfigOpt
	Presence                  *discord.GatewayMessageDataPresenceUpdate
	OS                        string
	Browser                   string
	Device                    string
}

Config lets you configure your Gateway instance.

func DefaultConfig

func DefaultConfig() *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 Server.

func WithAutoReconnect

func WithAutoReconnect(autoReconnect bool) ConfigOpt

WithAutoReconnect sets whether the Gateway should automatically reconnect to Discord.

func WithBrowser

func WithBrowser(browser string) ConfigOpt

WithBrowser sets the browser the bot is running on. See here for more information: https://discord.com/developers/docs/topics/gateway#identify-identify-connection-properties

func WithCompress

func WithCompress(compress bool) ConfigOpt

WithCompress sets whether this Gateway supports compression. See here for more information: https://discord.com/developers/docs/topics/gateway#encoding-and-compression

func WithDevice

func WithDevice(device string) ConfigOpt

WithDevice sets the device the bot is running on. See here for more information: https://discord.com/developers/docs/topics/gateway#identify-identify-connection-properties

func WithDialer added in v0.10.0

func WithDialer(dialer *websocket.Dialer) ConfigOpt

WithDialer sets the websocket.Dialer for the Gateway.

func WithGatewayIntents

func WithGatewayIntents(gatewayIntents ...discord.GatewayIntents) ConfigOpt

WithGatewayIntents sets the discord.GatewayIntents for the Gateway. See here for more information: https://discord.com/developers/docs/topics/gateway#gateway-intents

func WithGatewayURL

func WithGatewayURL(gatewayURL string) ConfigOpt

WithGatewayURL sets the Gateway URL for the Gateway.

func WithLargeThreshold

func WithLargeThreshold(largeThreshold int) ConfigOpt

WithLargeThreshold sets the threshold for the Gateway. See here for more information: https://discord.com/developers/docs/topics/gateway#identify-identify-structure

func WithLogger

func WithLogger(logger log.Logger) ConfigOpt

WithLogger sets the Logger for the Gateway.

func WithMaxReconnectTries

func WithMaxReconnectTries(maxReconnectTries int) ConfigOpt

WithMaxReconnectTries sets the maximum number of reconnect attempts before stopping.

func WithOS

func WithOS(os string) ConfigOpt

WithOS sets the operating system the bot is running on. See here for more information: https://discord.com/developers/docs/topics/gateway#identify-identify-connection-properties

func WithPresence

func WithPresence(presence discord.GatewayMessageDataPresenceUpdate) ConfigOpt

WithPresence sets the initial presence the bot should display.

func WithRateLimiter

func WithRateLimiter(rateLimiter RateLimiter) ConfigOpt

WithRateLimiter sets the grate.RateLimiter for the Gateway.

func WithRateRateLimiterConfigOpts added in v0.12.0

func WithRateRateLimiterConfigOpts(opts ...RateLimiterConfigOpt) ConfigOpt

WithRateRateLimiterConfigOpts lets you configure the default RateLimiter.

func WithSequence

func WithSequence(sequence int) ConfigOpt

WithSequence sets the last sequence received for the Gateway. If sessionID and lastSequence is present while connecting, the Gateway will try to resume the session.

func WithSessionID

func WithSessionID(sessionID string) ConfigOpt

WithSessionID sets the Session ID for the Gateway. If sessionID and lastSequence is present while connecting, the Gateway will try to resume the session.

func WithShardCount

func WithShardCount(shardCount int) ConfigOpt

WithShardCount sets the shard count for the Gateway. See here for more information on sharding: https://discord.com/developers/docs/topics/gateway#sharding

func WithShardID

func WithShardID(shardID int) ConfigOpt

WithShardID sets the shard ID for the Gateway. See here for more information on sharding: https://discord.com/developers/docs/topics/gateway#sharding

type CreateFunc

type CreateFunc func(token string, eventHandlerFunc EventHandlerFunc, closeHandlerFUnc CloseHandlerFunc, opts ...ConfigOpt) Gateway

CreateFunc is a type that is used to create a new Gateway(s).

type EventHandlerFunc

type EventHandlerFunc func(gatewayEventType discord.GatewayEventType, sequenceNumber int, shardID int, payload io.Reader)

EventHandlerFunc is a function that is called when an event is received.

type Gateway

type Gateway interface {
	// Logger returns the logger that is used by the Gateway.
	Logger() log.Logger

	// ShardID returns the shard ID that this Gateway is configured to use.
	ShardID() int

	// ShardCount returns the total number of shards that this Gateway is configured to use.
	ShardCount() int

	// SessionID returns the session ID that is used by this Gateway.
	// This may be nil if the Gateway was never connected to Discord, was gracefully closed with websocket.CloseNormalClosure or websocket.CloseGoingAway.
	SessionID() *string

	// LastSequenceReceived returns the last sequence number that was received by the Gateway.
	// This may be nil if the Gateway was never connected to Discord, was gracefully closed with websocket.CloseNormalClosure or websocket.CloseGoingAway.
	LastSequenceReceived() *int

	// GatewayIntents returns the discord.GatewayIntents that are used by this Gateway.
	GatewayIntents() discord.GatewayIntents

	// Open connects this Gateway to the Discord API.
	Open(ctx context.Context) error

	// Close gracefully closes the Gateway with the websocket.CloseNormalClosure code.
	// If the context is done, the Gateway connection will be killed.
	Close(ctx context.Context)

	// CloseWithCode closes the Gateway with the given code & message.
	// If the context is done, the Gateway connection will be killed.
	CloseWithCode(ctx context.Context, code int, message string)

	// Status returns the Status of the Gateway.
	Status() Status

	// Send sends a message to the Discord gateway with the opCode and data.
	// If context is deadline exceeds, the message sending will be aborted.
	Send(ctx context.Context, op discord.GatewayOpcode, data discord.GatewayMessageData) error

	// Latency returns the latency of the Gateway.
	// This is calculated by the time it takes to send a heartbeat and receive a heartbeat ack by discord.
	Latency() time.Duration
}

Gateway is what is used to connect to discord.

func New

func New(token string, eventHandlerFunc EventHandlerFunc, closeHandlerFunc CloseHandlerFunc, opts ...ConfigOpt) Gateway

New creates a new Gateway instance with the provided token, eventHandlerFunc, closeHandlerFunc and ConfigOpt(s).

type RateLimiter added in v0.12.0

type RateLimiter interface {
	// Logger returns the logger used by the RateLimiter.
	Logger() log.Logger

	// Close gracefully closes the RateLimiter.
	// If the context deadline is exceeded, the RateLimiter will be closed immediately.
	Close(ctx context.Context)

	// Reset resets the RateLimiter to its initial state.
	Reset()

	// Wait waits for the RateLimiter to be ready to send a new message.
	// If the context deadline is exceeded, Wait will return immediately and no message will be sent.
	Wait(ctx context.Context) error

	// Unlock unlocks the RateLimiter and allows the next message to be sent.
	Unlock()
}

RateLimiter provides handles the rate limiting logic for connecting to Discord's Gateway.

func NewRateLimiter added in v0.12.0

func NewRateLimiter(opts ...RateLimiterConfigOpt) RateLimiter

NewRateLimiter creates a new default RateLimiter with the given RateLimiterConfigOpt(s).

type RateLimiterConfig added in v0.12.0

type RateLimiterConfig struct {
	Logger            log.Logger
	CommandsPerMinute int
}

RateLimiterConfig lets you configure your Gateway instance.

func DefaultRateLimiterConfig added in v0.12.0

func DefaultRateLimiterConfig() *RateLimiterConfig

DefaultRateLimiterConfig returns a RateLimiterConfig with sensible defaults.

func (*RateLimiterConfig) Apply added in v0.12.0

func (c *RateLimiterConfig) Apply(opts []RateLimiterConfigOpt)

Apply applies the given RateLimiterConfigOpt(s) to the RateLimiterConfig

type RateLimiterConfigOpt added in v0.12.0

type RateLimiterConfigOpt func(config *RateLimiterConfig)

RateLimiterConfigOpt is a type alias for a function that takes a RateLimiterConfig and is used to configure your Server.

func WithCommandsPerMinute added in v0.12.0

func WithCommandsPerMinute(commandsPerMinute int) RateLimiterConfigOpt

WithCommandsPerMinute sets the number of commands per minute that the Gateway will allow.

func WithRateLimiterLogger added in v0.12.0

func WithRateLimiterLogger(logger log.Logger) RateLimiterConfigOpt

WithRateLimiterLogger sets the Logger for the Gateway.

type Status

type Status int

Status is the state that the client is currently in.

const (
	// StatusUnconnected is the initial state when a new Gateway is created.
	StatusUnconnected Status = iota

	// StatusConnecting is the state when the client is connecting to the Discord gateway.
	StatusConnecting

	// StatusWaitingForHello is the state when the Gateway is waiting for the first discord.GatewayOpcodeHello packet.
	StatusWaitingForHello

	// StatusIdentifying is the state when the Gateway received its first discord.GatewayOpcodeHello packet and now sends a discord.GatewayOpcodeIdentify packet.
	StatusIdentifying

	// StatusResuming is the state when the Gateway received its first discord.GatewayOpcodeHello packet and now sends a discord.GatewayOpcodeResume packet.
	StatusResuming

	// StatusWaitingForReady is the state when the Gateway received sent a discord.GatewayOpcodeIdentify or discord.GatewayOpcodeResume packet and now waits for a discord.GatewayOpcodeDispatch with discord.GatewayEventTypeReady packet.
	StatusWaitingForReady

	// StatusReady is the state when the Gateway received a discord.GatewayOpcodeDispatch with discord.GatewayEventTypeReady packet.
	StatusReady

	// StatusDisconnected is the state when the Gateway is disconnected.
	// Either due to an error or because the Gateway was closed gracefully.
	StatusDisconnected
)

Indicates how far along the client is too connecting.

func (Status) IsConnected

func (s Status) IsConnected() bool

IsConnected returns whether the Gateway is connected.

Jump to

Keyboard shortcuts

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