void ResolveLink() { var query = RequestText; try { var video = DownloadUrlResolver.GetDownloadUrls(Searches.FindYoutubeUrlByKeywords(query)) .Where(v => v.AdaptiveType == AdaptiveType.Audio) .OrderByDescending(v => v.AudioBitrate).FirstOrDefault(); if (video == null) { throw new Exception("Could not load any video elements"); } StreamUrl = video.DownloadUrl; Title = video.Title; string invalidChars = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()); foreach (char c in invalidChars) { FileName = FileName.Replace(c.ToString(), "_"); } StartBuffering(); LinkResolved = true; Channel.Send($"{User.Mention}, Queued **{video.Title}**"); } catch (Exception e) { // Send a message to the guy that queued that Channel.SendMessage("This video is unavailable in the country the Bot is running in, or you enter an invalid name or url."); Console.WriteLine("Cannot parse youtube url: " + query); Cancel(); } }
//m r,radio - init //m n,next - next in que //m p,pause - pauses, call again to unpause //m yq [key_words] - queue from yt by keywords //m s,stop - stop //m sh - shuffle songs //m pl - current playlist public override void Install(ModuleManager manager) { var client = NadekoBot.client; manager.CreateCommands("!m", cgb => { //queue all more complex commands commands.ForEach(cmd => cmd.Init(cgb)); cgb.CreateCommand("n") .Alias("next") .Description("Goes to the next song in the queue.") .Do(e => { if (Voice != null && Exit == false) { NextSong = true; } }); cgb.CreateCommand("s") .Alias("stop") .Description("Completely stops the music and unbinds the bot from the channel.") .Do(e => { if (Voice != null && Exit == false) { Exit = true; SongQueue = new List <YouTubeVideo>(); } }); cgb.CreateCommand("p") .Alias("pause") .Description("Pauses the song") .Do(async e => { if (Voice != null && Exit == false && CurrentSong != null) { Pause = !Pause; if (Pause) { await e.Send("Pausing. Run the command again to resume."); } else { await e.Send("Resuming..."); } } }); cgb.CreateCommand("q") .Alias("yq") .Description("Queue a song using a multi/single word name.\n**Usage**: `!m q Dream Of Venice`") .Parameter("Query", ParameterType.Unparsed) .Do(async e => { var youtube = YouTube.Default; var video = youtube.GetAllVideos(Searches.FindYoutubeUrlByKeywords(e.Args[0])) .Where(v => v.AdaptiveKind == AdaptiveKind.Audio) .OrderByDescending(v => v.AudioBitrate).FirstOrDefault(); if (video?.Uri != "" && video.Uri != null) { SongQueue.Add(video); if (SongQueue.Count > 1) { await e.Send("**Queued** " + video.FullName); } } }); cgb.CreateCommand("lq") .Alias("ls").Alias("lp") .Description("Lists up to 10 currently queued songs.") .Do(async e => { await e.Send(SongQueue.Count + " videos currently queued."); await e.Send(string.Join("\n", SongQueue.Select(v => v.FullName).Take(10))); }); cgb.CreateCommand("sh") .Description("Shuffles the current playlist.") .Do(async e => { if (SongQueue.Count < 2) { await e.Send("Not enough songs in order to perform the shuffle."); return; } SongQueue.Shuffle(); await e.Send("Songs shuffled!"); }); cgb.CreateCommand("radio") .Alias("music") .Description("Binds to a voice and text channel in order to play music.") .Parameter("ChannelName", ParameterType.Unparsed) .Do(async e => { if (Voice != null) { return; } VoiceChannel = e.Server.FindChannels(e.GetArg("ChannelName").Trim(), ChannelType.Voice).FirstOrDefault(); Voice = await client.Audio().Join(VoiceChannel); Exit = false; NextSong = false; Pause = false; try { while (true) { if (Exit) { break; } if (SongQueue.Count == 0 || Pause) { Thread.Sleep(100); continue; } if (!LoadNextSong()) { break; } await Task.Run(async() => { if (Exit) { Voice = null; Exit = false; await e.Send("Exiting..."); return; } var streamer = new AudioStreamer(Music.CurrentSong.Uri); streamer.Start(); while (streamer.BytesSentToTranscoder < 100 * 0x1000 || streamer.NetworkDone) { await Task.Delay(500); } int blockSize = 1920 * client.Audio().Config.Channels; byte[] buffer = new byte[blockSize]; var msg = await e.Send("Playing " + Music.CurrentSong.FullName + " [00:00]"); int counter = 0; int byteCount; while ((byteCount = streamer.PCMOutput.Read(buffer, 0, blockSize)) > 0) { Voice.Send(buffer, byteCount); counter += blockSize; if (NextSong) { NextSong = false; break; } if (Exit) { Exit = false; return; } while (Pause) { Thread.Sleep(100); } } }); } Voice.Wait(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } await Voice.Disconnect(); Voice = null; VoiceChannel = null; }); }); }