Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Voice overhaul #1593

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 19 additions & 17 deletions discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,30 @@ const VERSION = "0.28.1"

// New creates a new Discord session with provided token.
// If the token is for a bot, it must be prefixed with "Bot "
// e.g. "Bot ..."
//
// e.g. "Bot ..."
//
// Or if it is an OAuth2 token, it must be prefixed with "Bearer "
// e.g. "Bearer ..."
//
// e.g. "Bearer ..."
func New(token string) (s *Session, err error) {

// Create an empty Session interface.
s = &Session{
State: NewState(),
Ratelimiter: NewRatelimiter(),
StateEnabled: true,
Compress: true,
ShouldReconnectOnError: true,
ShouldReconnectVoiceOnSessionError: true,
ShouldRetryOnRateLimit: true,
ShardID: 0,
ShardCount: 1,
MaxRestRetries: 3,
Client: &http.Client{Timeout: (20 * time.Second)},
Dialer: websocket.DefaultDialer,
UserAgent: "DiscordBot (https://github.com/bwmarrin/discordgo, v" + VERSION + ")",
sequence: new(int64),
LastHeartbeatAck: time.Now().UTC(),
State: NewState(),
Ratelimiter: NewRatelimiter(),
StateEnabled: true,
Compress: true,
ShouldReconnectOnError: true,
ShouldRetryOnRateLimit: true,
ShardID: 0,
ShardCount: 1,
MaxRestRetries: 3,
Client: &http.Client{Timeout: (20 * time.Second)},
Dialer: websocket.DefaultDialer,
UserAgent: "DiscordBot (https://github.com/bwmarrin/discordgo, v" + VERSION + ")",
sequence: new(int64),
LastHeartbeatAck: time.Now().UTC(),
}

// Initialize the Identify Package with defaults
Expand Down
6 changes: 3 additions & 3 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ type UserUpdate struct {

// VoiceServerUpdate is the data for a VoiceServerUpdate event.
type VoiceServerUpdate struct {
Token string `json:"token"`
GuildID string `json:"guild_id"`
Endpoint string `json:"endpoint"`
Token string `json:"token"`
GuildID string `json:"guild_id"`
Endpoint *string `json:"endpoint"`
}

// VoiceStateUpdate is the data for a VoiceStateUpdate event.
Expand Down
9 changes: 7 additions & 2 deletions examples/airhorn/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/binary"
"flag"
"fmt"
Expand Down Expand Up @@ -186,7 +187,9 @@ func loadSound() error {
func playSound(s *discordgo.Session, guildID, channelID string) (err error) {

// Join the provided voice channel.
vc, err := s.ChannelVoiceJoin(guildID, channelID, false, true)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
vc, err := s.ChannelVoiceJoin(ctx, guildID, channelID, false, false)
cancel()
if err != nil {
return err
}
Expand All @@ -209,7 +212,9 @@ func playSound(s *discordgo.Session, guildID, channelID string) (err error) {
time.Sleep(250 * time.Millisecond)

// Disconnect from the provided voice channel.
vc.Disconnect()
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
vc.Disconnect(ctx)
cancel()

return nil
}
12 changes: 7 additions & 5 deletions examples/voice_receive/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"time"
Expand Down Expand Up @@ -30,7 +31,7 @@ func createPionRTPPacket(p *discordgo.Packet) *rtp.Packet {
Header: rtp.Header{
Version: 2,
// Taken from Discord voice docs
PayloadType: 0x78,
PayloadType: p.PayloadType,
SequenceNumber: p.Sequence,
Timestamp: p.Timestamp,
SSRC: p.SSRC,
Expand All @@ -39,7 +40,7 @@ func createPionRTPPacket(p *discordgo.Packet) *rtp.Packet {
}
}

func handleVoice(c chan *discordgo.Packet) {
func handleVoice(c <-chan *discordgo.Packet) {
files := make(map[uint32]media.Writer)
for p := range c {
file, ok := files[p.SSRC]
Expand Down Expand Up @@ -83,17 +84,18 @@ func main() {
return
}

v, err := s.ChannelVoiceJoin(GuildID, ChannelID, true, false)
v, err := s.ChannelVoiceJoin(context.Background(), GuildID, ChannelID, true, false)
if err != nil {
fmt.Println("failed to join voice channel:", err)
return
}

go func() {
time.Sleep(10 * time.Second)
close(v.OpusRecv)
v.Close()
v.Disconnect(context.Background())
}()

handleVoice(v.OpusRecv)

fmt.Println("exiting...")
}
3 changes: 0 additions & 3 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ type Session struct {
// Should the session reconnect the websocket on errors.
ShouldReconnectOnError bool

// Should voice connections reconnect on a session reconnect.
ShouldReconnectVoiceOnSessionError bool

// Should the session retry requests when rate limited.
ShouldRetryOnRateLimit bool

Expand Down
Loading
Loading