Beispiel #1
0
        public async Task <LavaTrack> GetTrack(string url, bool prioritiseSoundcloud = false)
        {
            try
            {
                LavaTrack track = null;

                if (Uri.TryCreate(url, UriKind.Absolute, out Uri uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))
                {
                    track = (await LavaRestClient.SearchTracksAsync(url)).Tracks.FirstOrDefault();
                }
                else
                {
                    if (prioritiseSoundcloud)
                    {
                        track = (await LavaRestClient.SearchSoundcloudAsync(url)).Tracks.FirstOrDefault();
                    }
                    else
                    {
                        track = (await LavaRestClient.SearchYouTubeAsync(url)).Tracks.FirstOrDefault();
                    }
                }

                return(track);
            }
            catch
            {
                return(null);
            }
        }
Beispiel #2
0
        public async Task <(bool, string)> PlayAsync(SocketVoiceChannel voiceChannel, ITextChannel textChannel, string query, ulong guildId, string prefix, int pos = -1)
        {
            var player = _lavaSocketClient.GetPlayer(guildId);

            if (player is null)
            {
                await _lavaSocketClient.ConnectAsync(voiceChannel, textChannel);

                player = _lavaSocketClient.GetPlayer(guildId);
            }
            LavaTrack    track     = null;
            SearchResult results   = null;
            var          uriResult = GetUriResult(query);

            switch (uriResult)
            {
            case UriResult.PlayList:
                results = await _lavaRestClient.SearchTracksAsync(query, true);

                return(false, await MakePlayList(player, results));

            case UriResult.YoutubeLink:
                results = await _lavaRestClient.SearchYouTubeAsync(query);

                track = results.Tracks.FirstOrDefault();
                break;

            case UriResult.SoundCloud:
                results = await _lavaRestClient.SearchSoundcloudAsync(query);

                track = results.Tracks.FirstOrDefault();
                break;

            default:
                results = await _lavaRestClient.SearchYouTubeAsync(query);

                break;
            }
            if (results.LoadType == LoadType.NoMatches || results.LoadType == LoadType.LoadFailed)
            {
                return(false, "No matches found.");
            }

            var tracks = results.Tracks.ToArray();

            if (pos == -1 && track == null)
            {
                return(true, MakeTrackList(prefix, tracks));
            }

            if (track == null)
            {
                track = tracks[pos];
            }

            if (player.IsPlaying)
            {
                player.Queue.Enqueue(track);
                return(false, $"{track.Title} has been added to the queue.");
            }
            await player.PlayAsync(track);

            return(false, $"Now Playing: **{track.Title}**");
        }