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

View File

@ -24,7 +24,6 @@ namespace NadekoBot.Classes.Music {
}
public class StreamRequest {
public Channel Channel { get; }
public Server Server { get; }
public User User { get; }
public string Query { get; }
@ -48,7 +47,6 @@ namespace NadekoBot.Classes.Music {
this.Server = e.Server;
this.Query = query;
Task.Run(() => ResolveStreamLink());
Console.WriteLine("6");
mc.SongQueue.Add(this);
}
@ -76,7 +74,7 @@ namespace NadekoBot.Classes.Music {
return;
}
musicStreamer = new MusicStreamer(this, video.DownloadUrl, Channel);
musicStreamer = new MusicStreamer(this, video.DownloadUrl);
if (OnQueued != null)
OnQueued();
return;
@ -126,7 +124,6 @@ namespace NadekoBot.Classes.Music {
}
public class MusicStreamer {
private Channel channel;
private DualStream buffer;
public StreamState State { get; internal set; }
@ -138,9 +135,8 @@ namespace NadekoBot.Classes.Music {
private readonly object _bufferLock = new object();
private bool prebufferingComplete = false;
public MusicStreamer(StreamRequest parent, string directUrl, Channel channel) {
public MusicStreamer(StreamRequest parent, string directUrl) {
this.parent = parent;
this.channel = channel;
this.buffer = new DualStream();
this.Url = directUrl;
Console.WriteLine("Created new streamer");
@ -206,7 +202,7 @@ namespace NadekoBot.Classes.Music {
read = await p.StandardOutput.BaseStream.ReadAsync(buf, 0, 1024);
//Console.WriteLine($"Read: {read}");
if (read == 0) {
if (attempt == 2) {
if (attempt == 5) {
try {
p.CancelOutputRead();
p.Close();
@ -216,7 +212,7 @@ namespace NadekoBot.Classes.Music {
return;
} else {
++attempt;
await Task.Delay(10);
await Task.Delay(20);
}
} else {
attempt = 0;
@ -296,9 +292,8 @@ namespace NadekoBot.Classes.Music {
internal void Stop() {
Console.WriteLine("Stopping playback");
if (State != StreamState.Completed) {
if(State == StreamState.Playing)
parent.OnCompleted();
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)]);
});
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")
.Description("Useless.")
.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.")
.Do(e => {
if (musicPlayers.ContainsKey(e.Server) == false) return;
var player = musicPlayers[e.Server];
MusicControls throwAwayValue;
musicPlayers.TryRemove(e.Server, out throwAwayValue);
player.Stop();
musicPlayers[e.Server].Stop();
});
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`")
.Parameter("query", ParameterType.Unparsed)
.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.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");
return;
}
if (e.GetArg("query") == null || e.GetArg("query").Length < 5)
if (e.GetArg("query") == null || e.GetArg("query").Length < 4)
return;
var player = musicPlayers[e.Server];
@ -93,8 +93,7 @@ namespace NadekoBot.Modules {
}
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...**");
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)}");
};
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)}");
};
sr.OnStarted += async () => {
@ -170,7 +174,7 @@ namespace NadekoBot.Modules {
Timer setgameTimer = new Timer();
setgameTimer.Interval = 20000;
setgameTimer.Elapsed += (s, e) => {
int num = musicPlayers.Where(kvp=>kvp.Value.CurrentSong != null).Count();
int num = musicPlayers.Where(kvp => kvp.Value.CurrentSong != null).Count();
NadekoBot.client.SetGame($"{num} songs".SnPl(num) + $", {musicPlayers.Sum(kvp => kvp.Value.SongQueue.Count())} queued");
};
cgb.CreateCommand("setgame")
@ -186,6 +190,14 @@ namespace NadekoBot.Modules {
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);
});
});
}
}