-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_ready.go
49 lines (40 loc) · 1.22 KB
/
state_ready.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package gateway
import (
"errors"
"fmt"
"github.com/discordpkg/gateway/encoding"
"io"
"github.com/discordpkg/gateway/event/opcode"
)
type Ready struct {
SessionID string `json:"session_id"`
ResumeGatewayURL string `json:"resume_gateway_url"`
}
// ReadyState is responsibile for the Ready phase of the gateway connection. It's responsibilities are:
// 1. Process incoming Ready event
// 2. Cache relevant Discord session data
// 4. Transition to the ConnectedState
//
// See the Discord documentation for more information:
// - https://discord.com/developers/docs/topics/gateway#ready-event
type ReadyState struct {
ctx *StateCtx
}
func (st *ReadyState) String() string {
return "ready"
}
func (st *ReadyState) Process(payload *Payload, _ io.Writer) error {
if payload.Op != opcode.Dispatch {
st.ctx.SetState(&ClosedState{})
return errors.New(fmt.Sprintf("incorrect opcode: %d, wants %d", int(payload.Op), int(opcode.Dispatch)))
}
var ready Ready
if err := encoding.Unmarshal(payload.Data, &ready); err != nil {
st.ctx.SetState(&ClosedState{})
return err
}
st.ctx.SessionID = ready.SessionID
st.ctx.ResumeGatewayURL = ready.ResumeGatewayURL
st.ctx.SetState(&ConnectedState{ctx: st.ctx})
return nil
}