From 0d6000daed6bfcc405266c41e837dbabbbf84095 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Mon, 18 Apr 2016 01:50:01 +0200 Subject: [PATCH] goto almost done, enabled experimental flags for C#7 preview support --- .../Modules/Music/Classes/MusicControls.cs | 8 ++ NadekoBot/Modules/Music/Classes/Song.cs | 21 +++++- NadekoBot/Modules/Music/MusicModule.cs | 74 +++++++++++++++---- NadekoBot/NadekoBot.csproj | 2 +- 4 files changed, 88 insertions(+), 17 deletions(-) diff --git a/NadekoBot/Modules/Music/Classes/MusicControls.cs b/NadekoBot/Modules/Music/Classes/MusicControls.cs index 70c5ef98..7011f092 100644 --- a/NadekoBot/Modules/Music/Classes/MusicControls.cs +++ b/NadekoBot/Modules/Music/Classes/MusicControls.cs @@ -175,6 +175,14 @@ namespace NadekoBot.Modules.Music.Classes } } + public void AddSong(Song s, int index) { + if (s == null) + throw new ArgumentNullException(nameof(s)); + lock (playlistLock) { + playlist.Insert(index, s); + } + } + public void RemoveSong(Song s) { if (s == null) diff --git a/NadekoBot/Modules/Music/Classes/Song.cs b/NadekoBot/Modules/Music/Classes/Song.cs index 2abf22f3..5c2a7466 100644 --- a/NadekoBot/Modules/Music/Classes/Song.cs +++ b/NadekoBot/Modules/Music/Classes/Song.cs @@ -43,11 +43,30 @@ namespace NadekoBot.Modules.Music.Classes private ulong bytesSent { get; set; } = 0; + public bool PrintStatusMessage { get; set; } = true; + + private int skipTo = 0; + public int SkipTo { + get { return SkipTo; } + set { + skipTo = value; + bytesSent = (ulong)skipTo * 3840 * 50; + } + } + private Song(SongInfo songInfo) { this.SongInfo = songInfo; } + public Song Clone() + { + var s = new Song(SongInfo); + s.MusicPlayer = MusicPlayer; + s.State = StreamState.Queued; + return s; + } + private Task BufferSong(CancellationToken cancelToken) => Task.Factory.StartNew(async () => { @@ -57,7 +76,7 @@ namespace NadekoBot.Modules.Music.Classes p = Process.Start(new ProcessStartInfo { FileName = "ffmpeg", - Arguments = $"-i {SongInfo.Uri} -f s16le -ar 48000 -ac 2 pipe:1 -loglevel quiet", + Arguments = $"-ss {skipTo} -i {SongInfo.Uri} -f s16le -ar 48000 -ac 2 pipe:1 -loglevel quiet", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = false, diff --git a/NadekoBot/Modules/Music/MusicModule.cs b/NadekoBot/Modules/Music/MusicModule.cs index c770d75a..bcd2bc0e 100644 --- a/NadekoBot/Modules/Music/MusicModule.cs +++ b/NadekoBot/Modules/Music/MusicModule.cs @@ -1,8 +1,8 @@ using Discord; using Discord.Commands; using Discord.Modules; -using NadekoBot.DataModels; using NadekoBot.Classes; +using NadekoBot.DataModels; using NadekoBot.Extensions; using NadekoBot.Modules.Music.Classes; using NadekoBot.Modules.Permissions.Classes; @@ -534,6 +534,42 @@ namespace NadekoBot.Modules.Music } } }); + + cgb.CreateCommand("goto") + .Description("Goes to a specific time in seconds in a song.") + .Parameter("time") + .Do(async e => + { + var skipToStr = e.GetArg("time")?.Trim(); + MusicPlayer musicPlayer; + if (!MusicPlayers.TryGetValue(e.Server, out musicPlayer)) + return; + + int skipTo; + if (!int.TryParse(skipToStr, out skipTo) || skipTo < 0) + return; + + var currentSong = musicPlayer.CurrentSong; + + if (currentSong == null) + return; + + //currentSong.PrintStatusMessage = false; + var gotoSong = currentSong.Clone(); + gotoSong.SkipTo = skipTo; + musicPlayer.AddSong(gotoSong, 0); + musicPlayer.Next(); + + var minutes = (skipTo / 60).ToString(); + var seconds = (skipTo % 60).ToString(); + + if (minutes.Length == 1) + minutes = "0" + minutes; + if (seconds.Length == 1) + seconds = "0" + seconds; + + await e.Channel.SendMessage($"`Skipped to {minutes}:{seconds}`"); + }); }); } @@ -561,27 +597,35 @@ namespace NadekoBot.Modules.Music Message lastFinishedMessage = null; mp.OnCompleted += async (s, song) => { - try + if (song.PrintStatusMessage) { - if (lastFinishedMessage != null) - await lastFinishedMessage.Delete(); - if (playingMessage != null) - await playingMessage.Delete(); - lastFinishedMessage = await textCh.SendMessage($"🎵`Finished`{song.PrettyName}"); + try + { + if (lastFinishedMessage != null) + await lastFinishedMessage.Delete(); + if (playingMessage != null) + await playingMessage.Delete(); + lastFinishedMessage = await textCh.SendMessage($"🎵`Finished`{song.PrettyName}"); + } + catch { } } - catch { } }; mp.OnStarted += async (s, song) => { - var sender = s as MusicPlayer; - if (sender == null) - return; - try + if (song.PrintStatusMessage) { - var msgTxt = $"🎵`Playing`{song.PrettyName} `Vol: {(int)(sender.Volume * 100)}%`"; - playingMessage = await textCh.SendMessage(msgTxt); + var sender = s as MusicPlayer; + if (sender == null) + return; + + try + { + + var msgTxt = $"🎵`Playing`{song.PrettyName} `Vol: {(int)(sender.Volume * 100)}%`"; + playingMessage = await textCh.SendMessage(msgTxt); + } + catch { } } - catch { } }; return mp; }); diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index e098355f..12f1a2bc 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -40,7 +40,7 @@ full false bin\Debug\ - DEBUG;TRACE + TRACE;DEBUG;__DEMO__,__DEMO_EXPERIMENTAL__ prompt 4 true