From 0c469113034145bde8213fcb0b8f404587193426 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Mon, 27 Jun 2016 22:32:08 +0200 Subject: [PATCH] Added !m autoplay (queues next related youtube song) --- NadekoBot/Classes/SearchHelper.cs | 27 ++++++++++++ .../Modules/Music/Classes/MusicControls.cs | 3 ++ NadekoBot/Modules/Music/MusicModule.cs | 43 ++++++++++++++----- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/NadekoBot/Classes/SearchHelper.cs b/NadekoBot/Classes/SearchHelper.cs index 049772cd..9525ec63 100644 --- a/NadekoBot/Classes/SearchHelper.cs +++ b/NadekoBot/Classes/SearchHelper.cs @@ -176,6 +176,33 @@ namespace NadekoBot.Classes return null; } + public static async Task GetRelatedVideoId(string id) + { + if (string.IsNullOrWhiteSpace(id)) + throw new ArgumentNullException(nameof(id)); + var match = new Regex("(?:youtu\\.be\\/|v=)(?[\\da-zA-Z\\-_]*)").Match(id); + if (match.Length > 1) + { + id = match.Groups["id"].Value; + } + var response = await GetResponseStringAsync( + $"https://www.googleapis.com/youtube/v3/search?" + + $"part=snippet&maxResults=1&type=video" + + $"&relatedToVideoId={id}" + + $"&key={NadekoBot.Creds.GoogleAPIKey}").ConfigureAwait(false); + JObject obj = JObject.Parse(response); + + var data = JsonConvert.DeserializeObject(response); + + if (data.items.Length > 0) + { + var toReturn = "http://www.youtube.com/watch?v=" + data.items[0].id.videoId.ToString(); + return toReturn; + } + else + return null; + } + public static async Task GetPlaylistIdByKeyword(string query) { if (string.IsNullOrWhiteSpace(NadekoBot.Creds.GoogleAPIKey)) diff --git a/NadekoBot/Modules/Music/Classes/MusicControls.cs b/NadekoBot/Modules/Music/Classes/MusicControls.cs index 6180706a..62eabc33 100644 --- a/NadekoBot/Modules/Music/Classes/MusicControls.cs +++ b/NadekoBot/Modules/Music/Classes/MusicControls.cs @@ -50,6 +50,7 @@ namespace NadekoBot.Modules.Music.Classes private bool Destroyed { get; set; } = false; public bool RepeatSong { get; private set; } = false; public bool RepeatPlaylist { get; private set; } = false; + public bool Autoplay { get; private set; } = false; public MusicPlayer(Channel startingVoiceChannel, float? defaultVolume) { @@ -239,5 +240,7 @@ namespace NadekoBot.Modules.Music.Classes internal bool ToggleRepeatSong() => this.RepeatSong = !this.RepeatSong; internal bool ToggleRepeatPlaylist() => this.RepeatPlaylist = !this.RepeatPlaylist; + + internal bool ToggleAutoplay() => this.Autoplay = !this.Autoplay; } } diff --git a/NadekoBot/Modules/Music/MusicModule.cs b/NadekoBot/Modules/Music/MusicModule.cs index ebd700f0..0241b870 100644 --- a/NadekoBot/Modules/Music/MusicModule.cs +++ b/NadekoBot/Modules/Music/MusicModule.cs @@ -677,10 +677,26 @@ namespace NadekoBot.Modules.Music return; await e.Channel.SendMessage($"🎶`Current song:` <{curSong.SongInfo.Query}>"); }); + + cgb.CreateCommand("autoplay") + .Alias("ap") + .Description("Toggles autoplay - When the song is finished, automatically queue a related youtube song. (Works only for youtube songs and when queue is empty)") + .Do(async e => + { + + MusicPlayer musicPlayer; + if (!MusicPlayers.TryGetValue(e.Server, out musicPlayer)) + return; + + if (!musicPlayer.ToggleAutoplay()) + await e.Channel.SendMessage("🎶`Autoplay disabled.`"); + else + await e.Channel.SendMessage("🎶`Autoplay enabled.`"); + }); }); } - private async Task QueueSong(Channel textCh, Channel voiceCh, string query, bool silent = false, MusicType musicType = MusicType.Normal) + public static async Task QueueSong(Channel textCh, Channel voiceCh, string query, bool silent = false, MusicType musicType = MusicType.Normal) { if (voiceCh == null || voiceCh.Server != textCh.Server) { @@ -713,8 +729,15 @@ namespace NadekoBot.Modules.Music if (playingMessage != null) await playingMessage.Delete().ConfigureAwait(false); lastFinishedMessage = await textCh.SendMessage($"🎵`Finished`{song.PrettyName}").ConfigureAwait(false); + if (mp.Autoplay && mp.Playlist.Count == 0 && song.SongInfo.Provider == "YouTube") + { + await QueueSong(textCh, voiceCh, await SearchHelper.GetRelatedVideoId(song.SongInfo.Query), silent, musicType).ConfigureAwait(false); + } + } + catch (Exception e) + { + Console.WriteLine(e); } - catch { } } }; mp.OnStarted += async (s, song) => @@ -745,14 +768,14 @@ namespace NadekoBot.Modules.Music var queuedMessage = await textCh.SendMessage($"🎵`Queued`{resolvedSong.PrettyName} **at** `#{musicPlayer.Playlist.Count}`").ConfigureAwait(false); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Task.Run(async () => - { - await Task.Delay(10000).ConfigureAwait(false); - try - { - await queuedMessage.Delete().ConfigureAwait(false); - } - catch { } - }).ConfigureAwait(false); + { + await Task.Delay(10000).ConfigureAwait(false); + try + { + await queuedMessage.Delete().ConfigureAwait(false); + } + catch { } + }).ConfigureAwait(false); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } }