Exemple #1
0
        public async Task PlayAsync([Remainder] string query)
        {
            var regExp = new Regex(@"youtu(.*)(be|com).*(list=([a-zA-Z0-9_\-]+))");
            var match  = regExp.Match(query);

            Victoria.Entities.SearchResult search;
            if (match.Success)
            {
                search = await RestClient.SearchTracksAsync(query, false);
            }
            else
            {
                search = await RestClient.SearchYouTubeAsync(query);
            }

            if (search.LoadType == LoadType.NoMatches ||
                search.LoadType == LoadType.LoadFailed)
            {
                await ReplyAsync("Nothing found");

                return;
            }

            //If there is no player, join the current channel and set the player
            if (player == null)
            {
                await Join();

                player = LavaShardClient.GetPlayer(Context.Guild.Id);
            }

            var track = search.Tracks.FirstOrDefault();

            if (player.IsPlaying)
            {
                if (search.LoadType == LoadType.PlaylistLoaded)
                {
                    foreach (var playlistTrack in search.Tracks)
                    {
                        player.Queue.Enqueue(playlistTrack);
                    }
                    await ReplyAsync($"{search.Tracks.Count()} tracks added from playlist: {search.PlaylistInfo.Name}");
                }
                else
                {
                    player.Queue.Enqueue(track);
                    await ReplyAsync($"{track.Title} has been queued.");
                }
            }
            else
            {
                await player.PlayAsync(track);
                await ReplyAsync($"Now Playing: {track.Title}");

                if (search.LoadType == LoadType.PlaylistLoaded)
                {
                    foreach (var playlistTrack in search.Tracks.Where(x => x.Id != track.Id))
                    {
                        player.Queue.Enqueue(playlistTrack);
                    }
                    await ReplyAsync($"{search.Tracks.Count()} tracks added from playlist: {search.PlaylistInfo.Name}");
                }
            }
        }
Exemple #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}**");
        }
Exemple #3
0
        public async Task <bool> PlayTracksAsync(string url,
                                                 ICommandContext Context,
                                                 ITextChannel textChannel,
                                                 bool sendError = true,
                                                 bool clear     = true, bool shuffled = false)
        {
            var tplayer = await JoinAsync(Context, textChannel, true, false);

            var player = tplayer.Item2;
            var guild  = _guildConfigs.GetOrAdd(player.VoiceChannel.GuildId, new GuildMusicConfig());

            if (tplayer.Item1 != Successful && tplayer.Item1 != SameChannel)
            {
                if (sendError)
                {
                    await textChannel.SendMessageAsync($"{Context.User.Mention} {GetError(11)}");
                }
                return(false);
            }


            SocketVoiceChannel myChannel = player.VoiceChannel as SocketVoiceChannel;

            if ((player.IsPlaying && myChannel.Users.Count > 2 && !(Context.User as IGuildUser).ContainsRole("DJ")))
            {
                if (sendError)
                {
                    await textChannel.SendMessageAsync($"{Context.User.Mention} {GetError(12)}");
                }
                return(false);
            }
            if (guild.Queue.Count == 0 && !clear && !guild.IsPlayed)
            {
                return(await PlayTracksAsync(url, Context, textChannel, sendError, true));
            }

            if (clear)
            {
                guild.Queue.Clear();
            }

            var tracks = new CQueue <LavaTrack>((await LavaRestClient.SearchTracksAsync(url, true)).Tracks);

            if (shuffled)
            {
                tracks.Shuffle();
            }
            foreach (var track in tracks)
            {
                if (track == tracks.FirstOrDefault())
                {
                    continue;
                }
                guild.Queue.Enqueue(track);
            }

            if (clear)
            {
                var track = tracks.FirstOrDefault();
                if (sendError)
                {
                    EmbedBuilder Embed = new EmbedBuilder
                    {
                        Title       = "Playing Song",
                        Description = $"[{track.Title}]({track.Uri.AbsoluteUri})"
                    };

                    Embed.AddField("Length", track.Length.ToString(), true);
                    Embed.AddField("Tracks in Queue", (guild.Queue.Count).ToString(), true);
                    Embed.AddField("Next Track", guild.Queue.Count == 0 ? "No tracks" : (guild.Queue[0] as LavaTrack).Title, true);

                    Embed.ImageUrl = await track.FetchThumbnailAsync();

                    await textChannel.SendMessageAsync("", false, Embed.Build());
                }
                await player.PlayAsync(track);
            }
            return(true);
        }