small changes

This commit is contained in:
Master Kwoth 2017-07-01 08:16:06 +02:00
parent d242952d4a
commit f8ad6dda50
4 changed files with 26 additions and 17 deletions

View File

@ -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]

View File

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

View File

@ -113,7 +113,10 @@ namespace NadekoBot.Services.Music
public void ResetCurrent()
{
CurrentIndex = 0;
lock (locker)
{
CurrentIndex = 0;
}
}
}
}

View File

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