.ms readded

This commit is contained in:
Master Kwoth 2017-07-04 23:38:11 +02:00
parent c33c2bce60
commit 9bb8f3d666
4 changed files with 58 additions and 47 deletions

View File

@ -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,52 +738,41 @@ 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<Song> ?? musicPlayer.Playlist.ToList();
// 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);
//}
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);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]

View File

@ -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) ?

View File

@ -9,7 +9,7 @@ namespace NadekoBot.Services.Music
{
public class MusicQueue : IDisposable
{
private LinkedList<SongInfo> Songs { get; } = new LinkedList<SongInfo>();
private LinkedList<SongInfo> Songs { get; set; } = new LinkedList<SongInfo>();
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<SongInfo>(playlist);
return s;
}
}
}
}

View File

@ -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))