diff --git a/src/NadekoBot/Modules/Music/Music.cs b/src/NadekoBot/Modules/Music/Music.cs index 472acd17..a8b7ae0a 100644 --- a/src/NadekoBot/Modules/Music/Music.cs +++ b/src/NadekoBot/Modules/Music/Music.cs @@ -253,10 +253,9 @@ namespace NadekoBot.Modules.Music [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] - public Task Destroy() + public async Task Destroy() { - _music.DestroyPlayer(Context.Guild.Id); - return Task.CompletedTask; + await _music.DestroyPlayer(Context.Guild.Id); } [NadekoCommand, Usage, Description, Aliases] diff --git a/src/NadekoBot/Services/Music/MusicPlayer.cs b/src/NadekoBot/Services/Music/MusicPlayer.cs index 64c3aa22..b4bc4009 100644 --- a/src/NadekoBot/Services/Music/MusicPlayer.cs +++ b/src/NadekoBot/Services/Music/MusicPlayer.cs @@ -15,7 +15,7 @@ namespace NadekoBot.Services.Music Playing, Completed } - public class MusicPlayer : IDisposable + public class MusicPlayer { private readonly Task _player; private readonly IVoiceChannel VoiceChannel; @@ -92,7 +92,12 @@ namespace NadekoBot.Services.Music }); var ac = await GetAudioClient(); if (ac == null) + { + await Task.Delay(900); + // just wait some time, maybe bot doesn't even have perms to join that voice channel, + // i don't want to spam connection attempts continue; + } var pcm = ac.CreatePCMStream(AudioApplication.Music); OnStarted?.Invoke(this, data.Song); @@ -175,8 +180,8 @@ namespace NadekoBot.Services.Music { Stopped = false; Unpause(); + CancelCurrentSong(); } - CancelCurrentSong(); } public void Stop(bool clearQueue = false) @@ -188,8 +193,8 @@ namespace NadekoBot.Services.Music if (clearQueue) Queue.Clear(); Unpause(); + CancelCurrentSong(); } - CancelCurrentSong(); } private void Unpause() @@ -283,18 +288,22 @@ namespace NadekoBot.Services.Music } } - public void Dispose() + public async Task Destroy() { - _log.Info("Disposing"); + _log.Info("Destroying"); lock (locker) { + Stop(); Exited = true; Unpause(); + + OnCompleted = null; + OnPauseChanged = null; + OnStarted = null; } - CancelCurrentSong(); - OnCompleted = null; - OnPauseChanged = null; - OnStarted = null; + var ac = _audioClient; + if (ac != null) + await ac.StopAsync(); } diff --git a/src/NadekoBot/Services/Music/MusicQueue.cs b/src/NadekoBot/Services/Music/MusicQueue.cs index 50219dc8..cf3a0818 100644 --- a/src/NadekoBot/Services/Music/MusicQueue.cs +++ b/src/NadekoBot/Services/Music/MusicQueue.cs @@ -113,7 +113,10 @@ namespace NadekoBot.Services.Music public void ResetCurrent() { - CurrentIndex = 0; + lock (locker) + { + CurrentIndex = 0; + } } } } diff --git a/src/NadekoBot/Services/Music/MusicService.cs b/src/NadekoBot/Services/Music/MusicService.cs index eb2a5016..79d787fc 100644 --- a/src/NadekoBot/Services/Music/MusicService.cs +++ b/src/NadekoBot/Services/Music/MusicService.cs @@ -5,11 +5,9 @@ using System.Threading.Tasks; using Discord; using NadekoBot.Extensions; using NadekoBot.Services.Database.Models; -using System.Text.RegularExpressions; using NLog; using System.IO; using VideoLibrary; -using System.Net.Http; using System.Collections.Generic; using Discord.Commands; @@ -235,10 +233,10 @@ namespace NadekoBot.Services.Music return song; } - public void DestroyPlayer(ulong id) + public async Task DestroyPlayer(ulong id) { if (MusicPlayers.TryRemove(id, out var mp)) - mp.Dispose(); + await mp.Destroy(); } // public async Task QueueSong(IGuildUser queuer, ITextChannel textCh, IVoiceChannel voiceCh, string query, bool silent = false, MusicType musicType = MusicType.Normal)