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 *gateway.Gateway)
- type Session
- func (s *Session) JoinChannel(gID discord.GuildID, cID discord.ChannelID, mute, deaf bool) error
- func (s *Session) JoinChannelCtx(ctx context.Context, gID discord.GuildID, cID discord.ChannelID, ...) error
- func (s *Session) Leave() error
- func (s *Session) LeaveCtx(ctx context.Context) error
- func (s *Session) ReadPacket() (*udp.Packet, error)
- func (s *Session) Speaking(flag voicegateway.SpeakingFlag) error
- func (s *Session) UseContext(ctx context.Context) error
- func (s *Session) VoiceUDPConn() *udp.Connection
- func (s *Session) Write(b []byte) (int, error)
- func (s *Session) WriteCtx(ctx context.Context, b []byte) (int, error)
Examples ¶
Constants ¶
const Protocol = "xsalsa20_poly1305"
Protocol is the encryption protocol that this library uses.
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.
var WSTimeout = 10 * 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.
Functions ¶
func AddIntents ¶
AddIntents adds the needed voice intents into gw. Bots should always call this before Open if voice is required.
Types ¶
type Session ¶
type Session struct { *handler.Handler ErrorLog func(err error) // contains filtered or unexported fields }
Session is a single voice session that wraps around the voice gateway and UDP connection.
Example ¶
package main import ( "io" "log" "github.com/diamondburned/arikawa/v2/discord" "github.com/diamondburned/arikawa/v2/state" "github.com/diamondburned/arikawa/v2/voice" ) var ( token string channelID discord.ChannelID ) // pseudo function for example func writeOpusInto(w io.Writer) {} func main() { s, err := state.New("Bot " + token) if err != nil { log.Fatalln("failed to make state:", err) } // This is required for bots. voice.AddIntents(s.Gateway) if err := s.Open(); err != nil { log.Fatalln("failed to open gateway:", err) } defer s.Close() c, err := s.Channel(channelID) if err != nil { log.Fatalln("failed to get channel:", err) } v, err := voice.NewSession(s) if err != nil { log.Fatalln("failed to create voice session:", err) } if err := v.JoinChannel(c.GuildID, c.ID, false, false); err != nil { log.Fatalln("failed to join voice channel:", err) } defer v.Leave() // Start writing Opus frames. for { writeOpusInto(v) } }
Output:
func NewSession ¶
NewSession creates a new voice session for the current user.
func NewSessionCustom ¶
NewSessionCustom creates a new voice session from the given session and user ID.
func (*Session) JoinChannel ¶
func (*Session) JoinChannelCtx ¶
func (s *Session) JoinChannelCtx( ctx context.Context, gID discord.GuildID, cID discord.ChannelID, mute, deaf bool) error
JoinChannelCtx joins a voice channel. Callers shouldn't use this method directly, but rather Voice's. This method shouldn't ever be called concurrently.
func (*Session) Leave ¶
Leave disconnects the current voice session from the currently connected channel.
func (*Session) LeaveCtx ¶
LeaveCtx disconencts with a context. Refer to Leave for more information.
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) Speaking ¶
func (s *Session) Speaking(flag voicegateway.SpeakingFlag) error
Speaking tells Discord we're speaking. This method should not be called concurrently.
func (*Session) UseContext ¶
UseContext tells the UDP voice connection to write with the given context.
func (*Session) VoiceUDPConn ¶
func (s *Session) VoiceUDPConn() *udp.Connection
VoiceUDPConn gets a voice UDP connection. The caller could use this method to circumvent the rapid mutex-read-lock acquire inside Write.
func (*Session) Write ¶
Write writes into the UDP voice connection WITHOUT a timeout. Refer to WriteCtx for more information.