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

View File

@ -209,8 +209,6 @@ namespace NadekoBot.Classes.Music {
while (this.MusicPlayer.Paused)
await Task.Delay(200, cancelToken);
buffer = AdjustVolume(buffer, MusicPlayer.Volume);
Console.WriteLine("ADJUST VOLUME ERROR");
voiceClient.Send(buffer, 0, read);
}
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]")
.Parameter("user", ParameterType.Unparsed)
.Parameter("channel", ParameterType.Unparsed)
.Do(async e => {
await Task.Run(async () => {
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]")
.Parameter("user", ParameterType.Unparsed)
.Parameter("server", ParameterType.Unparsed)
.Do(async e => {
await Task.Run(async () => {
var arg = e.GetArg("user");
var arg = e.GetArg("server")?.Trim();
if (string.IsNullOrWhiteSpace(arg))
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) {
await e.Channel.SendMessage("Cannot find that server");
return;