2020-04-25 02:36:33 +00:00
|
|
|
//
|
|
|
|
// For the brave souls who get this far: You are the chosen ones,
|
|
|
|
// the valiant knights of programming who toil away, without rest,
|
|
|
|
// fixing our most awful code. To you, true saviors, kings of men,
|
|
|
|
// I say this: never gonna give you up, never gonna let you down,
|
|
|
|
// never gonna run around and desert you. Never gonna make you cry,
|
|
|
|
// never gonna say goodbye. Never gonna tell a lie and hurt you.
|
|
|
|
//
|
|
|
|
|
|
|
|
package voicegateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-07-29 20:10:48 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
2021-09-28 20:19:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/ws"
|
2020-04-25 02:36:33 +00:00
|
|
|
)
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
// Version represents the current version of the Discord Gateway Gateway this package uses.
|
|
|
|
const Version = "4"
|
2020-04-25 02:36:33 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNoSessionID = errors.New("no sessionID was received")
|
|
|
|
ErrNoEndpoint = errors.New("no endpoint was received")
|
|
|
|
)
|
|
|
|
|
|
|
|
// State contains state information of a voice gateway.
|
|
|
|
type State struct {
|
2021-09-28 20:19:04 +00:00
|
|
|
UserID discord.UserID // constant
|
|
|
|
GuildID discord.GuildID // constant
|
2020-07-21 20:27:59 +00:00
|
|
|
ChannelID discord.ChannelID
|
2020-04-25 02:36:33 +00:00
|
|
|
|
|
|
|
SessionID string
|
|
|
|
Token string
|
|
|
|
Endpoint string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gateway represents a Discord Gateway Gateway connection.
|
|
|
|
type Gateway struct {
|
2021-09-28 20:19:04 +00:00
|
|
|
gateway *ws.Gateway
|
|
|
|
state State // constant
|
2020-04-25 02:36:33 +00:00
|
|
|
|
|
|
|
mutex sync.RWMutex
|
2021-09-28 20:19:04 +00:00
|
|
|
ready *ReadyEvent
|
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// DefaultGatewayOpts contains the default options to be used for connecting to
|
|
|
|
// the gateway.
|
|
|
|
var DefaultGatewayOpts = ws.GatewayOpts{
|
|
|
|
ReconnectDelay: func(try int) time.Duration {
|
|
|
|
// minimum 4 seconds
|
|
|
|
return time.Duration(4+(2*try)) * time.Second
|
|
|
|
},
|
|
|
|
// FatalCloseCodes contains the default gateway close codes that will cause
|
|
|
|
// the gateway to exit. In other words, it's a list of unrecoverable close
|
|
|
|
// codes.
|
|
|
|
FatalCloseCodes: []int{
|
|
|
|
4003, // not authenticated
|
|
|
|
4004, // authentication failed
|
|
|
|
4006, // session invalid
|
|
|
|
4009, // session timed out
|
|
|
|
4011, // server not found
|
|
|
|
4012, // unknown protocol
|
|
|
|
4014, // disconnected
|
|
|
|
4016, // unknown encryption mode
|
|
|
|
},
|
|
|
|
DialTimeout: 0,
|
|
|
|
ReconnectAttempt: 0,
|
|
|
|
AlwaysCloseGracefully: true,
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// New creates a new voice gateway.
|
2020-04-25 02:36:33 +00:00
|
|
|
func New(state State) *Gateway {
|
2020-11-03 17:16:42 +00:00
|
|
|
// https://discord.com/developers/docs/topics/voice-connections#establishing-a-voice-websocket-connection
|
2021-07-02 09:42:00 +00:00
|
|
|
endpoint := "wss://" + strings.TrimSuffix(state.Endpoint, ":80") + "/?v=" + Version
|
2020-10-29 18:32:30 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
gw := ws.NewGateway(
|
|
|
|
ws.NewWebsocket(ws.NewCodec(OpUnmarshalers), endpoint),
|
|
|
|
&DefaultGatewayOpts,
|
|
|
|
)
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
return &Gateway{
|
2021-09-28 20:19:04 +00:00
|
|
|
gateway: gw,
|
|
|
|
state: state,
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// Ready returns the ready event.
|
|
|
|
func (g *Gateway) Ready() *ReadyEvent {
|
|
|
|
g.mutex.RLock()
|
|
|
|
defer g.mutex.RUnlock()
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
return g.ready
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// LastError returns the last error that the gateway has received. It only
|
|
|
|
// returns a valid error if the gateway's event loop as exited. If the event
|
|
|
|
// loop hasn't been started AND stopped, the function will panic.
|
|
|
|
func (g *Gateway) LastError() error {
|
|
|
|
return g.gateway.LastError()
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// Send is a function to send an Op payload to the Gateway.
|
|
|
|
func (g *Gateway) Send(ctx context.Context, data ws.Event) error {
|
|
|
|
return g.gateway.Send(ctx, data)
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// Speaking sends a Speaking operation (opcode 5) to the Gateway Gateway.
|
|
|
|
func (g *Gateway) Speaking(ctx context.Context, flag SpeakingFlag) error {
|
|
|
|
g.mutex.RLock()
|
2021-07-02 09:42:00 +00:00
|
|
|
ready := g.ready
|
2021-09-28 20:19:04 +00:00
|
|
|
g.mutex.RUnlock()
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
if ready == nil {
|
|
|
|
return errors.New("Speaking called before gateway was ready")
|
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
return g.gateway.Send(ctx, &SpeakingEvent{
|
|
|
|
Speaking: flag,
|
|
|
|
Delay: 0,
|
2021-07-02 09:42:00 +00:00
|
|
|
SSRC: ready.SSRC,
|
2020-04-25 02:36:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
func (g *Gateway) Connect(ctx context.Context) <-chan ws.Op {
|
|
|
|
return g.gateway.Connect(ctx, (*gatewayImpl)(g))
|
2020-07-15 23:39:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
var (
|
|
|
|
// ErrMissingForIdentify is an error when we are missing information to
|
|
|
|
// identify.
|
|
|
|
ErrMissingForIdentify = errors.New("missing GuildID, UserID, SessionID, or Token for identify")
|
|
|
|
// ErrMissingForResume is an error when we are missing information to
|
|
|
|
// resume.
|
|
|
|
ErrMissingForResume = errors.New("missing GuildID, SessionID, or Token for resuming")
|
|
|
|
)
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
type gatewayImpl Gateway
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
func (g *gatewayImpl) sendIdentify(ctx context.Context) error {
|
|
|
|
id := IdentifyCommand{
|
|
|
|
GuildID: g.state.GuildID,
|
|
|
|
UserID: g.state.UserID,
|
|
|
|
SessionID: g.state.SessionID,
|
|
|
|
Token: g.state.Token,
|
|
|
|
}
|
|
|
|
if !id.GuildID.IsValid() || id == (IdentifyCommand{}) {
|
|
|
|
return ErrMissingForIdentify
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
return g.gateway.Send(ctx, &id)
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
func (g *gatewayImpl) sendResume(ctx context.Context) error {
|
|
|
|
if !g.state.GuildID.IsValid() || g.state.SessionID == "" || g.state.Token == "" {
|
|
|
|
return ErrMissingForResume
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
return g.gateway.Send(ctx, &ResumeCommand{
|
|
|
|
GuildID: g.state.GuildID,
|
|
|
|
SessionID: g.state.SessionID,
|
|
|
|
Token: g.state.Token,
|
|
|
|
})
|
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
func (g *gatewayImpl) OnOp(ctx context.Context, op ws.Op) bool {
|
|
|
|
switch data := op.Data.(type) {
|
|
|
|
case *HelloEvent:
|
|
|
|
g.gateway.ResetHeartbeat(data.HeartbeatInterval.Duration())
|
|
|
|
|
|
|
|
// Send Discord either the Identify packet (if it's a fresh
|
|
|
|
// connection), or a Resume packet (if it's a dead connection).
|
|
|
|
if g.ready == nil {
|
|
|
|
// SessionID is empty, so this is a completely new session.
|
|
|
|
if err := g.sendIdentify(ctx); err != nil {
|
|
|
|
g.gateway.SendErrorWrap(err, "failed to send identify")
|
|
|
|
g.gateway.QueueReconnect()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := g.sendResume(ctx); err != nil {
|
|
|
|
g.gateway.SendErrorWrap(err, "failed to send resume")
|
|
|
|
g.gateway.QueueReconnect()
|
|
|
|
}
|
2020-07-11 19:50:32 +00:00
|
|
|
}
|
2021-09-28 20:19:04 +00:00
|
|
|
case *ReadyEvent:
|
|
|
|
g.mutex.Lock()
|
|
|
|
g.ready = data
|
|
|
|
g.mutex.Unlock()
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
return true
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
func (g *gatewayImpl) SendHeartbeat(ctx context.Context) {
|
|
|
|
heartbeat := HeartbeatCommand(time.Now().UnixNano())
|
|
|
|
if err := g.gateway.Send(ctx, &heartbeat); err != nil {
|
|
|
|
g.gateway.SendErrorWrap(err, "heartbeat error")
|
|
|
|
g.gateway.QueueReconnect()
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
2021-09-28 20:19:04 +00:00
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
func (g *gatewayImpl) Close() error {
|
|
|
|
return nil
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|