fix #470 , !!ap now takes a random video out of 4 related ones

This commit is contained in:
Kwoth 2016-07-31 17:01:56 +02:00
parent 93af77778e
commit 31b2558523
3 changed files with 13 additions and 17 deletions

View File

@ -175,7 +175,7 @@ namespace NadekoBot.Classes
return null; return null;
} }
public static async Task<string> GetRelatedVideoId(string id) public static async Task<IEnumerable<string>> GetRelatedVideoIds(string id, int count = 1)
{ {
if (string.IsNullOrWhiteSpace(id)) if (string.IsNullOrWhiteSpace(id))
throw new ArgumentNullException(nameof(id)); throw new ArgumentNullException(nameof(id));
@ -186,20 +186,14 @@ namespace NadekoBot.Classes
} }
var response = await GetResponseStringAsync( var response = await GetResponseStringAsync(
$"https://www.googleapis.com/youtube/v3/search?" + $"https://www.googleapis.com/youtube/v3/search?" +
$"part=snippet&maxResults=1&type=video" + $"part=snippet&maxResults={count}&type=video" +
$"&relatedToVideoId={id}" + $"&relatedToVideoId={id}" +
$"&key={NadekoBot.Creds.GoogleAPIKey}").ConfigureAwait(false); $"&key={NadekoBot.Creds.GoogleAPIKey}").ConfigureAwait(false);
JObject obj = JObject.Parse(response); JObject obj = JObject.Parse(response);
var data = JsonConvert.DeserializeObject<YoutubeVideoSearch>(response); var data = JsonConvert.DeserializeObject<YoutubeVideoSearch>(response);
if (data.items.Length > 0) return data.items.Select(v => "http://www.youtube.com/watch?v=" + v.id.videoId);
{
var toReturn = "http://www.youtube.com/watch?v=" + data.items[0].id.videoId.ToString();
return toReturn;
}
else
return null;
} }
public static async Task<string> GetPlaylistIdByKeyword(string query) public static async Task<string> GetPlaylistIdByKeyword(string query)

View File

@ -48,8 +48,8 @@ namespace NadekoBot.Classes.Help.Commands
public Action<CommandEventArgs> DoGitFunc() => e => public Action<CommandEventArgs> DoGitFunc() => e =>
{ {
string helpstr = string helpstr =
$@"######For more information and how to setup your own NadekoBot, go to: **http://github.com/Kwoth/NadekoBot/** $@"######For more information and how to setup your own NadekoBot, go to: <http://github.com/Kwoth/NadekoBot/wiki>
######You can donate on patreon: `https://patreon.com/nadekobot` ######You can donate on patreon: <https://patreon.com/nadekobot>
######or paypal: `nadekodiscordbot@gmail.com` ######or paypal: `nadekodiscordbot@gmail.com`
#NadekoBot List Of Commands #NadekoBot List Of Commands

View File

@ -771,20 +771,22 @@ namespace NadekoBot.Modules.Music
var selSong = musicPlayer.Playlist.DefaultIfEmpty(null).ElementAtOrDefault(index - 1); var selSong = musicPlayer.Playlist.DefaultIfEmpty(null).ElementAtOrDefault(index - 1);
if (selSong == null) if (selSong == null)
{ {
await e.Channel.SendMessage("Could not select song, likely wrong index"); await e.Channel.SendMessage("Could not select song, likely wrong index");
} else }
else
{ {
await e.Channel.SendMessage($"🎶`Selected song {selSong.SongInfo.Title}:` <{selSong.SongInfo.Query}>").ConfigureAwait(false); await e.Channel.SendMessage($"🎶`Selected song {selSong.SongInfo.Title}:` <{selSong.SongInfo.Query}>").ConfigureAwait(false);
} }
} else }
else
{ {
var curSong = musicPlayer.CurrentSong; var curSong = musicPlayer.CurrentSong;
if (curSong == null) if (curSong == null)
return; return;
await e.Channel.SendMessage($"🎶`Current song:` <{curSong.SongInfo.Query}>").ConfigureAwait(false); await e.Channel.SendMessage($"🎶`Current song:` <{curSong.SongInfo.Query}>").ConfigureAwait(false);
} }
}); });
cgb.CreateCommand(Prefix + "autoplay") cgb.CreateCommand(Prefix + "autoplay")
@ -837,7 +839,7 @@ namespace NadekoBot.Modules.Music
lastFinishedMessage = await textCh.SendMessage($"🎵`Finished`{song.PrettyName}").ConfigureAwait(false); lastFinishedMessage = await textCh.SendMessage($"🎵`Finished`{song.PrettyName}").ConfigureAwait(false);
if (mp.Autoplay && mp.Playlist.Count == 0 && song.SongInfo.Provider == "YouTube") if (mp.Autoplay && mp.Playlist.Count == 0 && song.SongInfo.Provider == "YouTube")
{ {
await QueueSong(queuer, textCh, voiceCh, await SearchHelper.GetRelatedVideoId(song.SongInfo.Query), silent, musicType).ConfigureAwait(false); await QueueSong(queuer, textCh, voiceCh, (await SearchHelper.GetRelatedVideoIds(song.SongInfo.Query, 4)).ToList().Shuffle().FirstOrDefault(), silent, musicType).ConfigureAwait(false);
} }
} }
catch (Exception e) catch (Exception e)