/// <summary> /// Plays the designated <see cref="LavalinkTrack"/> /// </summary> /// <param name="track"></param> /// <returns></returns> public async Task PlayAsync(LavalinkTrack track) { CurrentTrack = track; await manager.PlayTrackAsync(track.TrackId, VoiceChannel.GuildId); Playing = true; }
/// <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(); })); }