Exemplo n.º 1
0
        public async Task Play([Remainder] string video)
        {
            var New   = false;
            var Guild = Core.Guilds[Context.Guild.Id];
            var User  = Context.Guild.GetUser(Context.User.Id);

            if (User.VoiceChannel == null && !(Core.AudioSessions.ContainsKey(Guild) && await MiXLib.GetRank(User.Id, Context.Guild) > MiXLib.Rank.User))
            {
                await ReplyAsync("", false, MiXLib.GetEmbed("**Error:** User is not in a voice channel.", null, new Color(200, 0, 0)));

                return;
            }
            AudioSession AudioSession = null;

            if (!Core.AudioSessions.ContainsKey(Guild))
            {
                var client = await User.VoiceChannel.ConnectAsync();

                AudioSession = new AudioSession(client, Guild);
                Core.AudioSessions[Guild] = AudioSession;
                New = true;
            }
            else if (User.VoiceChannel != Context.Guild.GetUser(Context.Client.CurrentUser.Id).VoiceChannel&& await MiXLib.GetRank(User.Id, Context.Guild) < MiXLib.Rank.JuniorModerator)
            {
                await ReplyAsync("", false, MiXLib.GetEmbed("**Error:** User is in different voice channel.", null, new Color(200, 0, 0)));

                return;
            }
            else
            {
                AudioSession = Core.AudioSessions[Guild];
            }
            MiXLib.VideoType type = MiXLib.VideoType.nil;
            if (
                (
                    video.StartsWith("https://www.youtube.com/watch?v=") ||
                    video.StartsWith("http://www.youtube.com/watch?v=") ||
                    video.StartsWith("https://youtube.com/watch?v=") ||
                    video.StartsWith("http://youtube.com/watch?v=") /*||
                                                                     * video.StartsWith("https://youtu.be/") ||
                                                                     * video.StartsWith("http://youtu.be/") */
                ) &&
                !video.Contains(' ') &&
                !video.Contains('\n')
                )
            {
                type = MiXLib.VideoType.URL;
            }
            else
            {
                type = MiXLib.VideoType.Search;
            }
            var id = "";

            if (type == MiXLib.VideoType.Search)
            {
                id = await Song.IDFromSearchAsync(video);
            }
            else
            {
                id = video.Substring(video.IndexOf("?v=") + 3);
            }
            if (AudioSession.Queue.ContainsKey(id))
            {
                await ReplyAsync("", false, MiXLib.GetEmbed("**Error:** Cannot add song that is already queued into the queue.", null, new Color(200, 0, 0)));

                return;
            }
            Song song;

            if (type == MiXLib.VideoType.URL)
            {
                song = await Song.FromURLAsync(video);
            }
            else
            {
                song = await Song.FromSearchAsync(video);
            }
            AudioSession.Queue.Add(song.ID, song);
            if (New)
            {
                while (AudioSession.Queue.Count > 0)
                {
                    AudioSession.Client.Disconnected += async(ex) =>
                    {
                        await Task.Delay(0);

                        AudioSession.Queue.Clear();
                        Core.AudioSessions.Remove(Guild);
                        if (ex == null)
                        {
                            await ReplyAsync("", false, MiXLib.GetEmbed("Queue Concluded."));
                        }
                    };
                    var Song = AudioSession.Queue.FirstOrDefault().Value;
                    await ReplyAsync("", false, MiXLib.GetEmbed($"Playing **{Song.Name}**"));

                    await MiXLib.SendAudioAsync(AudioSession.Client, Song.FilePath);

                    await Task.Delay(Song.Duration);

                    Song.Dispose();
                    AudioSession.Queue.Remove(Song.ID);
                    await Task.Delay(1000);
                }
            }
            await AudioSession.EndAsync();

            await ReplyAsync("", false, MiXLib.GetEmbed("Queue Concluded."));
        }