From 108a9fb1560da923d1b6e97c521618ca4cab23ad Mon Sep 17 00:00:00 2001 From: Kwoth Date: Thu, 16 Feb 2017 18:13:24 +0100 Subject: [PATCH 1/4] Fixes to music. Thanks to samvaio --- .../Modules/Music/Classes/MusicControls.cs | 85 +++++++++++-------- src/NadekoBot/Modules/Music/Music.cs | 11 +-- 2 files changed, 52 insertions(+), 44 deletions(-) diff --git a/src/NadekoBot/Modules/Music/Classes/MusicControls.cs b/src/NadekoBot/Modules/Music/Classes/MusicControls.cs index fe7fe953..92ca19ae 100644 --- a/src/NadekoBot/Modules/Music/Classes/MusicControls.cs +++ b/src/NadekoBot/Modules/Music/Classes/MusicControls.cs @@ -7,6 +7,8 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using NLog; + namespace NadekoBot.Modules.Music.Classes { @@ -46,17 +48,19 @@ namespace NadekoBot.Modules.Music.Classes // this should be written better public TimeSpan TotalPlaytime => - playlist.Any(s => s.TotalTime == TimeSpan.MaxValue) ? + _playlist.Any(s => s.TotalTime == TimeSpan.MaxValue) ? TimeSpan.MaxValue : - new TimeSpan(playlist.Sum(s => s.TotalTime.Ticks)); + new TimeSpan(_playlist.Sum(s => s.TotalTime.Ticks)); /// /// Users who recently got their music wish /// private ConcurrentHashSet recentlyPlayedUsers { get; } = new ConcurrentHashSet(); - private readonly List playlist = new List(); - public IReadOnlyCollection Playlist => playlist; + private readonly List _playlist = new List(); + private readonly Logger _log; + + public IReadOnlyCollection Playlist => _playlist; public Song CurrentSong { get; private set; } public CancellationTokenSource SongCancelSource { get; private set; } @@ -73,7 +77,7 @@ namespace NadekoBot.Modules.Music.Classes public IVoiceChannel PlaybackVoiceChannel { get; private set; } public ITextChannel OutputTextChannel { get; set; } - private bool Destroyed { get; set; } = false; + private bool destroyed { get; set; } = false; public bool RepeatSong { get; private set; } = false; public bool RepeatPlaylist { get; private set; } = false; public bool Autoplay { get; set; } = false; @@ -90,6 +94,8 @@ namespace NadekoBot.Modules.Music.Classes if (startingVoiceChannel == null) throw new ArgumentNullException(nameof(startingVoiceChannel)); + _log = LogManager.GetCurrentClassLogger(); + OutputTextChannel = outputChannel; Volume = defaultVolume ?? 1.0f; @@ -101,7 +107,7 @@ namespace NadekoBot.Modules.Music.Classes { try { - while (!Destroyed) + while (!destroyed) { try { @@ -119,14 +125,14 @@ namespace NadekoBot.Modules.Music.Classes } catch (Exception ex) { - Console.WriteLine("Action queue crashed"); - Console.WriteLine(ex); + _log.Warn("Action queue crashed"); + _log.Warn(ex); } }).ConfigureAwait(false); - var t = new Thread(new ThreadStart(async () => + var t = new Thread(async () => { - while (!Destroyed) + while (!destroyed) { try { @@ -139,7 +145,7 @@ namespace NadekoBot.Modules.Music.Classes try { await audioClient.DisconnectAsync().ConfigureAwait(false); } catch { } audioClient = await PlaybackVoiceChannel.ConnectAsync().ConfigureAwait(false); - var index = playlist.IndexOf(CurrentSong); + var index = _playlist.IndexOf(CurrentSong); if (index != -1) RemoveSongAt(index, true); @@ -148,11 +154,14 @@ namespace NadekoBot.Modules.Music.Classes { await CurrentSong.Play(audioClient, cancelToken); } - catch(OperationCanceledException) + catch (OperationCanceledException) + { + } + finally { OnCompleted(this, CurrentSong); } - + if (RepeatPlaylist) AddSong(CurrentSong, CurrentSong.QueuerName); @@ -163,8 +172,8 @@ namespace NadekoBot.Modules.Music.Classes } catch (Exception ex) { - Console.WriteLine("Music thread almost crashed."); - Console.WriteLine(ex); + _log.Warn("Music thread almost crashed."); + _log.Warn(ex); await Task.Delay(3000).ConfigureAwait(false); } finally @@ -179,7 +188,7 @@ namespace NadekoBot.Modules.Music.Classes await Task.Delay(300).ConfigureAwait(false); } } - })); + }); t.Start(); } @@ -199,7 +208,8 @@ namespace NadekoBot.Modules.Music.Classes { RepeatPlaylist = false; RepeatSong = false; - playlist.Clear(); + Autoplay = false; + _playlist.Clear(); if (!SongCancelSource.IsCancellationRequested) SongCancelSource.Cancel(); }); @@ -222,10 +232,10 @@ namespace NadekoBot.Modules.Music.Classes { if (!FairPlay) { - return playlist.FirstOrDefault(); + return _playlist.FirstOrDefault(); } - var song = playlist.FirstOrDefault(c => !recentlyPlayedUsers.Contains(c.QueuerName)) - ?? playlist.FirstOrDefault(); + var song = _playlist.FirstOrDefault(c => !recentlyPlayedUsers.Contains(c.QueuerName)) + ?? _playlist.FirstOrDefault(); if (song == null) return null; @@ -243,9 +253,9 @@ namespace NadekoBot.Modules.Music.Classes { actionQueue.Enqueue(() => { - var oldPlaylist = playlist.ToArray(); - playlist.Clear(); - playlist.AddRange(oldPlaylist.Shuffle()); + var oldPlaylist = _playlist.ToArray(); + _playlist.Clear(); + _playlist.AddRange(oldPlaylist.Shuffle()); }); } @@ -258,7 +268,7 @@ namespace NadekoBot.Modules.Music.Classes { s.MusicPlayer = this; s.QueuerName = username.TrimTo(10); - playlist.Add(s); + _playlist.Add(s); }); } @@ -268,7 +278,7 @@ namespace NadekoBot.Modules.Music.Classes throw new ArgumentNullException(nameof(s)); actionQueue.Enqueue(() => { - playlist.Insert(index, s); + _playlist.Insert(index, s); }); } @@ -278,7 +288,7 @@ namespace NadekoBot.Modules.Music.Classes throw new ArgumentNullException(nameof(s)); actionQueue.Enqueue(() => { - playlist.Remove(s); + _playlist.Remove(s); }); } @@ -286,10 +296,10 @@ namespace NadekoBot.Modules.Music.Classes { actionQueue.Enqueue(() => { - if (index < 0 || index >= playlist.Count) + if (index < 0 || index >= _playlist.Count) return; - var song = playlist.ElementAtOrDefault(index); - if (playlist.Remove(song) && !silent) + var song = _playlist.ElementAtOrDefault(index); + if (_playlist.Remove(song) && !silent) { SongRemoved(song, index); } @@ -301,14 +311,14 @@ namespace NadekoBot.Modules.Music.Classes { actionQueue.Enqueue(() => { - playlist.Clear(); + _playlist.Clear(); }); } public async Task UpdateSongDurationsAsync() { var curSong = CurrentSong; - var toUpdate = playlist.Where(s => s.SongInfo.ProviderType == MusicType.Normal && + var toUpdate = _playlist.Where(s => s.SongInfo.ProviderType == MusicType.Normal && s.TotalTime == TimeSpan.Zero) .ToArray(); if (curSong != null) @@ -341,8 +351,9 @@ namespace NadekoBot.Modules.Music.Classes { RepeatPlaylist = false; RepeatSong = false; - Destroyed = true; - playlist.Clear(); + Autoplay = false; + destroyed = true; + _playlist.Clear(); try { await audioClient.DisconnectAsync(); } catch { } if (!SongCancelSource.IsCancellationRequested) @@ -358,17 +369,17 @@ namespace NadekoBot.Modules.Music.Classes // audioClient = await voiceChannel.ConnectAsync().ConfigureAwait(false); //} - public bool ToggleRepeatSong() => this.RepeatSong = !this.RepeatSong; + public bool ToggleRepeatSong() => RepeatSong = !RepeatSong; - public bool ToggleRepeatPlaylist() => this.RepeatPlaylist = !this.RepeatPlaylist; + public bool ToggleRepeatPlaylist() => RepeatPlaylist = !RepeatPlaylist; - public bool ToggleAutoplay() => this.Autoplay = !this.Autoplay; + public bool ToggleAutoplay() => Autoplay = !Autoplay; public void ThrowIfQueueFull() { if (MaxQueueSize == 0) return; - if (playlist.Count >= MaxQueueSize) + if (_playlist.Count >= MaxQueueSize) throw new PlaylistFullException(); } } diff --git a/src/NadekoBot/Modules/Music/Music.cs b/src/NadekoBot/Modules/Music/Music.cs index fa7012fd..15d8e82e 100644 --- a/src/NadekoBot/Modules/Music/Music.cs +++ b/src/NadekoBot/Modules/Music/Music.cs @@ -838,8 +838,7 @@ namespace NadekoBot.Modules.Music { try { - if (lastFinishedMessage != null) - lastFinishedMessage.DeleteAfter(0); + lastFinishedMessage?.DeleteAfter(0); lastFinishedMessage = await mp.OutputTextChannel.EmbedAsync(new EmbedBuilder().WithOkColor() .WithAuthor(eab => eab.WithName("Finished Song").WithMusicIcon()) @@ -855,7 +854,7 @@ namespace NadekoBot.Modules.Music textCh, voiceCh, relatedVideos[new NadekoRandom().Next(0, relatedVideos.Count)], - silent, + true, musicType).ConfigureAwait(false); } } @@ -870,8 +869,7 @@ namespace NadekoBot.Modules.Music return; try { - if (playingMessage != null) - playingMessage.DeleteAfter(0); + playingMessage?.DeleteAfter(0); playingMessage = await mp.OutputTextChannel.EmbedAsync(new EmbedBuilder().WithOkColor() .WithAuthor(eab => eab.WithName("Playing Song").WithMusicIcon()) @@ -891,8 +889,7 @@ namespace NadekoBot.Modules.Music else msg = await mp.OutputTextChannel.SendConfirmAsync("🎵 Music playback **resumed**.").ConfigureAwait(false); - if (msg != null) - msg.DeleteAfter(10); + msg?.DeleteAfter(10); } catch { } }; From ad2dd3a565f715337840943e2f7ceebc914af91c Mon Sep 17 00:00:00 2001 From: Kwoth Date: Thu, 16 Feb 2017 19:44:39 +0100 Subject: [PATCH 2/4] reduced the delay on .ban, .softban, and .kick to 1 second, from 2 --- src/NadekoBot/Modules/Administration/Administration.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/NadekoBot/Modules/Administration/Administration.cs b/src/NadekoBot/Modules/Administration/Administration.cs index 20760373..ab618ac5 100644 --- a/src/NadekoBot/Modules/Administration/Administration.cs +++ b/src/NadekoBot/Modules/Administration/Administration.cs @@ -241,7 +241,7 @@ namespace NadekoBot.Modules.Administration try { await user.SendErrorAsync(GetText("bandm", Format.Bold(Context.Guild.Name), msg)); - await Task.Delay(2000).ConfigureAwait(false); + await Task.Delay(1000).ConfigureAwait(false); } catch @@ -276,7 +276,7 @@ namespace NadekoBot.Modules.Administration try { await user.SendErrorAsync(GetText("sbdm", Format.Bold(Context.Guild.Name), msg)); - await Task.Delay(2000).ConfigureAwait(false); + await Task.Delay(1000).ConfigureAwait(false); } catch { @@ -311,7 +311,7 @@ namespace NadekoBot.Modules.Administration try { await user.SendErrorAsync(GetText("kickdm", Format.Bold(Context.Guild.Name), msg)); - await Task.Delay(2000).ConfigureAwait(false); + await Task.Delay(1000).ConfigureAwait(false); } catch { } } From 7ce0702c786c2a8acf6d6a2ccb6d1916f13eb5a1 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Thu, 16 Feb 2017 19:48:51 +0100 Subject: [PATCH 3/4] banning, kicking and softbanning no longer have the delay at all. --- src/NadekoBot/Modules/Administration/Administration.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/NadekoBot/Modules/Administration/Administration.cs b/src/NadekoBot/Modules/Administration/Administration.cs index ab618ac5..40d7776f 100644 --- a/src/NadekoBot/Modules/Administration/Administration.cs +++ b/src/NadekoBot/Modules/Administration/Administration.cs @@ -241,8 +241,6 @@ namespace NadekoBot.Modules.Administration try { await user.SendErrorAsync(GetText("bandm", Format.Bold(Context.Guild.Name), msg)); - await Task.Delay(1000).ConfigureAwait(false); - } catch { @@ -276,7 +274,6 @@ namespace NadekoBot.Modules.Administration try { await user.SendErrorAsync(GetText("sbdm", Format.Bold(Context.Guild.Name), msg)); - await Task.Delay(1000).ConfigureAwait(false); } catch { @@ -311,7 +308,6 @@ namespace NadekoBot.Modules.Administration try { await user.SendErrorAsync(GetText("kickdm", Format.Bold(Context.Guild.Name), msg)); - await Task.Delay(1000).ConfigureAwait(false); } catch { } } From b9673d5918839fdb1e9e2f2b064549e6c378bda9 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Thu, 16 Feb 2017 20:32:42 +0100 Subject: [PATCH 4/4] no idea what i did, locale won't be pink anymore though, thx aurora --- src/NadekoBot/Modules/Music/Music.cs | 17 ++++++++++++----- src/NadekoBot/Modules/NadekoModule.cs | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/NadekoBot/Modules/Music/Music.cs b/src/NadekoBot/Modules/Music/Music.cs index 15d8e82e..256a1c43 100644 --- a/src/NadekoBot/Modules/Music/Music.cs +++ b/src/NadekoBot/Modules/Music/Music.cs @@ -840,11 +840,18 @@ namespace NadekoBot.Modules.Music { lastFinishedMessage?.DeleteAfter(0); - lastFinishedMessage = await mp.OutputTextChannel.EmbedAsync(new EmbedBuilder().WithOkColor() - .WithAuthor(eab => eab.WithName("Finished Song").WithMusicIcon()) - .WithDescription(song.PrettyName) - .WithFooter(ef => ef.WithText(song.PrettyInfo))) - .ConfigureAwait(false); + try + { + lastFinishedMessage = await mp.OutputTextChannel.EmbedAsync(new EmbedBuilder().WithOkColor() + .WithAuthor(eab => eab.WithName("Finished Song").WithMusicIcon()) + .WithDescription(song.PrettyName) + .WithFooter(ef => ef.WithText(song.PrettyInfo))) + .ConfigureAwait(false); + } + catch + { + // ignored + } if (mp.Autoplay && mp.Playlist.Count == 0 && song.SongInfo.ProviderType == MusicType.Normal) { diff --git a/src/NadekoBot/Modules/NadekoModule.cs b/src/NadekoBot/Modules/NadekoModule.cs index 4c8d17d3..d76a483c 100644 --- a/src/NadekoBot/Modules/NadekoModule.cs +++ b/src/NadekoBot/Modules/NadekoModule.cs @@ -32,7 +32,7 @@ namespace NadekoBot.Modules { _cultureInfo = NadekoBot.Localization.GetCultureInfo(Context.Guild?.Id); - _log.Warn("Culture info is {0}", _cultureInfo); + _log.Info("Culture info is {0}", _cultureInfo); } //public Task ReplyConfirmLocalized(string titleKey, string textKey, string url = null, string footer = null)