Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,22 @@ func (g *gatewayImpl) open(ctx context.Context) error {
g.lastHeartbeatSent = time.Now().UTC()
conn, rs, err := g.config.Dialer.DialContext(ctx, gatewayURL, nil)
if err != nil {
body := ""
if rs != nil && rs.Body != nil {
var body []byte
if rs != nil {
defer func() {
_ = rs.Body.Close()
}()
rawBody, bErr := io.ReadAll(rs.Body)
if bErr != nil {
g.config.Logger.ErrorContext(ctx, "error while reading response body", slog.Any("err", bErr))
body, err = io.ReadAll(rs.Body)
if err != nil {
g.config.Logger.ErrorContext(ctx, "error while reading response body", slog.Any("err", err))
}
body = string(rawBody)
}

g.config.Logger.ErrorContext(ctx, "error connecting to the gateway", slog.Any("err", err), slog.String("url", gatewayURL), slog.String("body", body))
g.config.Logger.ErrorContext(ctx, "error connecting to the gateway",
slog.Any("err", err),
slog.String("url", gatewayURL),
slog.String("body", string(body)),
)
g.connMu.Unlock()
return err
}
Expand Down Expand Up @@ -303,7 +306,7 @@ func (g *gatewayImpl) Close(ctx context.Context) {

func (g *gatewayImpl) CloseWithCode(ctx context.Context, code int, message string) {
if g.heartbeatCancel != nil {
g.config.Logger.DebugContext(ctx, "closing heartbeat goroutines...")
g.config.Logger.DebugContext(ctx, "closing heartbeat goroutine")
g.heartbeatCancel()
}

Expand Down Expand Up @@ -382,8 +385,10 @@ func (g *gatewayImpl) Presence() *MessageDataPresenceUpdate {
}

func (g *gatewayImpl) doReconnect(ctx context.Context) error {
try := 0
backoffIncrement := 0
var (
try int
backoffIncrement int
)

for {
// Exponentially backoff up to a limit of 10s
Expand Down
10 changes: 10 additions & 0 deletions gateway/gateway_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (e *Message) UnmarshalJSON(data []byte) error {
messageData = d

case OpcodeReconnect:
messageData = MessageDataReconnect{}

case OpcodeRequestGuildMembers:
var d MessageDataRequestGuildMembers
Expand All @@ -82,6 +83,7 @@ func (e *Message) UnmarshalJSON(data []byte) error {
messageData = d

case OpcodeHeartbeatACK:
messageData = MessageDataHeartbeatACK{}

case OpcodeRequestSoundboardSounds:
var d MessageDataRequestSoundboardSounds
Expand Down Expand Up @@ -658,6 +660,10 @@ type MessageDataResume struct {

func (MessageDataResume) messageData() {}

type MessageDataReconnect struct{}

func (MessageDataReconnect) messageData() {}

// MessageDataRequestGuildMembers is used for fetching all the members of a guild_events. It is recommended you have a strict
// member caching policy when using this.
type MessageDataRequestGuildMembers struct {
Expand All @@ -681,6 +687,10 @@ type MessageDataHello struct {

func (MessageDataHello) messageData() {}

type MessageDataHeartbeatACK struct{}

func (MessageDataHeartbeatACK) messageData() {}

type MessageDataRequestSoundboardSounds struct {
GuildIDs []snowflake.ID `json:"guild_ids"`
}
Expand Down
4 changes: 2 additions & 2 deletions voice/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (c *connImpl) HandleVoiceServerUpdate(update botgateway.EventVoiceServerUpd
}()
}

func (c *connImpl) handleMessage(gateway Gateway, op Opcode, data GatewayMessageData) {
func (c *connImpl) handleMessage(gateway Gateway, op Opcode, sequenceNumber int, data GatewayMessageData) {
switch d := data.(type) {
case GatewayMessageDataReady:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
Expand Down Expand Up @@ -238,7 +238,7 @@ func (c *connImpl) handleMessage(gateway Gateway, op Opcode, data GatewayMessage
}
}
if c.config.EventHandlerFunc != nil {
c.config.EventHandlerFunc(c.gateway, op, data)
c.config.EventHandlerFunc(gateway, op, sequenceNumber, data)
}
}

Expand Down
Loading
Loading