UNSTABLE - Music bugfixes, pat command

This commit is contained in:
Master Kwoth 2016-02-02 00:05:41 +01:00
parent ae0c041445
commit c721076147
4 changed files with 49 additions and 25 deletions

View File

@ -4,9 +4,11 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Commands; using Discord.Commands;
using MusicModule = NadekoBot.Modules.Music;
namespace NadekoBot.Classes.Music { namespace NadekoBot.Classes.Music {
public class MusicControls { public class MusicControls {
private CommandEventArgs _e;
public bool NextSong = false; public bool NextSong = false;
public IAudioClient Voice; public IAudioClient Voice;
@ -17,7 +19,7 @@ namespace NadekoBot.Classes.Music {
public bool IsPaused { get; internal set; } = false; public bool IsPaused { get; internal set; } = false;
public bool Stopped { get; private set; } public bool Stopped { get; private set; }
public Channel VoiceChannel; public Channel VoiceChannel = null;
public IAudioClient VoiceClient = null; public IAudioClient VoiceClient = null;
@ -34,13 +36,16 @@ namespace NadekoBot.Classes.Music {
NextSong = false; NextSong = false;
await LoadNextSong(); await LoadNextSong();
} }
await Task.Delay(1000); await Task.Delay(500);
} }
}); });
} }
public MusicControls(Channel voiceChannel) : this() { public MusicControls(Channel voiceChannel, CommandEventArgs e) : this() {
if (voiceChannel == null)
throw new ArgumentNullException(nameof(voiceChannel));
VoiceChannel = voiceChannel; VoiceChannel = voiceChannel;
_e = e;
} }
public async Task LoadNextSong() { public async Task LoadNextSong() {
@ -56,14 +61,13 @@ namespace NadekoBot.Classes.Music {
} }
try { try {
if (VoiceChannel == null)
VoiceChannel = CurrentSong.Channel;
if (VoiceClient == null) if (VoiceClient == null)
VoiceClient = await NadekoBot.client.Audio().Join(VoiceChannel); VoiceClient = await NadekoBot.client.Audio().Join(VoiceChannel);
await CurrentSong.Start(); await CurrentSong.Start();
} catch (Exception ex) { } catch (Exception ex) {
Console.WriteLine($"Starting failed: {ex}"); Console.WriteLine($"Starting failed: {ex}");
CurrentSong?.Stop(); CurrentSong?.Stop();
CurrentSong = null;
} }
} }
@ -78,6 +82,9 @@ namespace NadekoBot.Classes.Music {
CurrentSong = null; CurrentSong = null;
VoiceClient?.Disconnect(); VoiceClient?.Disconnect();
VoiceClient = null; VoiceClient = null;
MusicControls throwAwayValue;
MusicModule.musicPlayers.TryRemove(_e.Server, out throwAwayValue);
} }
internal bool TogglePause() => IsPaused = !IsPaused; internal bool TogglePause() => IsPaused = !IsPaused;

View File

@ -24,7 +24,6 @@ namespace NadekoBot.Classes.Music {
} }
public class StreamRequest { public class StreamRequest {
public Channel Channel { get; }
public Server Server { get; } public Server Server { get; }
public User User { get; } public User User { get; }
public string Query { get; } public string Query { get; }
@ -48,7 +47,6 @@ namespace NadekoBot.Classes.Music {
this.Server = e.Server; this.Server = e.Server;
this.Query = query; this.Query = query;
Task.Run(() => ResolveStreamLink()); Task.Run(() => ResolveStreamLink());
Console.WriteLine("6");
mc.SongQueue.Add(this); mc.SongQueue.Add(this);
} }
@ -76,7 +74,7 @@ namespace NadekoBot.Classes.Music {
return; return;
} }
musicStreamer = new MusicStreamer(this, video.DownloadUrl, Channel); musicStreamer = new MusicStreamer(this, video.DownloadUrl);
if (OnQueued != null) if (OnQueued != null)
OnQueued(); OnQueued();
return; return;
@ -126,7 +124,6 @@ namespace NadekoBot.Classes.Music {
} }
public class MusicStreamer { public class MusicStreamer {
private Channel channel;
private DualStream buffer; private DualStream buffer;
public StreamState State { get; internal set; } public StreamState State { get; internal set; }
@ -138,9 +135,8 @@ namespace NadekoBot.Classes.Music {
private readonly object _bufferLock = new object(); private readonly object _bufferLock = new object();
private bool prebufferingComplete = false; private bool prebufferingComplete = false;
public MusicStreamer(StreamRequest parent, string directUrl, Channel channel) { public MusicStreamer(StreamRequest parent, string directUrl) {
this.parent = parent; this.parent = parent;
this.channel = channel;
this.buffer = new DualStream(); this.buffer = new DualStream();
this.Url = directUrl; this.Url = directUrl;
Console.WriteLine("Created new streamer"); Console.WriteLine("Created new streamer");
@ -206,7 +202,7 @@ namespace NadekoBot.Classes.Music {
read = await p.StandardOutput.BaseStream.ReadAsync(buf, 0, 1024); read = await p.StandardOutput.BaseStream.ReadAsync(buf, 0, 1024);
//Console.WriteLine($"Read: {read}"); //Console.WriteLine($"Read: {read}");
if (read == 0) { if (read == 0) {
if (attempt == 2) { if (attempt == 5) {
try { try {
p.CancelOutputRead(); p.CancelOutputRead();
p.Close(); p.Close();
@ -216,7 +212,7 @@ namespace NadekoBot.Classes.Music {
return; return;
} else { } else {
++attempt; ++attempt;
await Task.Delay(10); await Task.Delay(20);
} }
} else { } else {
attempt = 0; attempt = 0;
@ -296,9 +292,8 @@ namespace NadekoBot.Classes.Music {
internal void Stop() { internal void Stop() {
Console.WriteLine("Stopping playback"); Console.WriteLine("Stopping playback");
if (State != StreamState.Completed) { if (State != StreamState.Completed) {
if(State == StreamState.Playing)
parent.OnCompleted();
State = StreamState.Completed; State = StreamState.Completed;
parent.OnCompleted();
} }
} }
} }

View File

@ -186,6 +186,16 @@ namespace NadekoBot.Modules {
await e.Send(u.Mention + praises[r.Next(0, praises.Count)]); await e.Send(u.Mention + praises[r.Next(0, praises.Count)]);
}); });
cgb.CreateCommand("pat")
.Description("Pat someone ^_^")
.Parameter("user", ParameterType.Unparsed)
.Do(async e => {
var user = e.GetArg("user");
if (user == null || e.Message.MentionedUsers.Count() == 0) return;
await e.Send($"{e.Message.MentionedUsers.First().Mention} http://i.imgur.com/eOJlnwP.gif");
});
cgb.CreateCommand("are you real") cgb.CreateCommand("are you real")
.Description("Useless.") .Description("Useless.")
.Do(async e => { .Do(async e => {

View File

@ -55,10 +55,7 @@ namespace NadekoBot.Modules {
.Description("Completely stops the music and unbinds the bot from the channel and cleanes up files.") .Description("Completely stops the music and unbinds the bot from the channel and cleanes up files.")
.Do(e => { .Do(e => {
if (musicPlayers.ContainsKey(e.Server) == false) return; if (musicPlayers.ContainsKey(e.Server) == false) return;
var player = musicPlayers[e.Server]; musicPlayers[e.Server].Stop();
MusicControls throwAwayValue;
musicPlayers.TryRemove(e.Server, out throwAwayValue);
player.Stop();
}); });
cgb.CreateCommand("p") cgb.CreateCommand("p")
@ -77,13 +74,16 @@ namespace NadekoBot.Modules {
.Description("Queue a song using keywords or link. **You must be in a voice channel**.\n**Usage**: `!m q Dream Of Venice`") .Description("Queue a song using keywords or link. **You must be in a voice channel**.\n**Usage**: `!m q Dream Of Venice`")
.Parameter("query", ParameterType.Unparsed) .Parameter("query", ParameterType.Unparsed)
.Do(async e => { .Do(async e => {
if (e.User.VoiceChannel?.Server != e.Server) {
await e.Send(":anger: You need to be in the voice channel on this server.");
return;
}
if (musicPlayers.ContainsKey(e.Server) == false) if (musicPlayers.ContainsKey(e.Server) == false)
if (!musicPlayers.TryAdd(e.Server, new MusicControls(e.User.VoiceChannel))) { if (!musicPlayers.TryAdd(e.Server, new MusicControls(e.User.VoiceChannel, e))) {
await e.Send("Failed to create a music player for this server"); await e.Send("Failed to create a music player for this server");
return; return;
} }
if (e.GetArg("query") == null || e.GetArg("query").Length < 5) if (e.GetArg("query") == null || e.GetArg("query").Length < 4)
return; return;
var player = musicPlayers[e.Server]; var player = musicPlayers[e.Server];
@ -93,8 +93,7 @@ namespace NadekoBot.Modules {
} }
try { try {
if (e.User.VoiceChannel?.Server != e.Server)
throw new ArgumentException("You need to be in the voice channel on this server.");
Message qmsg = await e.Channel.SendMessage(":musical_note: **Searching...**"); Message qmsg = await e.Channel.SendMessage(":musical_note: **Searching...**");
var sr = new StreamRequest(e, e.GetArg("query"), player); var sr = new StreamRequest(e, e.GetArg("query"), player);
@ -112,6 +111,11 @@ namespace NadekoBot.Modules {
await qmsg.Edit($":musical_note:**Queued** {sr.Title.TrimTo(55)}"); await qmsg.Edit($":musical_note:**Queued** {sr.Title.TrimTo(55)}");
}; };
sr.OnCompleted += async () => { sr.OnCompleted += async () => {
MusicControls mc;
if (musicPlayers.TryGetValue(e.Server, out mc)) {
if (mc.SongQueue.Count == 0)
mc.Stop();
}
await e.Send($":musical_note:**Finished playing** {sr.Title.TrimTo(55)}"); await e.Send($":musical_note:**Finished playing** {sr.Title.TrimTo(55)}");
}; };
sr.OnStarted += async () => { sr.OnStarted += async () => {
@ -186,6 +190,14 @@ namespace NadekoBot.Modules {
await e.Send("Music status " + (setgameEnabled ? "enabled" : "disabled")); await e.Send("Music status " + (setgameEnabled ? "enabled" : "disabled"));
}); });
cgb.CreateCommand("debug")
.Description("Writes some music data to console. **BOT OWNER ONLY**")
.Do(e => {
var output = "SERVER_NAME---SERVER_ID-----USERCOUNT----QUEUED\n" +
string.Join("\n", musicPlayers.Select(kvp => kvp.Key.Name + "--" + kvp.Key.Id + " --" + kvp.Key.Users.Count() + "--" + kvp.Value.SongQueue.Count));
Console.WriteLine(output);
});
}); });
} }
} }