Documentation
¶
Index ¶
- Variables
- type Client
- type ClosedState
- type ConnectedState
- type DefaultHeartbeatHandler
- type DiscordError
- type Handler
- type HeartbeatHandler
- type Hello
- type HelloState
- type Identify
- type IdentifyConnectionProperties
- type Logger
- type Option
- func WithBotToken(token string) Option
- func WithCommandRateLimiter(ratelimiter RateLimiter) Option
- func WithDirectMessageEvents(events ...event.Type) Option
- func WithEventHandler(handler Handler) Option
- func WithExistingSession(deadClient *Client) Option
- func WithGuildEvents(events ...event.Type) Option
- func WithHeartbeatHandler(handler HeartbeatHandler) Option
- func WithIdentifyConnectionProperties(properties *IdentifyConnectionProperties) Option
- func WithIdentifyRateLimiter(ratelimiter RateLimiter) Option
- func WithIntents(intents intent.Type) Option
- func WithLogger(logger Logger) Option
- func WithShardInfo(id ShardID, count int) Option
- type Payload
- type RateLimiter
- type Ready
- type ReadyState
- type ResumableClosedState
- type Resume
- type ResumeState
- type ShardID
- type State
- type StateCloser
- type StateCtx
- func (ctx *StateCtx) Close(closeWriter io.Writer) error
- func (ctx *StateCtx) CloseCodeHandler(payload *Payload) error
- func (ctx *StateCtx) Process(payload *Payload, pipe io.Writer) error
- func (ctx *StateCtx) SessionIssueHandler(payload *Payload) error
- func (ctx *StateCtx) SetState(state State)
- func (ctx *StateCtx) String() string
- func (ctx *StateCtx) Write(pipe io.Writer, evt event.Type, payload encoding.RawMessage) error
- func (ctx *StateCtx) WriteNormalClose(pipe io.Writer) error
- func (ctx *StateCtx) WriteRestartClose(pipe io.Writer) error
Constants ¶
This section is empty.
Variables ¶
var ErrIdentifyRateLimited = fmt.Errorf("can't send identify command: %w", ErrRateLimited)
var ErrNotConnectedYet = errors.New("client is not in a connected state")
var ErrOutOfSync = errors.New("sequence number was out of sync")
var ErrRateLimited = errors.New("unable to send message to Discord due to hitting rate limited")
var ErrSequenceNumberSkipped = errors.New("the sequence number increased with more than 1, events lost")
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client provides a user target interface, for simplified Discord interaction.
Note: It's not suitable for internal processes/states.
func (*Client) ProcessNext ¶
ProcessNext processes the next Discord message and update state accordingly. On error, you are expected to call Client.Close to notify Discord about any issues accumulated in the Client.
func (*Client) ResumeURL ¶
ResumeURL returns the URL to be used when dialing a new websocket connection. An empty string is returned when the shard can not be resumed, and you should instead use "Get Gateway Bot" endpoint to fetch the correct URL for connecting.
The client is assumed to have been correctly closed before calling this.
type ClosedState ¶
type ClosedState struct { }
func (*ClosedState) String ¶
func (st *ClosedState) String() string
type ConnectedState ¶
type ConnectedState struct {
// contains filtered or unexported fields
}
ConnectedState handles any discord events after a successful gateway connection. The only possible state after this is the ClosedState or it's derivatives such as a resumable state.
See the Discord documentation for more information:
- https://discord.com/developers/docs/topics/gateway#dispatch-events
- https://discord.com/developers/docs/topics/gateway#heartbeat-interval-example-heartbeat-ack
- https://discord.com/developers/docs/topics/gateway#heartbeat-requests
func (*ConnectedState) Process ¶
func (st *ConnectedState) Process(payload *Payload, pipe io.Writer) error
func (*ConnectedState) String ¶
func (st *ConnectedState) String() string
type DefaultHeartbeatHandler ¶
type DefaultHeartbeatHandler struct { TextWriter io.Writer // ConnectionCloser assumes that by closing the connection we can trigger an interrupt signal for whatever process // that is busy reading/waiting for the next websocket frame/message. After an interrupt you are expected to call // Client.Close - allowing the client to properly update its internal state. This allows the client to be operated // be a single process, avoiding the need of locking complexity (see gatewayutil/shard.go for an example). // // If this doesn't achieve what you need/want, then implement you own version using the HeartbeatHandler interface. ConnectionCloser io.Closer // contains filtered or unexported fields }
func (*DefaultHeartbeatHandler) Configure ¶
func (p *DefaultHeartbeatHandler) Configure(ctx *StateCtx, interval time.Duration)
func (*DefaultHeartbeatHandler) Run ¶
func (p *DefaultHeartbeatHandler) Run()
type DiscordError ¶
func (DiscordError) CanReconnect ¶
func (c DiscordError) CanReconnect() bool
func (*DiscordError) Error ¶
func (c *DiscordError) Error() string
type HeartbeatHandler ¶
type HelloState ¶
type HelloState struct { Identity *Identify // contains filtered or unexported fields }
HelloState is one of several initial state for the client. It's responsibility are as follows
- Process incoming Hello event
- Initiate a heartbeat process
- Send Identify message
- Transition to the ReadyState
This state is responsible for handling the Hello phase of the gateway connection. See the Discord documentation for more information:
- https://discord.com/developers/docs/topics/gateway#connecting
- https://discord.com/developers/docs/topics/gateway#hello-event
- https://discord.com/developers/docs/topics/gateway#sending-heartbeats
- https://discord.com/developers/docs/topics/gateway#identifying
func (*HelloState) String ¶
func (st *HelloState) String() string
type Identify ¶
type Identify struct { BotToken string `json:"token"` Properties interface{} `json:"properties"` Compress bool `json:"compress,omitempty"` LargeThreshold uint8 `json:"large_threshold,omitempty"` Shard [2]int `json:"shard"` Presence interface{} `json:"presence"` Intents intent.Type `json:"intents"` }
type Logger ¶
type Logger interface { // Debug low level insight in system behavior to assist diagnostic. Debug(format string, args ...interface{}) // Info general information that might be interesting Info(format string, args ...interface{}) // Warn creeping technical debt, such as dependency updates will cause the system to not compile/break. Warn(format string, args ...interface{}) // Error recoverable events/issues that does not cause a system shutdown, but is also crucial and needs to be // dealt with quickly. Error(format string, args ...interface{}) // Panic identifies system crashing/breaking issues that forces the application to shut down or completely stop Panic(format string, args ...interface{}) }
Logger for logging different situations
type Option ¶
Option for initializing a new gateway client. An option must be deterministic regardless of when or how many times it is executed.
func WithBotToken ¶
func WithCommandRateLimiter ¶
func WithCommandRateLimiter(ratelimiter RateLimiter) Option
func WithDirectMessageEvents ¶
func WithEventHandler ¶
WithEventHandler provides a callback that is triggered on incoming events. Note that the allowlist will filter out events you have not requested.
Warning: this function call is blocking. You should not run heavy logic in the handler, preferably just forward it to a processing component. An example usage would be to send it to a buffered worker channel.
func WithExistingSession ¶
func WithGuildEvents ¶
func WithHeartbeatHandler ¶
func WithHeartbeatHandler(handler HeartbeatHandler) Option
WithHeartbeatHandler allows overwriting default heartbeat behavior. Basic behavior is achieved with the DefaultHeartbeatHandler:
NewClient( WithHeartbeatHandler(&DefaultHeartbeatHandler{ TextWriter: }) )
func WithIdentifyConnectionProperties ¶
func WithIdentifyConnectionProperties(properties *IdentifyConnectionProperties) Option
func WithIdentifyRateLimiter ¶
func WithIdentifyRateLimiter(ratelimiter RateLimiter) Option
func WithIntents ¶
func WithLogger ¶
func WithShardInfo ¶
type Payload ¶
type Payload struct { Op opcode.Type `json:"op"` Data encoding.RawMessage `json:"d"` Seq int64 `json:"s,omitempty"` EventName event.Type `json:"t,omitempty"` // CloseCode is a special case for this library. // You can specify an io.Reader which produces relevant closecode data // for correct handling of close frames // TODO: improve documentation CloseCode closecode.Type `json:"closecode,omitempty"` }
type ReadyState ¶
type ReadyState struct {
// contains filtered or unexported fields
}
ReadyState is responsibile for the Ready phase of the gateway connection. It's responsibilities are:
- Process incoming Ready event
- Cache relevant Discord session data
- Transition to the ConnectedState
See the Discord documentation for more information:
func (*ReadyState) String ¶
func (st *ReadyState) String() string
type ResumableClosedState ¶
type ResumableClosedState struct {
// contains filtered or unexported fields
}
func (*ResumableClosedState) Process ¶
func (st *ResumableClosedState) Process(payload *Payload, _ io.Writer) error
func (*ResumableClosedState) String ¶
func (st *ResumableClosedState) String() string
type ResumeState ¶
type ResumeState struct {
// contains filtered or unexported fields
}
ResumeState wraps a ConnectedState until a Resumed event is received from Discord...
func (*ResumeState) Process ¶
func (st *ResumeState) Process(payload *Payload, pipe io.Writer) error
func (*ResumeState) String ¶
func (st *ResumeState) String() string
type StateCloser ¶
StateCloser any state implementing a Close method may overwrite the default behavior of StateCtx.Close
type StateCtx ¶
type StateCtx struct { SessionID string ResumeGatewayURL string // contains filtered or unexported fields }