diff --git a/src/NadekoBot/Modules/Music/Music.cs b/src/NadekoBot/Modules/Music/Music.cs index 15f7f6a3..ab131a0f 100644 --- a/src/NadekoBot/Modules/Music/Music.cs +++ b/src/NadekoBot/Modules/Music/Music.cs @@ -246,6 +246,9 @@ namespace NadekoBot.Modules.Music var add = ""; if (mp.Stopped) add += Format.Bold(GetText("queue_stopped", Format.Code(Prefix + "play"))) + "\n"; + var mps = mp.MaxPlaytimeSeconds; + if (mps > 0) + add += Format.Bold(GetText("song_skips_after", TimeSpan.FromSeconds(mps).ToString("g"))) + "\n"; if (mp.RepeatCurrentSong) add += "🔂 " + GetText("repeating_cur_song") + "\n"; else if (mp.Shuffle) @@ -259,7 +262,7 @@ namespace NadekoBot.Modules.Music } if (!string.IsNullOrWhiteSpace(add)) - desc += add + "\n"; + desc = add + "\n" + desc; var embed = new EmbedBuilder() .WithAuthor(eab => eab.WithName(GetText("player_queue", curPage + 1, lastPage + 1)) @@ -795,24 +798,21 @@ namespace NadekoBot.Modules.Music await ReplyConfirmLocalized("max_queue_x", size).ConfigureAwait(false); } - //[NadekoCommand, Usage, Description, Aliases] - //[RequireContext(ContextType.Guild)] - //public async Task SetMaxPlaytime(uint seconds) - //{ - // if (seconds < 15 && seconds != 0) - // return; + [NadekoCommand, Usage, Description, Aliases] + [RequireContext(ContextType.Guild)] + public async Task SetMaxPlaytime(uint seconds) + { + if (seconds < 15 && seconds != 0) + return; + + var mp = await _music.GetOrCreatePlayer(Context); + mp.MaxPlaytimeSeconds = seconds; + if (seconds == 0) + await ReplyConfirmLocalized("max_playtime_none").ConfigureAwait(false); + else + await ReplyConfirmLocalized("max_playtime_set", seconds).ConfigureAwait(false); + } - // var channel = (ITextChannel)Context.Channel; - // MusicPlayer musicPlayer; - // if ((musicPlayer = _music.GetPlayer(Context.Guild.Id)) == null) - // return; - // musicPlayer.MaxPlaytimeSeconds = seconds; - // if (seconds == 0) - // await ReplyConfirmLocalized("max_playtime_none").ConfigureAwait(false); - // else - // await ReplyConfirmLocalized("max_playtime_set", seconds).ConfigureAwait(false); - //} - [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] public async Task ReptCurSong() diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index d8977412..78297dd9 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -1198,7 +1198,7 @@ `{0}drawnew` or `{0}drawnew 5` - playlistshuffle plsh + shuffle plsh Shuffles the current playlist. diff --git a/src/NadekoBot/Services/Music/MusicPlayer.cs b/src/NadekoBot/Services/Music/MusicPlayer.cs index 38babb9c..10d4d2ac 100644 --- a/src/NadekoBot/Services/Music/MusicPlayer.cs +++ b/src/NadekoBot/Services/Music/MusicPlayer.cs @@ -50,6 +50,13 @@ namespace NadekoBot.Services.Music get => Queue.MaxQueueSize; set => Queue.MaxQueueSize = value; } + public uint MaxPlaytimeSeconds { get; set; } + + + const int _frameBytes = 3840; + const float _miliseconds = 20.0f; + public TimeSpan CurrentTime => TimeSpan.FromSeconds(_bytesSent / (float)_frameBytes / (1000 / _miliseconds)); + private int _bytesSent = 0; private IAudioClient _audioClient; private readonly object locker = new object(); @@ -78,6 +85,7 @@ namespace NadekoBot.Services.Music { while (!Exited) { + _bytesSent = 0; CancellationToken cancelToken; (int Index, SongInfo Song) data; lock (locker) @@ -122,12 +130,14 @@ namespace NadekoBot.Services.Music int bytesRead = 0; try { - while ((bytesRead = await b.ReadAsync(buffer, 0, buffer.Length, cancelToken).ConfigureAwait(false)) > 0) + while ((bytesRead = await b.ReadAsync(buffer, 0, buffer.Length, cancelToken).ConfigureAwait(false)) > 0 + && (MaxPlaytimeSeconds <= 0 || MaxPlaytimeSeconds >= CurrentTime.TotalSeconds)) { var vol = Volume; if (vol != 1) AdjustVolume(buffer, vol); await pcm.WriteAsync(buffer, 0, bytesRead, cancelToken).ConfigureAwait(false); + unchecked { _bytesSent += bytesRead; } await (pauseTaskSource?.Task ?? Task.CompletedTask); } diff --git a/src/NadekoBot/Services/Music/SongBuffer.cs b/src/NadekoBot/Services/Music/SongBuffer.cs index 0e507344..9f26671d 100644 --- a/src/NadekoBot/Services/Music/SongBuffer.cs +++ b/src/NadekoBot/Services/Music/SongBuffer.cs @@ -69,6 +69,7 @@ Check the guides for your platform on how to setup ffmpeg correctly: Linux Guide: https://goo.gl/ShjCUo"); } catch (OperationCanceledException) { } + catch (InvalidOperationException) { } // when ffmpeg is disposed catch (Exception ex) { _log.Info(ex); diff --git a/src/NadekoBot/_strings/ResponseStrings.en-US.json b/src/NadekoBot/_strings/ResponseStrings.en-US.json index 705b3c34..2703d2fd 100644 --- a/src/NadekoBot/_strings/ResponseStrings.en-US.json +++ b/src/NadekoBot/_strings/ResponseStrings.en-US.json @@ -455,6 +455,7 @@ "music_songs_shuffle_disable": "Songs will no longer shuffle.", "music_song_moved": "Song moved", "music_song_not_found": "No song found.", + "music_song_skips_after": "Songs will skip after {0}", "music_time_format": "{0}h {1}m {2}s", "music_to_position": "To position", "music_unlimited": "unlimited",