internal Task OnTrackExceptionAsync(TrackExceptionEventArgs eventArgs)
 => TrackException.InvokeAsync(this, eventArgs);
Exemplo n.º 2
0
 /// <summary>
 ///     Invokes the <see cref="TrackException"/> event asynchronously. (Can be override for
 ///     event catching)
 /// </summary>
 /// <param name="eventArgs">the event arguments</param>
 /// <returns>a task that represents the asynchronous operation</returns>
 protected virtual Task OnTrackExceptionAsync(TrackExceptionEventArgs eventArgs)
 => TrackException.InvokeAsync(this, eventArgs);
Exemplo n.º 3
0
        /// <summary>
        /// Starts the lavalink connection
        /// </summary>
        /// <returns></returns>
        public Task StartAsync()
        {
            return(Task.Run(() =>
            {
                if (baseDiscordClient.CurrentUser == null)
                {
                    throw new InvalidOperationException("Can't connect when CurrentUser is null. Please wait until Discord connects.");
                }

                lavalinkCancellation = new CancellationTokenSource();

                // Setup the lavalink websocket connection
                webSocket = new LavalinkWebSocket(this, config);

                webSocket.OnReceive += (message) =>
                {
                    // TODO: Implement stats event
                    switch ((string)message["op"])
                    {
                    case "playerUpdate":
                        {
                            logger.Log("Received Dispatch (PLAYER_UPDATE)", LogSeverity.Debug);

                            ulong guildId = (ulong)message["guildId"];

                            if (players.TryGetValue(guildId, out LavalinkPlayer player))
                            {
                                LavalinkTrack currentTrack = player.CurrentTrack;

                                player.FireEvent(Event.PlayerUpdate, message["state"]["position"]);
                                PlayerUpdate?.InvokeAsync(player, currentTrack, (long)message["state"]["position"]);
                            }

                            break;
                        }

                    case "event":
                        {
                            ulong guildId = (ulong)message["guildId"];

                            if (players.TryGetValue(guildId, out LavalinkPlayer player))
                            {
                                LavalinkTrack currentTrack = player.CurrentTrack;

                                switch ((string)message["type"])
                                {
                                case "TrackEndEvent":
                                    {
                                        logger.Log("Received Dispatch (TRACK_END_EVENT)", LogSeverity.Debug);

                                        player.FireEvent(Event.TrackEnd, message["reason"]);
                                        TrackEnd?.InvokeAsync(player, currentTrack, (string)message["reason"]);

                                        break;
                                    }

                                case "TrackExceptionEvent":
                                    {
                                        logger.Log("Received Dispatch (TRACK_EXCEPTION_EVENT)", LogSeverity.Debug);

                                        player.FireEvent(Event.TrackException, message["error"]);
                                        TrackException?.InvokeAsync(player, currentTrack, (string)message["error"]);

                                        break;
                                    }

                                case "TrackStuckEvent":
                                    {
                                        logger.Log("Received Dispatch (TRACK_STUCK_EVENT)", LogSeverity.Debug);

                                        player.FireEvent(Event.TrackStuck, message["thresholdMs"]);
                                        TrackStuck?.InvokeAsync(player, currentTrack, (long)message["thresholdMs"]);

                                        break;
                                    }

                                default:
                                    {
                                        logger.Log($"Received Unknown Event Type {(string)message["type"]}", LogSeverity.Debug);

                                        break;
                                    }
                                }
                            }

                            break;
                        }

                    case "stats":
                        {
                            Stats?.InvokeAsync(new LavalinkStats(message));

                            break;
                        }

                    default:
                        {
                            logger.Log($"Received Uknown Dispatch ({(string)message["op"]})", LogSeverity.Debug);

                            break;
                        }
                    }

                    return Task.CompletedTask;
                };

                webSocket.OnClosed += async(closeStatus, closeDescription) =>
                {
                    await playerLock.WaitAsync();

                    players.Clear();
                    ConnectWebSocket();

                    playerLock.Release();
                };

                ConnectWebSocket();
            }));
        }