From 8e1c20624d7d40148056c7fa97c6940262f8fe6f Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sun, 2 Jul 2017 14:49:37 +0200 Subject: [PATCH] Fixed bugs, added .play command which acts as .n 1 when used without arguments or as .q command when used with serach query --- .../DataStructures/PoopyRingBuffer.cs | 7 +--- .../Modules/Administration/Administration.cs | 2 +- src/NadekoBot/Modules/Music/Music.cs | 34 +++++++++++++----- src/NadekoBot/NadekoBot.cs | 3 -- src/NadekoBot/Resources/CommandStrings.resx | 9 +++++ src/NadekoBot/Services/Music/Exceptions.cs | 6 ++-- src/NadekoBot/Services/Music/MusicPlayer.cs | 25 ++++++++----- src/NadekoBot/Services/Music/MusicQueue.cs | 35 ++++++++++--------- src/NadekoBot/Services/Music/SongBuffer.cs | 1 + 9 files changed, 76 insertions(+), 46 deletions(-) diff --git a/src/NadekoBot/DataStructures/PoopyRingBuffer.cs b/src/NadekoBot/DataStructures/PoopyRingBuffer.cs index b0b9f19c..28b391e6 100644 --- a/src/NadekoBot/DataStructures/PoopyRingBuffer.cs +++ b/src/NadekoBot/DataStructures/PoopyRingBuffer.cs @@ -1,9 +1,4 @@ -using NadekoBot.Extensions; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; +using System; using System.Threading; using System.Threading.Tasks; diff --git a/src/NadekoBot/Modules/Administration/Administration.cs b/src/NadekoBot/Modules/Administration/Administration.cs index 129970e1..fafd9f48 100644 --- a/src/NadekoBot/Modules/Administration/Administration.cs +++ b/src/NadekoBot/Modules/Administration/Administration.cs @@ -134,7 +134,7 @@ namespace NadekoBot.Modules.Administration await user.RemoveRolesAsync(userRoles).ConfigureAwait(false); await ReplyConfirmLocalized("rar", Format.Bold(user.ToString())).ConfigureAwait(false); } - catch (Exception ex) + catch (Exception) { await ReplyErrorLocalized("rar_err").ConfigureAwait(false); } diff --git a/src/NadekoBot/Modules/Music/Music.cs b/src/NadekoBot/Modules/Music/Music.cs index a8906e62..493e3c08 100644 --- a/src/NadekoBot/Modules/Music/Music.cs +++ b/src/NadekoBot/Modules/Music/Music.cs @@ -84,7 +84,16 @@ namespace NadekoBot.Modules.Music private async Task InternalQueue(MusicPlayer mp, SongInfo songInfo, bool silent) { - var qData = mp.Enqueue(songInfo); + (bool Success, int Index) qData; + try + { + qData = mp.Enqueue(songInfo); + } + catch (QueueFullException) + { + await ReplyErrorLocalized("queue_full", mp.MaxQueueSize).ConfigureAwait(false); + throw; + } if (qData.Success) { if (!silent) @@ -111,8 +120,16 @@ namespace NadekoBot.Modules.Music } } } - - //todo add play command. .play = .n, .play whatever = .q whatever + //todo test play + [NadekoCommand, Usage, Description, Aliases] + [RequireContext(ContextType.Guild)] + public Task Play([Remainder]string query = null) + { + if (string.IsNullOrWhiteSpace(query)) + try { return Queue(query); } catch (QueueFullException) { return Task.CompletedTask; } + else + return Next(); + } [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] @@ -120,7 +137,7 @@ namespace NadekoBot.Modules.Music { var mp = await _music.GetOrCreatePlayer(Context); var songInfo = await _music.ResolveSong(query, Context.User.ToString()); - await InternalQueue(mp, songInfo, false); + try { await InternalQueue(mp, songInfo, false); } catch (QueueFullException) { return; } if ((await Context.Guild.GetCurrentUserAsync()).GetPermissions((IGuildChannel)Context.Channel).ManageMessages) { @@ -241,7 +258,7 @@ namespace NadekoBot.Modules.Music var mp = await _music.GetOrCreatePlayer(Context); - mp.Next(); + mp.Next(skipCount); } [NadekoCommand, Usage, Description, Aliases] @@ -520,7 +537,7 @@ namespace NadekoBot.Modules.Music await Context.Channel.EmbedAsync(embed).ConfigureAwait(false); } - //todo test shuffle + [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] public async Task ShufflePlaylist() @@ -746,8 +763,7 @@ namespace NadekoBot.Modules.Music //} - - //todo test smq + [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] public async Task SetMaxQueue(uint size = 0) @@ -756,7 +772,7 @@ namespace NadekoBot.Modules.Music return; var mp = await _music.GetOrCreatePlayer(Context); - mp.SetMaxQueueSize(size); + mp.MaxQueueSize = size; if (size == 0) await ReplyConfirmLocalized("max_queue_unlimited").ConfigureAwait(false); diff --git a/src/NadekoBot/NadekoBot.cs b/src/NadekoBot/NadekoBot.cs index 00bf8319..855c5652 100644 --- a/src/NadekoBot/NadekoBot.cs +++ b/src/NadekoBot/NadekoBot.cs @@ -212,8 +212,6 @@ namespace NadekoBot var pokemonService = new PokemonService(); #endregion - - //initialize Services Services = new NServiceProvider.ServiceProviderBuilder() .Add(Localization) @@ -269,7 +267,6 @@ namespace NadekoBot .Add(this) .Build(); - CommandHandler.AddServices(Services); //setup typereaders diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index 8ddcf04d..d8977412 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -1467,6 +1467,15 @@ `{0}n` or `{0}n 5` + + play start + + + If no arguments are specified, acts as `{0}next 1` command. If you specify a search query, acts as a `{0}q` command + + + `{0}play` or `{0}play Dream Of Venice` + stop s diff --git a/src/NadekoBot/Services/Music/Exceptions.cs b/src/NadekoBot/Services/Music/Exceptions.cs index af195c15..8d4dab72 100644 --- a/src/NadekoBot/Services/Music/Exceptions.cs +++ b/src/NadekoBot/Services/Music/Exceptions.cs @@ -2,12 +2,12 @@ namespace NadekoBot.Services.Music { - public class PlaylistFullException : Exception + public class QueueFullException : Exception { - public PlaylistFullException(string message) : base(message) + public QueueFullException(string message) : base(message) { } - public PlaylistFullException() : base("Queue is full.") { } + public QueueFullException() : base("Queue is full.") { } } public class SongNotFoundException : Exception diff --git a/src/NadekoBot/Services/Music/MusicPlayer.cs b/src/NadekoBot/Services/Music/MusicPlayer.cs index b31831e8..e49e8967 100644 --- a/src/NadekoBot/Services/Music/MusicPlayer.cs +++ b/src/NadekoBot/Services/Music/MusicPlayer.cs @@ -45,6 +45,11 @@ namespace NadekoBot.Services.Music public bool Shuffle { get; private set; } public bool Autoplay { get; private set; } public bool RepeatPlaylist { get; private set; } = true; + public uint MaxQueueSize + { + get => Queue.MaxQueueSize; + set => Queue.MaxQueueSize = value; + } private IAudioClient _audioClient; private readonly object locker = new object(); @@ -137,7 +142,6 @@ namespace NadekoBot.Services.Music } finally { - _log.Info("Next song"); do { await Task.Delay(500); @@ -146,7 +150,10 @@ namespace NadekoBot.Services.Music if (!RepeatCurrentSong) //if repeating current song, just ignore other settings, and play this song again (don't change the index) { if (Shuffle) + { + _log.Info("Random song"); Queue.Random(); //if shuffle is set, set current song index to a random number + } else { //if last song, and autoplay is enabled, and if it's a youtube song @@ -155,19 +162,25 @@ namespace NadekoBot.Services.Music { try { + _log.Info("Loading related song"); //todo test autoplay await _musicService.TryQueueRelatedSongAsync(data.Song.Query, OutputTextChannel, VoiceChannel); Queue.Next(); } - catch { } + catch + { + _log.Info("Loading related song failed."); + } } else if (Queue.Count == data.Index && !RepeatPlaylist) { //todo test repeatplaylist + _log.Info("Stopping because repeatplaylist is disabled"); Stop(); } else { + _log.Info("Next song"); Queue.Next(); } } @@ -208,10 +221,11 @@ namespace NadekoBot.Services.Music return (true, Queue.Count); } - public void Next() + public void Next(int skipCount) { lock (locker) { + Queue.Next(skipCount - 1); Stopped = false; Unpause(); CancelCurrentSong(); @@ -364,11 +378,6 @@ namespace NadekoBot.Services.Music } } - public void SetMaxQueueSize(uint size) - { - Queue.SetMaxQueueSize(size); - } - public void SetVoiceChannel(IVoiceChannel vch) { VoiceChannel = vch; diff --git a/src/NadekoBot/Services/Music/MusicQueue.cs b/src/NadekoBot/Services/Music/MusicQueue.cs index 0715ef2f..9156d0a7 100644 --- a/src/NadekoBot/Services/Music/MusicQueue.cs +++ b/src/NadekoBot/Services/Music/MusicQueue.cs @@ -50,23 +50,37 @@ namespace NadekoBot.Services.Music } } - public uint maxQueueSize { get; private set; } + private uint _maxQueueSize; + public uint MaxQueueSize + { + get => _maxQueueSize; + set + { + if (value < 0) + throw new ArgumentOutOfRangeException(nameof(value)); + + lock (locker) + { + _maxQueueSize = value; + } + } + } public void Add(SongInfo song) { song.ThrowIfNull(nameof(song)); lock (locker) { - if(maxQueueSize !=0 && CurrentIndex >= maxQueueSize) - throw new PlaylistFullException(); + if(MaxQueueSize != 0 && Songs.Count >= MaxQueueSize) + throw new QueueFullException(); Songs.AddLast(song); } } - public void Next() + public void Next(int skipCount = 1) { lock(locker) - CurrentIndex++; + CurrentIndex += skipCount; } public void Dispose() @@ -133,16 +147,5 @@ namespace NadekoBot.Services.Music CurrentIndex = new NadekoRandom().Next(Songs.Count); } } - - public void SetMaxQueueSize(uint size) - { - if (size < 0) - throw new ArgumentOutOfRangeException(nameof(size)); - - lock (locker) - { - maxQueueSize = size; - } - } } } diff --git a/src/NadekoBot/Services/Music/SongBuffer.cs b/src/NadekoBot/Services/Music/SongBuffer.cs index 953e640a..033ac9fd 100644 --- a/src/NadekoBot/Services/Music/SongBuffer.cs +++ b/src/NadekoBot/Services/Music/SongBuffer.cs @@ -68,6 +68,7 @@ Check the guides for your platform on how to setup ffmpeg correctly: Windows Guide: https://goo.gl/OjKk8F Linux Guide: https://goo.gl/ShjCUo"); } + catch (OperationCanceledException) { } catch (Exception ex) { _log.Info(ex);