From 9506dbb72c5cf061e5dc44c4d5d4eb1f9b3a1eb5 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Thu, 25 Feb 2016 05:57:24 +0100 Subject: [PATCH] Doesn't leave when finished playing. Totaly stable, tested and proven /sarcasm --- NadekoBot/Classes/Music/MusicControls.cs | 40 +++++++++++++++--------- NadekoBot/Classes/Music/StreamRequest.cs | 2 +- NadekoBot/Commands/PlayingRotate.cs | 2 +- NadekoBot/Modules/Music.cs | 2 +- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/NadekoBot/Classes/Music/MusicControls.cs b/NadekoBot/Classes/Music/MusicControls.cs index 97494239..b9857243 100644 --- a/NadekoBot/Classes/Music/MusicControls.cs +++ b/NadekoBot/Classes/Music/MusicControls.cs @@ -28,21 +28,30 @@ namespace NadekoBot.Classes.Music { public MusicControls() { Task.Run(async () => { - while (!Stopped) { - if (CurrentSong == null) { - if (SongQueue.Count > 0) - await LoadNextSong(); + while (true) { + if (!Stopped) { + if (CurrentSong == null) { + if (SongQueue.Count > 0) + await LoadNextSong(); + } + else if (CurrentSong.State == StreamState.Completed || NextSong) { + NextSong = false; + await LoadNextSong(); + } } - else if (CurrentSong.State == StreamState.Completed || NextSong) { - NextSong = false; - await LoadNextSong(); - } + else if (VoiceClient == null) + break; await Task.Delay(500); } }); } + internal void AddSong(StreamRequest streamRequest) { + Stopped = false; + this.SongQueue.Add(streamRequest); + } + public MusicControls(Channel voiceChannel, CommandEventArgs e, float? vol) : this() { if (voiceChannel == null) throw new ArgumentNullException(nameof(voiceChannel)); @@ -60,8 +69,7 @@ namespace NadekoBot.Classes.Music { SongQueue.RemoveAt(0); } else { - VoiceClient?.Disconnect(); - VoiceClient = null; + Stop(); return; } @@ -81,16 +89,18 @@ namespace NadekoBot.Classes.Music { } } - internal void Stop() { + internal void Stop(bool leave = false) { Stopped = true; SongQueue.Clear(); CurrentSong?.Stop(); CurrentSong = null; - VoiceClient?.Disconnect(); - VoiceClient = null; + if (leave) { + VoiceClient?.Disconnect(); + VoiceClient = null; - MusicControls throwAwayValue; - MusicModule.musicPlayers.TryRemove(_e.Server, out throwAwayValue); + MusicControls throwAwayValue; + MusicModule.musicPlayers.TryRemove(_e.Server, out throwAwayValue); + } } public int SetVolume(int value) { diff --git a/NadekoBot/Classes/Music/StreamRequest.cs b/NadekoBot/Classes/Music/StreamRequest.cs index 178dbb53..bbb0e503 100644 --- a/NadekoBot/Classes/Music/StreamRequest.cs +++ b/NadekoBot/Classes/Music/StreamRequest.cs @@ -49,7 +49,7 @@ namespace NadekoBot.Classes.Music { this.Server = e.Server; this.Query = query; this.RadioLink = radio; - mc.SongQueue.Add(this); + mc.AddSong(this); } public async Task Resolve() { diff --git a/NadekoBot/Commands/PlayingRotate.cs b/NadekoBot/Commands/PlayingRotate.cs index e47e27b8..51880896 100644 --- a/NadekoBot/Commands/PlayingRotate.cs +++ b/NadekoBot/Commands/PlayingRotate.cs @@ -16,7 +16,7 @@ namespace NadekoBot.Commands { {"%servers%", ()=> NadekoBot.client.Servers.Count().ToString() }, {"%users%", () => NadekoBot.client.Servers.SelectMany(s=>s.Users).Count().ToString() }, {"%playing%", () => { - var cnt = Modules.Music.musicPlayers.Count; + var cnt = Modules.Music.musicPlayers.Where(kvp => kvp.Value.CurrentSong != null).Count(); if(cnt == 1) { try { var mp = Modules.Music.musicPlayers.FirstOrDefault(); diff --git a/NadekoBot/Modules/Music.cs b/NadekoBot/Modules/Music.cs index 7111881e..76c0ecff 100644 --- a/NadekoBot/Modules/Music.cs +++ b/NadekoBot/Modules/Music.cs @@ -49,7 +49,7 @@ namespace NadekoBot.Modules { .Description("Completely stops the music, unbinds the bot from the channel, and cleans up files.") .Do(e => { if (musicPlayers.ContainsKey(e.Server) == false) return; - musicPlayers[e.Server].Stop(); + musicPlayers[e.Server].Stop(true); }); cgb.CreateCommand("p")