Fixed bugs, added .play command which acts as .n 1 when used without arguments or as .q command when used with serach query

This commit is contained in:
Master Kwoth 2017-07-02 14:49:37 +02:00
parent 5015b6ad95
commit 8e1c20624d
9 changed files with 76 additions and 46 deletions

View File

@ -1,9 +1,4 @@
using NadekoBot.Extensions; using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;

View File

@ -134,7 +134,7 @@ namespace NadekoBot.Modules.Administration
await user.RemoveRolesAsync(userRoles).ConfigureAwait(false); await user.RemoveRolesAsync(userRoles).ConfigureAwait(false);
await ReplyConfirmLocalized("rar", Format.Bold(user.ToString())).ConfigureAwait(false); await ReplyConfirmLocalized("rar", Format.Bold(user.ToString())).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception)
{ {
await ReplyErrorLocalized("rar_err").ConfigureAwait(false); await ReplyErrorLocalized("rar_err").ConfigureAwait(false);
} }

View File

@ -84,7 +84,16 @@ namespace NadekoBot.Modules.Music
private async Task InternalQueue(MusicPlayer mp, SongInfo songInfo, bool silent) 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 (qData.Success)
{ {
if (!silent) if (!silent)
@ -111,8 +120,16 @@ namespace NadekoBot.Modules.Music
} }
} }
} }
//todo test play
//todo add play command. .play = .n, .play whatever = .q whatever [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] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
@ -120,7 +137,7 @@ namespace NadekoBot.Modules.Music
{ {
var mp = await _music.GetOrCreatePlayer(Context); var mp = await _music.GetOrCreatePlayer(Context);
var songInfo = await _music.ResolveSong(query, Context.User.ToString()); 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) if ((await Context.Guild.GetCurrentUserAsync()).GetPermissions((IGuildChannel)Context.Channel).ManageMessages)
{ {
@ -241,7 +258,7 @@ namespace NadekoBot.Modules.Music
var mp = await _music.GetOrCreatePlayer(Context); var mp = await _music.GetOrCreatePlayer(Context);
mp.Next(); mp.Next(skipCount);
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
@ -520,7 +537,7 @@ namespace NadekoBot.Modules.Music
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false); await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
} }
//todo test shuffle
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task ShufflePlaylist() public async Task ShufflePlaylist()
@ -747,7 +764,6 @@ namespace NadekoBot.Modules.Music
//} //}
//todo test smq
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task SetMaxQueue(uint size = 0) public async Task SetMaxQueue(uint size = 0)
@ -756,7 +772,7 @@ namespace NadekoBot.Modules.Music
return; return;
var mp = await _music.GetOrCreatePlayer(Context); var mp = await _music.GetOrCreatePlayer(Context);
mp.SetMaxQueueSize(size); mp.MaxQueueSize = size;
if (size == 0) if (size == 0)
await ReplyConfirmLocalized("max_queue_unlimited").ConfigureAwait(false); await ReplyConfirmLocalized("max_queue_unlimited").ConfigureAwait(false);

View File

@ -212,8 +212,6 @@ namespace NadekoBot
var pokemonService = new PokemonService(); var pokemonService = new PokemonService();
#endregion #endregion
//initialize Services //initialize Services
Services = new NServiceProvider.ServiceProviderBuilder() Services = new NServiceProvider.ServiceProviderBuilder()
.Add<ILocalization>(Localization) .Add<ILocalization>(Localization)
@ -269,7 +267,6 @@ namespace NadekoBot
.Add<NadekoBot>(this) .Add<NadekoBot>(this)
.Build(); .Build();
CommandHandler.AddServices(Services); CommandHandler.AddServices(Services);
//setup typereaders //setup typereaders

View File

@ -1467,6 +1467,15 @@
<data name="next_usage" xml:space="preserve"> <data name="next_usage" xml:space="preserve">
<value>`{0}n` or `{0}n 5`</value> <value>`{0}n` or `{0}n 5`</value>
</data> </data>
<data name="play_cmd" xml:space="preserve">
<value>play start</value>
</data>
<data name="play_desc" xml:space="preserve">
<value>If no arguments are specified, acts as `{0}next 1` command. If you specify a search query, acts as a `{0}q` command</value>
</data>
<data name="play_usage" xml:space="preserve">
<value>`{0}play` or `{0}play Dream Of Venice`</value>
</data>
<data name="stop_cmd" xml:space="preserve"> <data name="stop_cmd" xml:space="preserve">
<value>stop s</value> <value>stop s</value>
</data> </data>

View File

@ -2,12 +2,12 @@
namespace NadekoBot.Services.Music 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 public class SongNotFoundException : Exception

View File

@ -45,6 +45,11 @@ namespace NadekoBot.Services.Music
public bool Shuffle { get; private set; } public bool Shuffle { get; private set; }
public bool Autoplay { get; private set; } public bool Autoplay { get; private set; }
public bool RepeatPlaylist { get; private set; } = true; public bool RepeatPlaylist { get; private set; } = true;
public uint MaxQueueSize
{
get => Queue.MaxQueueSize;
set => Queue.MaxQueueSize = value;
}
private IAudioClient _audioClient; private IAudioClient _audioClient;
private readonly object locker = new object(); private readonly object locker = new object();
@ -137,7 +142,6 @@ namespace NadekoBot.Services.Music
} }
finally finally
{ {
_log.Info("Next song");
do do
{ {
await Task.Delay(500); 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 (!RepeatCurrentSong) //if repeating current song, just ignore other settings, and play this song again (don't change the index)
{ {
if (Shuffle) if (Shuffle)
{
_log.Info("Random song");
Queue.Random(); //if shuffle is set, set current song index to a random number Queue.Random(); //if shuffle is set, set current song index to a random number
}
else else
{ {
//if last song, and autoplay is enabled, and if it's a youtube song //if last song, and autoplay is enabled, and if it's a youtube song
@ -155,19 +162,25 @@ namespace NadekoBot.Services.Music
{ {
try try
{ {
_log.Info("Loading related song");
//todo test autoplay //todo test autoplay
await _musicService.TryQueueRelatedSongAsync(data.Song.Query, OutputTextChannel, VoiceChannel); await _musicService.TryQueueRelatedSongAsync(data.Song.Query, OutputTextChannel, VoiceChannel);
Queue.Next(); Queue.Next();
} }
catch { } catch
{
_log.Info("Loading related song failed.");
}
} }
else if (Queue.Count == data.Index && !RepeatPlaylist) else if (Queue.Count == data.Index && !RepeatPlaylist)
{ {
//todo test repeatplaylist //todo test repeatplaylist
_log.Info("Stopping because repeatplaylist is disabled");
Stop(); Stop();
} }
else else
{ {
_log.Info("Next song");
Queue.Next(); Queue.Next();
} }
} }
@ -208,10 +221,11 @@ namespace NadekoBot.Services.Music
return (true, Queue.Count); return (true, Queue.Count);
} }
public void Next() public void Next(int skipCount)
{ {
lock (locker) lock (locker)
{ {
Queue.Next(skipCount - 1);
Stopped = false; Stopped = false;
Unpause(); Unpause();
CancelCurrentSong(); CancelCurrentSong();
@ -364,11 +378,6 @@ namespace NadekoBot.Services.Music
} }
} }
public void SetMaxQueueSize(uint size)
{
Queue.SetMaxQueueSize(size);
}
public void SetVoiceChannel(IVoiceChannel vch) public void SetVoiceChannel(IVoiceChannel vch)
{ {
VoiceChannel = vch; VoiceChannel = vch;

View File

@ -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) public void Add(SongInfo song)
{ {
song.ThrowIfNull(nameof(song)); song.ThrowIfNull(nameof(song));
lock (locker) lock (locker)
{ {
if(maxQueueSize !=0 && CurrentIndex >= maxQueueSize) if(MaxQueueSize != 0 && Songs.Count >= MaxQueueSize)
throw new PlaylistFullException(); throw new QueueFullException();
Songs.AddLast(song); Songs.AddLast(song);
} }
} }
public void Next() public void Next(int skipCount = 1)
{ {
lock(locker) lock(locker)
CurrentIndex++; CurrentIndex += skipCount;
} }
public void Dispose() public void Dispose()
@ -133,16 +147,5 @@ namespace NadekoBot.Services.Music
CurrentIndex = new NadekoRandom().Next(Songs.Count); CurrentIndex = new NadekoRandom().Next(Songs.Count);
} }
} }
public void SetMaxQueueSize(uint size)
{
if (size < 0)
throw new ArgumentOutOfRangeException(nameof(size));
lock (locker)
{
maxQueueSize = size;
}
}
} }
} }

View File

@ -68,6 +68,7 @@ Check the guides for your platform on how to setup ffmpeg correctly:
Windows Guide: https://goo.gl/OjKk8F Windows Guide: https://goo.gl/OjKk8F
Linux Guide: https://goo.gl/ShjCUo"); Linux Guide: https://goo.gl/ShjCUo");
} }
catch (OperationCanceledException) { }
catch (Exception ex) catch (Exception ex)
{ {
_log.Info(ex); _log.Info(ex);