Added !m autoplay (queues next related youtube song)

This commit is contained in:
Master Kwoth 2016-06-27 22:32:08 +02:00
parent 453ae8bbc2
commit 0c46911303
3 changed files with 63 additions and 10 deletions

View File

@ -176,6 +176,33 @@ namespace NadekoBot.Classes
return null;
}
public static async Task<string> GetRelatedVideoId(string id)
{
if (string.IsNullOrWhiteSpace(id))
throw new ArgumentNullException(nameof(id));
var match = new Regex("(?:youtu\\.be\\/|v=)(?<id>[\\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<YoutubeVideoSearch>(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<string> GetPlaylistIdByKeyword(string query)
{
if (string.IsNullOrWhiteSpace(NadekoBot.Creds.GoogleAPIKey))

View File

@ -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;
}
}

View File

@ -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) =>