Documentation ¶
Overview ¶
Package voice handles the Discord voice gateway and UDP connections. It does not handle book-keeping of those sessions.
This package abstracts the subpackage voice/voicesession and voice/udp.
Index ¶
- Constants
- Variables
- func AddIntents(gw interface{ ... })
- type MainSession
- type ReconnectError
- type Session
- func (s *Session) JoinChannel(ctx context.Context, chID discord.ChannelID, mute, deaf bool) error
- func (s *Session) JoinChannelAndSpeak(ctx context.Context, chID discord.ChannelID, mute, deaf bool) error
- func (s *Session) Leave(ctx context.Context) error
- func (s *Session) ReadPacket() (*udp.Packet, error)
- func (s *Session) SetUDPDialer(d udp.DialFunc)
- func (s *Session) Speaking(ctx context.Context, flag voicegateway.SpeakingFlag) error
- func (s *Session) Write(b []byte) (int, error)
Examples ¶
Constants ¶
const Intents = gateway.IntentGuilds | gateway.IntentGuildVoiceStates
Intents are the intents needed for voice to work properly.
const Protocol = "xsalsa20_poly1305"
Protocol is the encryption protocol that this library uses.
const WSTimeout = 25 * time.Second
WSTimeout is the duration to wait for a gateway operation including Session to complete before erroring out. This only applies to functions that don't take in a context already.
Variables ¶
var ErrAlreadyConnecting = errors.New("already connecting")
ErrAlreadyConnecting is returned when the session is already connecting.
var ErrCannotSend = errors.New("cannot send audio to closed channel")
ErrCannotSend is an error when audio is sent to a closed channel.
Functions ¶
func AddIntents ¶
AddIntents adds the needed voice intents into gw. Bots should always call this before Open if voice is required.
Types ¶
type MainSession ¶
type MainSession interface { // AddHandler describes the method in handler.Handler. AddHandler(handler interface{}) (rm func()) // Me returns the current user. Me() (*discord.User, error) // Channel queries for the channel with the given ID. Channel(discord.ChannelID) (*discord.Channel, error) // SendGateway is a helper to send messages over the gateway. SendGateway(ctx context.Context, m ws.Event) error }
MainSession abstracts both session.Session and state.State.
type ReconnectError ¶
type ReconnectError struct {
Err error
}
ReconnectError is emitted into Session.Handler everytime the voice gateway fails to be reconnected. It implements the error interface.
type Session ¶
type Session struct { *handler.Handler WSTimeout time.Duration // global WSTimeout WSMaxRetry int // 2 WSRetryDelay time.Duration // 2s WSWaitDuration time.Duration // 5s // contains filtered or unexported fields }
Session is a single voice session that wraps around the voice gateway and UDP connection.
Example ¶
package main import ( "context" "log" "os" "os/signal" "github.com/diamondburned/arikawa/v3/discord" "github.com/diamondburned/arikawa/v3/state" "github.com/diamondburned/arikawa/v3/voice" "github.com/diamondburned/arikawa/v3/voice/testdata" ) var ( token string channelID discord.ChannelID ) func main() { ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) defer cancel() s := state.New("Bot " + token) // This is required for bots. voice.AddIntents(s) if err := s.Open(ctx); err != nil { log.Fatalln("failed to open gateway:", err) } defer s.Close() v, err := voice.NewSession(s) if err != nil { log.Fatalln("failed to create voice session:", err) } if err := v.JoinChannelAndSpeak(ctx, channelID, false, false); err != nil { log.Fatalln("failed to join voice channel:", err) } defer v.Leave(ctx) if err := testdata.WriteOpus(v, "testdata/nico.dca"); err != nil { log.Fatalln("failed to write opus:", err) } }
Output:
func NewSession ¶
func NewSession(state MainSession) (*Session, error)
NewSession creates a new voice session for the current user.
func NewSessionCustom ¶
func NewSessionCustom(ses MainSession, userID discord.UserID) *Session
NewSessionCustom creates a new voice session from the given session and user ID.
func (*Session) JoinChannel ¶
JoinChannel joins the given voice channel with the default timeout.
func (*Session) JoinChannelAndSpeak ¶
func (s *Session) JoinChannelAndSpeak(ctx context.Context, chID discord.ChannelID, mute, deaf bool) error
JoinChannelAndSpeak is a convenient function that calls JoinChannel then Speaking.
func (*Session) Leave ¶
Leave disconnects the current voice session from the currently connected channel.
func (*Session) ReadPacket ¶
ReadPacket reads a single packet from the UDP connection. This is NOT at all thread safe, and must be used very carefully. The backing buffer is always reused.
func (*Session) SetUDPDialer ¶
SetUDPDialer sets the given dialer to be used for dialing UDP voice connections.
func (*Session) Speaking ¶
func (s *Session) Speaking(ctx context.Context, flag voicegateway.SpeakingFlag) error
Speaking tells Discord we're speaking. This method should not be called concurrently.
If only NotSpeaking (0) is given, then even if the gateway cannot be reached, a nil error will be returned. This is because sending Discord a not-speaking event is a destruction command that doesn't affect the outcome of anything done after whatsoever.