!m smq added (setmaxqueue)

This commit is contained in:
Master Kwoth 2016-07-08 05:20:10 +02:00
parent 8044bbe978
commit adc449f765
4 changed files with 87 additions and 12 deletions

View File

@ -51,6 +51,7 @@ namespace NadekoBot.Modules.Music.Classes
public bool RepeatSong { get; private set; } = false; public bool RepeatSong { get; private set; } = false;
public bool RepeatPlaylist { get; private set; } = false; public bool RepeatPlaylist { get; private set; } = false;
public bool Autoplay { get; set; } = false; public bool Autoplay { get; set; } = false;
public uint MaxQueueSize { get; set; } = 0;
public MusicPlayer(Channel startingVoiceChannel, float? defaultVolume) public MusicPlayer(Channel startingVoiceChannel, float? defaultVolume)
{ {
@ -172,6 +173,7 @@ namespace NadekoBot.Modules.Music.Classes
{ {
if (s == null) if (s == null)
throw new ArgumentNullException(nameof(s)); throw new ArgumentNullException(nameof(s));
ThrowIfQueueFull();
lock (playlistLock) lock (playlistLock)
{ {
s.MusicPlayer = this; s.MusicPlayer = this;
@ -244,5 +246,13 @@ namespace NadekoBot.Modules.Music.Classes
internal bool ToggleRepeatPlaylist() => this.RepeatPlaylist = !this.RepeatPlaylist; internal bool ToggleRepeatPlaylist() => this.RepeatPlaylist = !this.RepeatPlaylist;
internal bool ToggleAutoplay() => this.Autoplay = !this.Autoplay; internal bool ToggleAutoplay() => this.Autoplay = !this.Autoplay;
internal void ThrowIfQueueFull()
{
if (MaxQueueSize == 0)
return;
if (playlist.Count >= MaxQueueSize)
throw new PlaylistFullException();
}
} }
} }

View File

@ -0,0 +1,12 @@
using System;
namespace NadekoBot.Modules.Music.Classes
{
class PlaylistFullException : Exception
{
public PlaylistFullException(string message) : base(message)
{
}
public PlaylistFullException() : base("Queue is full.") { }
}
}

View File

@ -230,7 +230,7 @@ namespace NadekoBot.Modules.Music
}); });
cgb.CreateCommand("max") cgb.CreateCommand("max")
.Description("Sets the music volume to 100% (real max is actually 150%).\n**Usage**: `!m max`") .Description("Sets the music volume to 100%.\n**Usage**: `!m max`")
.Do(e => .Do(e =>
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
@ -296,7 +296,7 @@ namespace NadekoBot.Modules.Music
var ids = await SearchHelper.GetVideoIDs(plId, 500).ConfigureAwait(false); var ids = await SearchHelper.GetVideoIDs(plId, 500).ConfigureAwait(false);
if (ids == null || ids.Count == 0) if (ids == null || ids.Count == 0)
{ {
await e.Channel.SendMessage($"🎵`Failed to find any songs.`"); await e.Channel.SendMessage($"🎵 `Failed to find any songs.`");
return; return;
} }
//todo TEMPORARY SOLUTION, USE RESOLVE QUEUE IN THE FUTURE //todo TEMPORARY SOLUTION, USE RESOLVE QUEUE IN THE FUTURE
@ -310,6 +310,8 @@ namespace NadekoBot.Modules.Music
{ {
await QueueSong(e.User, e.Channel, e.User.VoiceChannel, id, true).ConfigureAwait(false); await QueueSong(e.User, e.Channel, e.User.VoiceChannel, id, true).ConfigureAwait(false);
} }
catch (PlaylistFullException)
{ break; }
catch { } catch { }
} }
await msg.Edit("🎵 `Playlist queue complete.`").ConfigureAwait(false); await msg.Edit("🎵 `Playlist queue complete.`").ConfigureAwait(false);
@ -334,6 +336,8 @@ namespace NadekoBot.Modules.Music
return; return;
foreach (var svideo in scvids.Skip(1)) foreach (var svideo in scvids.Skip(1))
{
try
{ {
mp.AddSong(new Song(new Classes.SongInfo mp.AddSong(new Song(new Classes.SongInfo
{ {
@ -344,6 +348,8 @@ namespace NadekoBot.Modules.Music
Query = svideo.TrackLink, Query = svideo.TrackLink,
}), e.User.Name); }), e.User.Name);
} }
catch (PlaylistFullException) { break; }
}
}); });
cgb.CreateCommand("localplaylst") cgb.CreateCommand("localplaylst")
@ -361,9 +367,17 @@ namespace NadekoBot.Modules.Music
var fileEnum = new DirectoryInfo(arg).GetFiles() var fileEnum = new DirectoryInfo(arg).GetFiles()
.Where(x => !x.Attributes.HasFlag(FileAttributes.Hidden | FileAttributes.System)); .Where(x => !x.Attributes.HasFlag(FileAttributes.Hidden | FileAttributes.System));
foreach (var file in fileEnum) foreach (var file in fileEnum)
{
try
{ {
await QueueSong(e.User, e.Channel, e.User.VoiceChannel, file.FullName, true, MusicType.Local).ConfigureAwait(false); await QueueSong(e.User, e.Channel, e.User.VoiceChannel, file.FullName, true, MusicType.Local).ConfigureAwait(false);
} }
catch (PlaylistFullException)
{
break;
}
catch { }
}
await e.Channel.SendMessage("🎵 `Directory queue complete.`").ConfigureAwait(false); await e.Channel.SendMessage("🎵 `Directory queue complete.`").ConfigureAwait(false);
} }
catch { } catch { }
@ -481,6 +495,29 @@ namespace NadekoBot.Modules.Music
}); });
cgb.CreateCommand("setmaxqueue")
.Alias("smq")
.Description($"Sets a maximum queue size. Supply 0 or no argument to have no limit. \n**Usage**: `{Prefix} smq` 50 or `{Prefix} smq`")
.Parameter("size", ParameterType.Unparsed)
.Do(async e =>
{
MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(e.Server, out musicPlayer))
{
return;
}
var sizeStr = e.GetArg("size")?.Trim();
uint size = 0;
if (string.IsNullOrWhiteSpace(sizeStr) || !uint.TryParse(sizeStr, out size))
{
size = 0;
}
musicPlayer.MaxQueueSize = size;
await e.Channel.SendMessage($"🎵 `Max queue set to {(size == 0 ? ("unlimited") : size + " tracks")}`");
});
cgb.CreateCommand("cleanup") cgb.CreateCommand("cleanup")
.Description("Cleans up hanging voice connections. **Bot Owner Only!**\n**Usage**: `!m cleanup`") .Description("Cleans up hanging voice connections. **Bot Owner Only!**\n**Usage**: `!m cleanup`")
.AddCheck(SimpleCheckers.OwnerOnly()) .AddCheck(SimpleCheckers.OwnerOnly())
@ -630,6 +667,10 @@ namespace NadekoBot.Modules.Music
{ {
await QueueSong(e.User, textCh, voiceCh, si.Query, true, (MusicType)si.ProviderType).ConfigureAwait(false); await QueueSong(e.User, textCh, voiceCh, si.Query, true, (MusicType)si.ProviderType).ConfigureAwait(false);
} }
catch (PlaylistFullException)
{
break;
}
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Failed QueueSong in load playlist. {ex}"); Console.WriteLine($"Failed QueueSong in load playlist. {ex}");
@ -800,8 +841,19 @@ namespace NadekoBot.Modules.Music
}; };
return mp; return mp;
}); });
var resolvedSong = await Song.ResolveSong(query, musicType).ConfigureAwait(false); Song resolvedSong;
try
{
musicPlayer.ThrowIfQueueFull();
resolvedSong = await Song.ResolveSong(query, musicType).ConfigureAwait(false);
musicPlayer.AddSong(resolvedSong, queuer.Name); musicPlayer.AddSong(resolvedSong, queuer.Name);
}
catch (PlaylistFullException)
{
await textCh.SendMessage($"🎵 `Queue is full at {musicPlayer.MaxQueueSize}/{musicPlayer.MaxQueueSize}.` ");
throw;
}
if (!silent) if (!silent)
{ {
var queuedMessage = await textCh.SendMessage($"🎵`Queued`{resolvedSong.PrettyName} **at** `#{musicPlayer.Playlist.Count}`").ConfigureAwait(false); var queuedMessage = await textCh.SendMessage($"🎵`Queued`{resolvedSong.PrettyName} **at** `#{musicPlayer.Playlist.Count}`").ConfigureAwait(false);

View File

@ -144,6 +144,7 @@
<Compile Include="Classes\FlowersHandler.cs" /> <Compile Include="Classes\FlowersHandler.cs" />
<Compile Include="Modules\Conversations\Commands\RipCommand.cs" /> <Compile Include="Modules\Conversations\Commands\RipCommand.cs" />
<Compile Include="Modules\CustomReactions\CustomReactions.cs" /> <Compile Include="Modules\CustomReactions\CustomReactions.cs" />
<Compile Include="Modules\Music\Classes\PlaylistFullException.cs" />
<Compile Include="Modules\Programming\Commands\HaskellRepl.cs" /> <Compile Include="Modules\Programming\Commands\HaskellRepl.cs" />
<Compile Include="Modules\Programming\ProgrammingModule.cs" /> <Compile Include="Modules\Programming\ProgrammingModule.cs" />
<Compile Include="Modules\Searches\Commands\EvalCommand.cs" /> <Compile Include="Modules\Searches\Commands\EvalCommand.cs" />