From 54b6af2a1953e05eb2278fd7ab9f4c5489bc125e Mon Sep 17 00:00:00 2001 From: Kwoth Date: Tue, 27 Dec 2016 14:32:58 +0100 Subject: [PATCH] !!smp added (max playtime) --- .../Modules/Music/Classes/MusicControls.cs | 11 +++++- src/NadekoBot/Modules/Music/Classes/Song.cs | 5 +-- src/NadekoBot/Modules/Music/Music.cs | 35 +++++++++++++------ .../Resources/CommandStrings.Designer.cs | 27 ++++++++++++++ src/NadekoBot/Resources/CommandStrings.resx | 9 +++++ 5 files changed, 73 insertions(+), 14 deletions(-) diff --git a/src/NadekoBot/Modules/Music/Classes/MusicControls.cs b/src/NadekoBot/Modules/Music/Classes/MusicControls.cs index 1e439d00..bf3be763 100644 --- a/src/NadekoBot/Modules/Music/Classes/MusicControls.cs +++ b/src/NadekoBot/Modules/Music/Classes/MusicControls.cs @@ -34,7 +34,16 @@ namespace NadekoBot.Modules.Music.Classes /// Player will prioritize different queuer name /// over the song position in the playlist /// - public bool FairPlay { get; set; } = true; + public bool FairPlay { get; set; } = false; + + /// + /// Song will stop playing after this amount of time. + /// To prevent people queueing radio or looped songs + /// while other people want to listen to other songs too. + /// + public uint MaxPlaytimeSeconds { get; set; } = 0; + + public TimeSpan TotalPlaytime => new TimeSpan(playlist.Sum(s => s.TotalTime.Ticks)); /// /// Users who recently got their music wish diff --git a/src/NadekoBot/Modules/Music/Classes/Song.cs b/src/NadekoBot/Modules/Music/Classes/Song.cs index 0e52f74b..c4e53336 100644 --- a/src/NadekoBot/Modules/Music/Classes/Song.cs +++ b/src/NadekoBot/Modules/Music/Classes/Song.cs @@ -49,7 +49,7 @@ namespace NadekoBot.Modules.Music.Classes public string PrettyName => $"**[{SongInfo.Title.TrimTo(65)}]({songUrl})**"; - public string PrettyInfo => $"{PrettyTotalTime} | {PrettyProvider} | {QueuerName}"; + public string PrettyInfo => $"🔉 {(int)(MusicPlayer.Volume * 100)}% | {PrettyTotalTime} | {PrettyProvider} | {QueuerName}"; public string PrettyFullName => $"{PrettyName}\n\t\t*{PrettyInfo}*"; @@ -185,7 +185,8 @@ namespace NadekoBot.Modules.Music.Classes int nextTime = Environment.TickCount + milliseconds; byte[] buffer = new byte[frameBytes]; - while (!cancelToken.IsCancellationRequested) + while (!cancelToken.IsCancellationRequested && //song canceled for whatever reason + !(MusicPlayer.MaxPlaytimeSeconds != 0 && CurrentTime.TotalSeconds >= MusicPlayer.MaxPlaytimeSeconds)) // or exceedded max playtime { //Console.WriteLine($"Read: {songBuffer.ReadPosition}\nWrite: {songBuffer.WritePosition}\nContentLength:{songBuffer.ContentLength}\n---------"); var read = await inStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); diff --git a/src/NadekoBot/Modules/Music/Music.cs b/src/NadekoBot/Modules/Music/Music.cs index 5262bdd1..182666fd 100644 --- a/src/NadekoBot/Modules/Music/Music.cs +++ b/src/NadekoBot/Modules/Music/Music.cs @@ -232,14 +232,11 @@ namespace NadekoBot.Modules.Music return; try { await musicPlayer.UpdateSongDurationsAsync().ConfigureAwait(false); } catch { } - var embed = new EmbedBuilder() - .WithAuthor(eab => eab.WithName("Now Playing") - .WithMusicIcon()) - .WithTitle(currentSong.SongInfo.Title) - .WithDescription(currentSong.PrettyFullTime) - .WithFooter(ef => ef.WithText($"{currentSong.PrettyProvider} | {currentSong.QueuerName}")) - .WithOkColor() - .WithThumbnail(tn => tn.Url = currentSong.Thumbnail); + var embed = new EmbedBuilder().WithOkColor() + .WithAuthor(eab => eab.WithName("Now Playing").WithMusicIcon()) + .WithDescription(currentSong.PrettyName) + .WithFooter(ef => ef.WithText(currentSong.PrettyFullTime + $" | {currentSong.PrettyProvider} | {currentSong.QueuerName}")); + await channel.EmbedAsync(embed.Build()).ConfigureAwait(false); } @@ -557,13 +554,29 @@ namespace NadekoBot.Modules.Music var channel = (ITextChannel)umsg.Channel; MusicPlayer musicPlayer; if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer)) - { return; - } musicPlayer.MaxQueueSize = size; await channel.SendConfirmAsync($"🎵 Max queue set to {(size == 0 ? ("unlimited") : size + " tracks")}."); } + [NadekoCommand, Usage, Description, Aliases] + [RequireContext(ContextType.Guild)] + public async Task SetMaxPlaytime(IUserMessage imsg, uint seconds) + { + if (seconds < 15) + return; + + var channel = (ITextChannel)imsg.Channel; + MusicPlayer musicPlayer; + if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer)) + return; + musicPlayer.MaxPlaytimeSeconds = seconds; + if(seconds == 0) + await channel.SendConfirmAsync($"🎵 Max playtime has no limit now."); + else + await channel.SendConfirmAsync($"🎵 Max playtime set to {seconds} seconds."); + } + [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] public async Task ReptCurSong(IUserMessage umsg) @@ -845,7 +858,7 @@ namespace NadekoBot.Modules.Music playingMessage = await textCh.EmbedAsync(new EmbedBuilder().WithOkColor() .WithAuthor(eab => eab.WithName("Playing Song").WithMusicIcon()) .WithDescription(song.PrettyName) - .WithFooter(ef => ef.WithText($"🔉 {(int)(sender.Volume * 100)}% | {song.PrettyInfo}")) + .WithFooter(ef => ef.WithText(song.PrettyInfo)) .Build()) .ConfigureAwait(false); } diff --git a/src/NadekoBot/Resources/CommandStrings.Designer.cs b/src/NadekoBot/Resources/CommandStrings.Designer.cs index e51a9405..1be7930f 100644 --- a/src/NadekoBot/Resources/CommandStrings.Designer.cs +++ b/src/NadekoBot/Resources/CommandStrings.Designer.cs @@ -6377,6 +6377,33 @@ namespace NadekoBot.Resources { } } + /// + /// Looks up a localized string similar to setmaxplaytime smp. + /// + public static string setmaxplaytime_cmd { + get { + return ResourceManager.GetString("setmaxplaytime_cmd", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Sets a maximum number of seconds (>14) a song can run before being skipped automatically. Set 0 to have no limit.. + /// + public static string setmaxplaytime_desc { + get { + return ResourceManager.GetString("setmaxplaytime_desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to `{0}smp 0` or `{0}smp 270`. + /// + public static string setmaxplaytime_usage { + get { + return ResourceManager.GetString("setmaxplaytime_usage", resourceCulture); + } + } + /// /// Looks up a localized string similar to setmaxqueue smq. /// diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index a75839a5..88f2ef8e 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -2826,4 +2826,13 @@ `{0}def heresy` + + setmaxplaytime smp + + + Sets a maximum number of seconds (>14) a song can run before being skipped automatically. Set 0 to have no limit. + + + `{0}smp 0` or `{0}smp 270` + \ No newline at end of file