コード例 #1
0
ファイル: YouTubeModule.cs プロジェクト: dsdude123/MariBot
        public async Task PlayYoutubeVideo([Remainder] string videoUrl)
        {
            var channel = (Context.Message.Author as IGuildUser)?.VoiceChannel;

            if (channel == null)
            {
                await ReplyAsync("You must be in a voice channel to use this command!");

                return;
            }

            if (videoUrl.Equals("skip")) // redirection for skip command
            {
                await SkipVideo();

                return;
            }

            // get guild and video id
            var    guild   = Context.Guild.Id.ToString();
            string videoid = YouTubeService.getID(videoUrl);

            videoid = videoid.Remove(videoid.Length - 1);

            if (!queues.ContainsKey(guild)) // check if a queue has already been made for this session
            {
                queues[guild] = new Queue <string>();
            }

            List <int> duration;
            string     name;

            if (videoDatabase.cache.ContainsKey(videoid)) // check if our video store has the video so we can save time
            {
                name     = videoDatabase.cache[videoid].name;
                duration = videoDatabase.cache[videoid].duration;
            }
            else // not in video store
            {
                name     = YouTubeService.getName(videoUrl).TrimEnd('\n');
                duration = YouTubeService.getDuration(videoUrl);
                YouTubeObject newVideo = new YouTubeObject();
                newVideo.name     = name;
                newVideo.duration = duration;
                videoDatabase.cache.Add(videoid, newVideo);
                datbaseChanged = true;
            }
            if (duration.Count > 1)
            {
                if (duration.Count > 2)
                {
                    ReplyAsync("Videos longer than 7 minutes are prohibited!");
                }

                if (duration[0] > 7 && duration[1] > 0)
                {
                    ReplyAsync("Videos longer than 7 minutes are prohibited!");
                }
            }

            queues[guild].Enqueue(videoUrl);
            ReplyAsync("Added `" + name + "` to queue!");

            if (audioLocks.ContainsKey(guild) && audioLocks[guild].Equals(true)) // check if we are actively playing a video
            {
                return;
            }

            audioLocks[guild] = true;

            try
            {
                var audioClient = await channel.ConnectAsync();

                while (queues[guild].Count > 0)
                {
                    var nextVideoUrl = queues[guild].Dequeue();
                    videoid = YouTubeService.getID(nextVideoUrl).TrimEnd('\n');
                    if (!File.Exists(Environment.CurrentDirectory + "\\cache\\" + videoid + ".mp3"))
                    {
                        var status = YouTubeService.getVideo(nextVideoUrl, videoid, guild);
                    }
                    await ReplyAsync("Now playing: `" + videoDatabase.cache[videoid].name + "`");
                    await PlayAudio(audioClient, Environment.CurrentDirectory + "\\cache\\" + videoid + ".mp3");
                }
                audioLocks[guild] = false;
                audioClient.StopAsync();
            }
            catch (Exception e)
            {
                audioLocks[guild] = false;
                await ReplyAsync(e.Message);
            }
        }