2020-04-25 03:29:14 +00:00
|
|
|
// Package voice handles the Discord voice gateway and UDP connections, as well
|
|
|
|
// as managing and keeping track of multiple voice sessions.
|
|
|
|
//
|
|
|
|
// This package abstracts the subpackage voice/voicesession and voice/udp.
|
2020-04-20 01:21:10 +00:00
|
|
|
package voice
|
|
|
|
|
|
|
|
import (
|
2020-10-22 17:47:27 +00:00
|
|
|
"context"
|
2020-04-20 01:21:10 +00:00
|
|
|
"log"
|
2020-05-13 20:43:00 +00:00
|
|
|
"strconv"
|
2020-04-20 01:21:10 +00:00
|
|
|
"sync"
|
2020-10-22 17:47:27 +00:00
|
|
|
"time"
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-10-28 22:39:59 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v2/gateway"
|
|
|
|
"github.com/diamondburned/arikawa/v2/state"
|
2020-04-20 01:21:10 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// defaultErrorHandler is the default error handler
|
2020-05-16 21:14:49 +00:00
|
|
|
defaultErrorHandler = func(err error) { log.Println("voice gateway error:", err) }
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-20 17:52:07 +00:00
|
|
|
// ErrCannotSend is an error when audio is sent to a closed channel.
|
|
|
|
ErrCannotSend = errors.New("cannot send audio to closed channel")
|
2020-04-20 01:21:10 +00:00
|
|
|
)
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Voice represents a Voice Repository used for managing voice sessions.
|
2020-04-20 01:21:10 +00:00
|
|
|
type Voice struct {
|
2020-04-25 02:36:33 +00:00
|
|
|
*state.State
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Session holds all of the active voice sessions.
|
|
|
|
mapmutex sync.Mutex
|
2020-07-21 20:27:59 +00:00
|
|
|
sessions map[discord.GuildID]*Session
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
// Callbacks to remove the handlers.
|
|
|
|
closers []func()
|
|
|
|
|
2020-04-20 01:21:10 +00:00
|
|
|
// ErrorLog will be called when an error occurs (defaults to log.Println)
|
|
|
|
ErrorLog func(err error)
|
|
|
|
}
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
// NewVoiceFromToken creates a new voice session from the given token.
|
|
|
|
func NewVoiceFromToken(token string) (*Voice, error) {
|
|
|
|
s, err := state.New(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create a new session")
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewVoice(s), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewVoice creates a new Voice repository wrapped around a state. The function
|
|
|
|
// will also automatically add the GuildVoiceStates intent, as that is required.
|
2020-04-20 01:21:10 +00:00
|
|
|
func NewVoice(s *state.State) *Voice {
|
|
|
|
v := &Voice{
|
2020-04-25 02:36:33 +00:00
|
|
|
State: s,
|
2020-07-21 20:27:59 +00:00
|
|
|
sessions: make(map[discord.GuildID]*Session),
|
2020-04-20 01:21:10 +00:00
|
|
|
ErrorLog: defaultErrorHandler,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the required event handlers to the session.
|
2020-07-11 19:50:32 +00:00
|
|
|
v.closers = []func(){
|
|
|
|
s.AddHandler(v.onVoiceStateUpdate),
|
|
|
|
s.AddHandler(v.onVoiceServerUpdate),
|
|
|
|
}
|
2020-04-20 01:21:10 +00:00
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// onVoiceStateUpdate receives VoiceStateUpdateEvents from the gateway
|
|
|
|
// to keep track of the current user's voice state.
|
|
|
|
func (v *Voice) onVoiceStateUpdate(e *gateway.VoiceStateUpdateEvent) {
|
|
|
|
// Get the current user.
|
|
|
|
me, err := v.Me()
|
|
|
|
if err != nil {
|
|
|
|
v.ErrorLog(err)
|
|
|
|
return
|
|
|
|
}
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Ignore the event if it is an update from another user.
|
|
|
|
if me.ID != e.UserID {
|
|
|
|
return
|
|
|
|
}
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Get the stored voice session for the given guild.
|
|
|
|
vs, ok := v.GetSession(e.GuildID)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Do what we must.
|
|
|
|
vs.UpdateState(e)
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Remove the connection if the current user has disconnected.
|
|
|
|
if e.ChannelID == 0 {
|
|
|
|
v.RemoveSession(e.GuildID)
|
|
|
|
}
|
|
|
|
}
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// onVoiceServerUpdate receives VoiceServerUpdateEvents from the gateway
|
|
|
|
// to manage the current user's voice connections.
|
|
|
|
func (v *Voice) onVoiceServerUpdate(e *gateway.VoiceServerUpdateEvent) {
|
|
|
|
// Get the stored voice session for the given guild.
|
|
|
|
vs, ok := v.GetSession(e.GuildID)
|
2020-04-20 01:21:10 +00:00
|
|
|
if !ok {
|
2020-04-25 02:36:33 +00:00
|
|
|
return
|
2020-04-20 01:21:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Do what we must.
|
|
|
|
vs.UpdateServer(e)
|
|
|
|
}
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// GetSession gets a session for a guild with a read lock.
|
2020-07-21 20:27:59 +00:00
|
|
|
func (v *Voice) GetSession(guildID discord.GuildID) (*Session, bool) {
|
2020-04-25 02:36:33 +00:00
|
|
|
v.mapmutex.Lock()
|
|
|
|
defer v.mapmutex.Unlock()
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// For some reason you cannot just put `return v.sessions[]` and return a bool D:
|
|
|
|
conn, ok := v.sessions[guildID]
|
|
|
|
return conn, ok
|
|
|
|
}
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// RemoveSession removes a session.
|
2020-07-21 20:27:59 +00:00
|
|
|
func (v *Voice) RemoveSession(guildID discord.GuildID) {
|
2020-04-25 02:36:33 +00:00
|
|
|
v.mapmutex.Lock()
|
|
|
|
defer v.mapmutex.Unlock()
|
|
|
|
|
|
|
|
// Ensure that the session is disconnected.
|
|
|
|
if ses, ok := v.sessions[guildID]; ok {
|
|
|
|
ses.Disconnect()
|
2020-04-20 01:21:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
delete(v.sessions, guildID)
|
|
|
|
}
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// JoinChannel joins the specified channel in the specified guild.
|
2020-07-21 20:27:59 +00:00
|
|
|
func (v *Voice) JoinChannel(gID discord.GuildID, cID discord.ChannelID, muted, deafened bool) (*Session, error) {
|
2020-04-25 02:36:33 +00:00
|
|
|
// Get the stored voice session for the given guild.
|
|
|
|
conn, ok := v.GetSession(gID)
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Create a new voice session if one does not exist.
|
|
|
|
if !ok {
|
|
|
|
u, err := v.Me()
|
|
|
|
if err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to get self")
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
conn = NewSession(v.Session, u.ID)
|
2020-07-11 19:50:32 +00:00
|
|
|
conn.ErrorLog = v.ErrorLog
|
2020-04-25 02:36:33 +00:00
|
|
|
|
|
|
|
v.mapmutex.Lock()
|
|
|
|
v.sessions[gID] = conn
|
|
|
|
v.mapmutex.Unlock()
|
|
|
|
}
|
2020-04-20 01:21:10 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Connect.
|
|
|
|
return conn, conn.JoinChannel(gID, cID, muted, deafened)
|
2020-04-20 01:21:10 +00:00
|
|
|
}
|
2020-05-13 20:43:00 +00:00
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
func (v *Voice) Close() error {
|
|
|
|
err := &CloseError{
|
2020-07-21 20:27:59 +00:00
|
|
|
SessionErrors: make(map[discord.GuildID]error),
|
2020-07-11 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v.mapmutex.Lock()
|
|
|
|
defer v.mapmutex.Unlock()
|
|
|
|
|
|
|
|
// Remove all callback handlers.
|
|
|
|
for _, fn := range v.closers {
|
|
|
|
fn()
|
|
|
|
}
|
|
|
|
|
|
|
|
for gID, s := range v.sessions {
|
2020-10-22 17:47:27 +00:00
|
|
|
log.Println("closing", gID)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
|
|
|
if dErr := s.DisconnectCtx(ctx); dErr != nil {
|
2020-07-11 19:50:32 +00:00
|
|
|
err.SessionErrors[gID] = dErr
|
|
|
|
}
|
2020-10-22 17:47:27 +00:00
|
|
|
cancel()
|
|
|
|
log.Println("closed", gID)
|
2020-07-11 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err.StateErr = v.State.Close()
|
|
|
|
if err.HasError() {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:43:00 +00:00
|
|
|
type CloseError struct {
|
2020-07-21 20:27:59 +00:00
|
|
|
SessionErrors map[discord.GuildID]error
|
2020-05-13 20:43:00 +00:00
|
|
|
StateErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *CloseError) HasError() bool {
|
|
|
|
if e.StateErr != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-05-13 21:38:07 +00:00
|
|
|
return len(e.SessionErrors) > 0
|
2020-05-13 20:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *CloseError) Error() string {
|
|
|
|
if e.StateErr != nil {
|
|
|
|
return e.StateErr.Error()
|
|
|
|
}
|
|
|
|
|
2020-05-13 21:38:07 +00:00
|
|
|
if len(e.SessionErrors) < 1 {
|
2020-05-13 20:43:00 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-05-13 21:38:07 +00:00
|
|
|
return strconv.Itoa(len(e.SessionErrors)) + " voice sessions returned errors while attempting to disconnect"
|
2020-05-13 20:43:00 +00:00
|
|
|
}
|