From 9bb8f3d666124fd3bc93cc32435be6c8f7a4d247 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Tue, 4 Jul 2017 23:38:11 +0200 Subject: [PATCH] .ms readded --- src/NadekoBot/Modules/Music/Music.cs | 72 +++++++++------------ src/NadekoBot/Services/Music/MusicPlayer.cs | 7 +- src/NadekoBot/Services/Music/MusicQueue.cs | 18 +++++- src/NadekoBot/Services/Music/SongBuffer.cs | 8 ++- 4 files changed, 58 insertions(+), 47 deletions(-) diff --git a/src/NadekoBot/Modules/Music/Music.cs b/src/NadekoBot/Modules/Music/Music.cs index 4d3584c5..d8bc759d 100644 --- a/src/NadekoBot/Modules/Music/Music.cs +++ b/src/NadekoBot/Modules/Music/Music.cs @@ -45,6 +45,7 @@ namespace NadekoBot.Modules.Music var t = _music.DestroyPlayer(arg.Id); return Task.CompletedTask; } + //todo changing server region is bugged again private Task Client_UserVoiceStateUpdated(SocketUser iusr, SocketVoiceState oldState, SocketVoiceState newState) { @@ -725,7 +726,7 @@ namespace NadekoBot.Modules.Music [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] - public void Move() + public async Task Move() { var vch = ((IGuildUser)Context.User).VoiceChannel; @@ -737,53 +738,42 @@ namespace NadekoBot.Modules.Music if (mp == null) return; - mp.SetVoiceChannel(vch); + await mp.SetVoiceChannel(vch); } - //[NadekoCommand, Usage, Description, Aliases] - //[RequireContext(ContextType.Guild)] - //public async Task MoveSong([Remainder] string fromto) - //{ - // if (string.IsNullOrWhiteSpace(fromto)) - // return; + [NadekoCommand, Usage, Description, Aliases] + [RequireContext(ContextType.Guild)] + public async Task MoveSong([Remainder] string fromto) + { + if (string.IsNullOrWhiteSpace(fromto)) + return; - // MusicPlayer musicPlayer; - // if ((musicPlayer = _music.GetPlayer(Context.Guild.Id)) == null) - // return; + MusicPlayer mp = _music.GetPlayerOrDefault(Context.Guild.Id); + if (mp == null) + return; - // fromto = fromto?.Trim(); - // var fromtoArr = fromto.Split('>'); + fromto = fromto?.Trim(); + var fromtoArr = fromto.Split('>'); - // int n1; - // int n2; + SongInfo s; + if (fromtoArr.Length != 2 || !int.TryParse(fromtoArr[0], out var n1) || + !int.TryParse(fromtoArr[1], out var n2) || n1 < 1 || n2 < 1 || n1 == n2 + || (s = mp.MoveSong(n1, n2)) == null) + { + await ReplyConfirmLocalized("invalid_input").ConfigureAwait(false); + return; + } - // var playlist = musicPlayer.Playlist as List ?? musicPlayer.Playlist.ToList(); + var embed = new EmbedBuilder() + .WithTitle(s.Title.TrimTo(65)) + .WithUrl(s.SongUrl) + .WithAuthor(eab => eab.WithName(GetText("song_moved")).WithIconUrl("https://cdn.discordapp.com/attachments/155726317222887425/258605269972549642/music1.png")) + .AddField(fb => fb.WithName(GetText("from_position")).WithValue($"#{n1}").WithIsInline(true)) + .AddField(fb => fb.WithName(GetText("to_position")).WithValue($"#{n2}").WithIsInline(true)) + .WithColor(NadekoBot.OkColor); + await Context.Channel.EmbedAsync(embed).ConfigureAwait(false); + } - // if (fromtoArr.Length != 2 || !int.TryParse(fromtoArr[0], out n1) || - // !int.TryParse(fromtoArr[1], out n2) || n1 < 1 || n2 < 1 || n1 == n2 || - // n1 > playlist.Count || n2 > playlist.Count) - // { - // await ReplyConfirmLocalized("invalid_input").ConfigureAwait(false); - // return; - // } - - // var s = playlist[n1 - 1]; - // playlist.Insert(n2 - 1, s); - // var nn1 = n2 < n1 ? n1 : n1 - 1; - // playlist.RemoveAt(nn1); - - // var embed = new EmbedBuilder() - // .WithTitle($"{s.SongInfo.Title.TrimTo(70)}") - // .WithUrl(s.SongUrl) - // .WithAuthor(eab => eab.WithName(GetText("song_moved")).WithIconUrl("https://cdn.discordapp.com/attachments/155726317222887425/258605269972549642/music1.png")) - // .AddField(fb => fb.WithName(GetText("from_position")).WithValue($"#{n1}").WithIsInline(true)) - // .AddField(fb => fb.WithName(GetText("to_position")).WithValue($"#{n2}").WithIsInline(true)) - // .WithColor(NadekoBot.OkColor); - // await Context.Channel.EmbedAsync(embed).ConfigureAwait(false); - - // //await channel.SendConfirmAsync($"🎵Moved {s.PrettyName} `from #{n1} to #{n2}`").ConfigureAwait(false); - //} - [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] public async Task SetMaxQueue(uint size = 0) diff --git a/src/NadekoBot/Services/Music/MusicPlayer.cs b/src/NadekoBot/Services/Music/MusicPlayer.cs index b810c413..2064fb45 100644 --- a/src/NadekoBot/Services/Music/MusicPlayer.cs +++ b/src/NadekoBot/Services/Music/MusicPlayer.cs @@ -365,7 +365,6 @@ namespace NadekoBot.Services.Music public int Enqueue(SongInfo song) { - _log.Info("Adding song"); lock (locker) { if (Exited) @@ -547,7 +546,7 @@ namespace NadekoBot.Services.Music } } - public void SetVoiceChannel(IVoiceChannel vch) + public async Task SetVoiceChannel(IVoiceChannel vch) { lock (locker) { @@ -555,6 +554,7 @@ namespace NadekoBot.Services.Music return; VoiceChannel = vch; } + _audioClient = await vch.ConnectAsync(); } public async Task UpdateSongDurationsAsync() @@ -581,6 +581,9 @@ namespace NadekoBot.Services.Music } } + public SongInfo MoveSong(int n1, int n2) + => Queue.MoveSong(n1, n2); + //// this should be written better //public TimeSpan TotalPlaytime => // _playlist.Any(s => s.TotalTime == TimeSpan.MaxValue) ? diff --git a/src/NadekoBot/Services/Music/MusicQueue.cs b/src/NadekoBot/Services/Music/MusicQueue.cs index 967a5197..c8890484 100644 --- a/src/NadekoBot/Services/Music/MusicQueue.cs +++ b/src/NadekoBot/Services/Music/MusicQueue.cs @@ -9,7 +9,7 @@ namespace NadekoBot.Services.Music { public class MusicQueue : IDisposable { - private LinkedList Songs { get; } = new LinkedList(); + private LinkedList Songs { get; set; } = new LinkedList(); private int _currentIndex = 0; public int CurrentIndex { @@ -147,5 +147,21 @@ namespace NadekoBot.Services.Music CurrentIndex = new NadekoRandom().Next(Songs.Count); } } + + public SongInfo MoveSong(int n1, int n2) + { + lock (locker) + { + var playlist = Songs.ToList(); + if (n1 > playlist.Count || n2 > playlist.Count) + return null; + var s = playlist[n1 - 1]; + playlist.Insert(n2 - 1, s); + var nn1 = n2 < n1 ? n1 : n1 - 1; + playlist.RemoveAt(nn1); + Songs = new LinkedList(playlist); + return s; + } + } } } diff --git a/src/NadekoBot/Services/Music/SongBuffer.cs b/src/NadekoBot/Services/Music/SongBuffer.cs index af6bac8b..a3ec23a4 100644 --- a/src/NadekoBot/Services/Music/SongBuffer.cs +++ b/src/NadekoBot/Services/Music/SongBuffer.cs @@ -19,7 +19,7 @@ namespace NadekoBot.Services.Music public string SongUri { get; private set; } - private volatile bool restart = false; + //private volatile bool restart = false; public SongBuffer(string songUri, string skipTo) { @@ -50,11 +50,13 @@ namespace NadekoBot.Services.Music private void P_ErrorDataReceived(object sender, DataReceivedEventArgs e) { + if (string.IsNullOrWhiteSpace(e.Data)) + return; _log.Error(">>> " + e.Data); if (e.Data?.Contains("Error in the pull function") == true) { _log.Error("Ignore this."); - restart = true; + //restart = true; } } @@ -99,7 +101,7 @@ namespace NadekoBot.Services.Music if (!written) await Task.Delay(2000, cancelToken); } - while (!written); + while (!written && !cancelToken.IsCancellationRequested); lock (locker) if (_outStream.Length > 200_000 || bytesRead == 0) if (toReturn.TrySetResult(true))