public async Task TrackEnded(TrackEndedEventArgs args) { if (!args.Reason.ShouldPlayNext()) { return; } if (!args.Player.Queue.TryDequeue(out var queueable)) { //await args.Player.TextChannel.SendMessageAsync("Playback Finished."); return; } if (!(queueable is LavaTrack track)) { await args.Player.TextChannel.SendMessageAsync("Next item in queue is not a track."); return; } await args.Player.PlayAsync(track); await args.Player.TextChannel.SendMessageAsync( embed : await EmbedHandler.CreateSongPlayEmbed(track)); }
/*** * Searches the internet for a song and plays it */ public async Task <Embed> PlayAsync(string query, IGuild guild, SocketGuildUser user) { //Check If User Is Connected To Voice Cahnnel. if (user.VoiceChannel == null) { return(await EmbedHandler.CreateErrorEmbed("Music, Join/Play", "You Must First Join a Voice Channel.")); } //Check the guild has a player available. if (!_lavaNode.HasPlayer(guild)) { return(await EmbedHandler.CreateErrorEmbed("Music, Play", "I'm not connected to a voice channel.")); } try { //Get the player for that guild. var player = _lavaNode.GetPlayer(guild); //Find The Youtube Track the User requested. LavaTrack track; SearchResponse search = new SearchResponse(); if (query.Contains("youtube.com")) { // Search Youtube search = await _lavaNode.SearchYouTubeAsync(query); } else if (query.Contains("soundcloud.com")) { // Search Soundcloud search = await _lavaNode.SearchSoundCloudAsync(query); } else { // Search youtube if not a URL search = await _lavaNode.SearchYouTubeAsync(query); } //If we couldn't find anything, tell the user. if (search.LoadStatus == LoadStatus.NoMatches) { return(await EmbedHandler.CreateErrorEmbed("Music", $"I wasn't able to find anything for {query}.")); } //Get the first track from the search results. //TODO: Add a 1-5 list for the user to pick from. (Like Fredboat) track = search.Tracks.FirstOrDefault(); //If the Bot is already playing music, or if it is paused but still has music in the queue, Add the requested track to the queue. if (player.Track != null && player.PlayerState is PlayerState.Playing || player.PlayerState is PlayerState.Paused) { player.Queue.Enqueue(track); Log.Information("Music", $"{track.Title} has been added to the queue."); return(await EmbedHandler.CreateSongQueueEmbed(track)); } //Player was not playing anything, so lets play the requested track. await player.PlayAsync(track); Log.Information("Music", $"Bot Now Playing: {track.Title}\nUrl: {track.Url}"); return(await EmbedHandler.CreateSongPlayEmbed(track)); } //If after all the checks we did, something still goes wrong. Tell the user about it so they can report it back to us. catch (Exception ex) { return(await EmbedHandler.CreateErrorEmbed("Music, Play", ex.Message)); } }