improved blacklist server, small cleanup

This commit is contained in:
Master Kwoth 2016-03-04 22:54:03 +01:00
parent b910c82a09
commit f6bb62cb32
3 changed files with 20 additions and 28 deletions

View File

@ -43,7 +43,7 @@ namespace NadekoBot.Classes.Music {
public Channel PlaybackVoiceChannel { get; private set; } public Channel PlaybackVoiceChannel { get; private set; }
private bool Stopped { get; set; } = false; private bool Destroyed { get; set; } = false;
public MusicPlayer(Channel startingVoiceChannel, float? defaultVolume) { public MusicPlayer(Channel startingVoiceChannel, float? defaultVolume) {
if (startingVoiceChannel == null) if (startingVoiceChannel == null)
@ -57,30 +57,25 @@ namespace NadekoBot.Classes.Music {
cancelToken = SongCancelSource.Token; cancelToken = SongCancelSource.Token;
Task.Run(async () => { Task.Run(async () => {
while (!Stopped) { while (!Destroyed) {
try { try {
audioClient = await PlaybackVoiceChannel.JoinAudio(); audioClient = await PlaybackVoiceChannel.JoinAudio();
} } catch {
catch {
await Task.Delay(1000); await Task.Delay(1000);
continue; continue;
} }
CurrentSong = GetNextSong(); CurrentSong = GetNextSong();
if (CurrentSong != null) { var curSong = CurrentSong;
if (curSong != null) {
try { try {
OnStarted(CurrentSong); OnStarted(curSong);
await CurrentSong.Play(audioClient, cancelToken); await curSong.Play(audioClient, cancelToken);
} } catch (OperationCanceledException) {
catch (OperationCanceledException) {
Console.WriteLine("Song canceled"); Console.WriteLine("Song canceled");
} } catch (Exception ex) {
catch (Exception ex) {
Console.WriteLine($"Exception in PlaySong: {ex}"); Console.WriteLine($"Exception in PlaySong: {ex}");
} }
try { OnCompleted(curSong);
OnCompleted(CurrentSong);
}
catch { }
SongCancelSource = new CancellationTokenSource(); SongCancelSource = new CancellationTokenSource();
cancelToken = SongCancelSource.Token; cancelToken = SongCancelSource.Token;
} }
@ -101,6 +96,7 @@ namespace NadekoBot.Classes.Music {
public void Stop() { public void Stop() {
lock (playlistLock) { lock (playlistLock) {
playlist.Clear(); playlist.Clear();
CurrentSong = null;
if (!SongCancelSource.IsCancellationRequested) if (!SongCancelSource.IsCancellationRequested)
SongCancelSource.Cancel(); SongCancelSource.Cancel();
} }
@ -174,14 +170,11 @@ namespace NadekoBot.Classes.Music {
public void Destroy() { public void Destroy() {
lock (playlistLock) { lock (playlistLock) {
playlist.Clear(); playlist.Clear();
Destroyed = true;
if (!SongCancelSource.IsCancellationRequested) if (!SongCancelSource.IsCancellationRequested)
SongCancelSource.Cancel(); SongCancelSource.Cancel();
try {
Stopped = true;
audioClient.Disconnect(); audioClient.Disconnect();
} }
catch {}
}
} }
} }
} }

View File

@ -209,8 +209,6 @@ namespace NadekoBot.Classes.Music {
while (this.MusicPlayer.Paused) while (this.MusicPlayer.Paused)
await Task.Delay(200, cancelToken); await Task.Delay(200, cancelToken);
buffer = AdjustVolume(buffer, MusicPlayer.Volume); buffer = AdjustVolume(buffer, MusicPlayer.Volume);
Console.WriteLine("ADJUST VOLUME ERROR");
voiceClient.Send(buffer, 0, read); voiceClient.Send(buffer, 0, read);
} }
await bufferTask; await bufferTask;

View File

@ -451,9 +451,9 @@ namespace NadekoBot.Modules {
}); });
}); });
cgb.CreateCommand(Prefix + "ucl") cgb.CreateCommand(Prefix + "cbl")
.Description("Blacklists a mentioned channel (#general for example).\n**Usage**: ;ubl [channel_mention]") .Description("Blacklists a mentioned channel (#general for example).\n**Usage**: ;ubl [channel_mention]")
.Parameter("user", ParameterType.Unparsed) .Parameter("channel", ParameterType.Unparsed)
.Do(async e => { .Do(async e => {
await Task.Run(async () => { await Task.Run(async () => {
if (!e.Message.MentionedChannels.Any()) return; if (!e.Message.MentionedChannels.Any()) return;
@ -464,15 +464,16 @@ namespace NadekoBot.Modules {
}); });
}); });
cgb.CreateCommand(Prefix + "usl") cgb.CreateCommand(Prefix + "sbl")
.Description("Blacklists a server by a name or id (#general for example).\n**Usage**: ;usl [servername/serverid]") .Description("Blacklists a server by a name or id (#general for example).\n**Usage**: ;usl [servername/serverid]")
.Parameter("user", ParameterType.Unparsed) .Parameter("server", ParameterType.Unparsed)
.Do(async e => { .Do(async e => {
await Task.Run(async () => { await Task.Run(async () => {
var arg = e.GetArg("user"); var arg = e.GetArg("server")?.Trim();
if (string.IsNullOrWhiteSpace(arg)) if (string.IsNullOrWhiteSpace(arg))
return; return;
var server = NadekoBot.Client.FindServers(arg.Trim()).FirstOrDefault(); var server = NadekoBot.Client.Servers.FirstOrDefault(s => s.Id.ToString() == arg) ??
NadekoBot.Client.FindServers(arg.Trim()).FirstOrDefault();
if (server == null) { if (server == null) {
await e.Channel.SendMessage("Cannot find that server"); await e.Channel.SendMessage("Cannot find that server");
return; return;