/
types.go
125 lines (105 loc) · 3.04 KB
/
types.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package voice
import (
"github.com/BOOMfinity/bfcord/gateway"
"github.com/andersfylling/snowflake/v5"
"github.com/segmentio/encoding/json"
"github.com/unxcepted/websocket"
)
/*
Own eventChannel codes:
101 - connection finished - sent when session description OR resumed is received
111 - shutdown - shutdowns heartbeat loop and websocket connection
*/
type ConnectOptions struct {
VoiceState *gateway.VoiceStateUpdateEvent
VoiceServer *gateway.VoiceServerUpdateEvent
ChannelID snowflake.ID
GuildID snowflake.ID
Debug bool
OnClose func()
}
type SpeakingFlag uint64
const (
NotSpeaking SpeakingFlag = 0
Microphone SpeakingFlag = 1 << iota
SoundShare
Priority
)
type OPCode uint8
const (
identifyOP OPCode = iota
selectProtocolOP
readyOP
heartbeatOP
sessionDescriptionOP
speakingOP
heartbeatAckOP
resumeOP
helloOP
resumedOP
clientDisconnectOP
)
const ( // known close codes
// Discord normal
SessionNoLongerValid websocket.StatusCode = 4006
SessionTimeout websocket.StatusCode = 4009
Disconnected websocket.StatusCode = 4014
ServerCrashed websocket.StatusCode = 4015
// Discord abnormal
UnknownOPCode websocket.StatusCode = 4001
FailedToDecodePayload websocket.StatusCode = 4002
NotAuthenticated websocket.StatusCode = 4003
AuthenticationFailed websocket.StatusCode = 4004
AlreadyAuthenticated websocket.StatusCode = 4005
ServerNotFound websocket.StatusCode = 4011
UnknownProtocol websocket.StatusCode = 4012
UnknownEncryption websocket.StatusCode = 4016
BfcordVoiceReconnect websocket.StatusCode = 4969
)
type Payload struct {
Data json.RawMessage `json:"d"`
Op OPCode `json:"op"`
}
func payloadTo[V any](p *Payload) (res V, err error) {
err = json.Unmarshal(p.Data, &res)
return
}
type IdentifyPayload struct {
SessionID string `json:"session_id"`
Token string `json:"token"`
ServerID snowflake.ID `json:"server_id"`
UserID snowflake.ID `json:"user_id"`
}
type ReadyPayload struct {
IP string `json:"IP"`
Modes []string `json:"modes"`
SSRC uint32 `json:"ssrc"`
Port uint16 `json:"port"`
// heartbeat_interval here is an erroneous field and should be ignored. The correct heartbeat_interval value comes from the Hello payload.
}
type helloPayload struct {
HeartbeatInterval float32 `json:"heartbeat_interval"`
}
type selectProtocolPayload struct {
Protocol string `json:"protocol"` // always UDP
Data selectProtocolData `json:"data"`
}
type selectProtocolData struct {
Address string `json:"address"`
EncryptionMode string `json:"mode"`
Port uint16 `json:"port"`
}
type sessionDescriptionPayload struct {
EncryptionMode string `json:"mode"`
SecretKey [32]byte `json:"secret_key"`
}
type speakingPayload struct {
Speaking SpeakingFlag `json:"speaking"`
Delay int `json:"delay"`
SSRC uint32 `json:"ssrc"`
}
type resumePayload struct {
SessionID string `json:"session_id"`
Token string `json:"token"`
ServerID snowflake.ID `json:"server_id"`
}