Better strings

This commit is contained in:
Master Kwoth 2016-02-06 14:43:03 +01:00
parent 62b2f0c869
commit dafe20c98f
3 changed files with 46 additions and 35 deletions

View File

@ -29,6 +29,9 @@ namespace NadekoBot.Classes.Music {
public string Query { get; } public string Query { get; }
public string Title { get; internal set; } = String.Empty; public string Title { get; internal set; } = String.Empty;
private string Provider { get; set; }
public string FullPrettyName => $"**【 {Title.TrimTo(55)} 】**`{Provider}`";
private MusicStreamer musicStreamer = null; private MusicStreamer musicStreamer = null;
public StreamState State => musicStreamer?.State ?? privateState; public StreamState State => musicStreamer?.State ?? privateState;
@ -60,13 +63,15 @@ namespace NadekoBot.Classes.Music {
try { try {
if (RadioLink) { if (RadioLink) {
uri = Query; uri = Query;
Title = $"Radio Stream - <{Query}>"; Title = $"{Query}";
Provider = "Radio Stream";
} }
else if (SoundCloud.Default.IsSoundCloudLink(Query)) { else if (SoundCloud.Default.IsSoundCloudLink(Query)) {
if (OnResolving != null) if (OnResolving != null)
OnResolving(); OnResolving();
var svideo = await SoundCloud.Default.GetVideoAsync(Query); var svideo = await SoundCloud.Default.GetVideoAsync(Query);
Title = svideo.FullName + " - SoundCloud"; Title = svideo.FullName;
Provider = "SoundCloud";
uri = svideo.StreamLink; uri = svideo.StreamLink;
Console.WriteLine(uri); Console.WriteLine(uri);
} else { } else {
@ -83,7 +88,8 @@ namespace NadekoBot.Classes.Music {
if (video == null) // do something with this error if (video == null) // do something with this error
throw new Exception("Could not load any video elements based on the query."); throw new Exception("Could not load any video elements based on the query.");
Title = video.Title; //.Substring(0,video.Title.Length-10); // removing trailing "- You Tube" Title = video.Title.Substring(0,video.Title.Length-10); // removing trailing "- You Tube"
Provider = "YouTube";
uri = video.Uri; uri = video.Uri;
} }
} catch (Exception ex) { } catch (Exception ex) {
@ -176,11 +182,9 @@ namespace NadekoBot.Classes.Music {
}); });
int attempt = 0; int attempt = 0;
while (true) { while (true) {
int magickBuffer = 1; while (buffer.writePos - buffer.readPos > 5.MB() && State != StreamState.Completed) {
//wait for the read pos to catch up with write pos
while (buffer.writePos - buffer.readPos > 1.MB() && State != StreamState.Completed) {
prebufferingComplete = true; prebufferingComplete = true;
await Task.Delay(150); await Task.Delay(200);
} }
if (State == StreamState.Completed) { if (State == StreamState.Completed) {
@ -192,8 +196,8 @@ namespace NadekoBot.Classes.Music {
return; return;
} }
if (buffer.readPos > 1.MiB() && buffer.writePos > 1.MiB()) { // if buffer is over 5 MiB, create new one if (buffer.readPos > 5.MiB() && buffer.writePos > 5.MiB()) { // if buffer is over 5 MiB, create new one
var skip = 1.MB(); //remove only 5 MB, just in case var skip = 5.MB(); //remove only 5 MB, just in case
var newBuffer = new DualStream(); var newBuffer = new DualStream();
lock (_bufferLock) { lock (_bufferLock) {
@ -207,9 +211,9 @@ namespace NadekoBot.Classes.Music {
} }
} }
var buf = new byte[1024]; var buf = new byte[2048];
int read = 0; int read = 0;
read = await p.StandardOutput.BaseStream.ReadAsync(buf, 0, 1024); read = await p.StandardOutput.BaseStream.ReadAsync(buf, 0, 2048);
//Console.WriteLine($"Read: {read}"); //Console.WriteLine($"Read: {read}");
if (read == 0) { if (read == 0) {
if (attempt == 5) { if (attempt == 5) {
@ -240,7 +244,7 @@ namespace NadekoBot.Classes.Music {
Task.Factory.StartNew(async () => { Task.Factory.StartNew(async () => {
await BufferSong(); await BufferSong();
}, TaskCreationOptions.LongRunning).ConfigureAwait(false); }).ConfigureAwait(false);
// prebuffering wait stuff start // prebuffering wait stuff start
int bufferAttempts = 0; int bufferAttempts = 0;

View File

@ -52,9 +52,9 @@ namespace NadekoBot.Modules {
.Do(async e => { .Do(async e => {
if (musicPlayers.ContainsKey(e.Server) == false) return; if (musicPlayers.ContainsKey(e.Server) == false) return;
if (musicPlayers[e.Server].TogglePause()) if (musicPlayers[e.Server].TogglePause())
await e.Send("Music player paused."); await e.Send(":musical_note:`Music player paused.`");
else else
await e.Send("Music player unpaused."); await e.Send(":musical_note:`Music player unpaused.`");
}); });
cgb.CreateCommand("q") cgb.CreateCommand("q")
@ -67,14 +67,17 @@ namespace NadekoBot.Modules {
.Alias("ls").Alias("lp") .Alias("ls").Alias("lp")
.Description("Lists up to 10 currently queued songs.") .Description("Lists up to 10 currently queued songs.")
.Do(async e => { .Do(async e => {
if (musicPlayers.ContainsKey(e.Server) == false) await e.Send(":musical_note: No active music player."); if (musicPlayers.ContainsKey(e.Server) == false) {
await e.Send(":musical_note: No active music player.");
return;
}
var player = musicPlayers[e.Server]; var player = musicPlayers[e.Server];
string toSend = ":musical_note: " + player.SongQueue.Count + " videos currently queued. "; string toSend = ":musical_note: **" + player.SongQueue.Count + "** `videos currently queued.` ";
if (player.SongQueue.Count >= 25) if (player.SongQueue.Count >= 25)
toSend += "**Song queue is full!**\n"; toSend += "**Song queue is full!**\n";
await e.Send(toSend); await e.Send(toSend);
int number = 1; int number = 1;
await e.Send(string.Join("\n", player.SongQueue.Select(v => $"`{number++}.` {v.Title.TrimTo(60)}").Take(10))); await e.Send(string.Join("\n", player.SongQueue.Take(10).Select(v => $"`{number++}.` {v.FullPrettyName}")));
}); });
cgb.CreateCommand("np") cgb.CreateCommand("np")
@ -83,7 +86,7 @@ namespace NadekoBot.Modules {
.Do(async e => { .Do(async e => {
if (musicPlayers.ContainsKey(e.Server) == false) return; if (musicPlayers.ContainsKey(e.Server) == false) return;
var player = musicPlayers[e.Server]; var player = musicPlayers[e.Server];
await e.Send($"Now Playing **{player.CurrentSong.Title}**"); await e.Send($":musical_note:`Now Playing` {player.CurrentSong.FullPrettyName}");
}); });
cgb.CreateCommand("vol") cgb.CreateCommand("vol")
@ -99,7 +102,7 @@ namespace NadekoBot.Modules {
return; return;
} }
volume = player.SetVolume(volume); volume = player.SetVolume(volume);
await e.Send($":musical_note:Volume set to {volume}50%"); await e.Send($":musical_note: `Volume set to {volume}%`");
}); });
cgb.CreateCommand("min").Alias("mute") cgb.CreateCommand("min").Alias("mute")
@ -137,7 +140,7 @@ namespace NadekoBot.Modules {
} }
player.SongQueue.Shuffle(); player.SongQueue.Shuffle();
await e.Send(":musical_note: Songs shuffled!"); await e.Send(":musical_note: `Songs shuffled.`");
}); });
bool setgameEnabled = false; bool setgameEnabled = false;
@ -158,7 +161,7 @@ namespace NadekoBot.Modules {
else else
setgameTimer.Stop(); setgameTimer.Stop();
await e.Send("Music status " + (setgameEnabled ? "enabled" : "disabled")); await e.Send("`Music status " + (setgameEnabled ? "enabled`" : "disabled`"));
}); });
cgb.CreateCommand("pl") cgb.CreateCommand("pl")
@ -171,12 +174,12 @@ namespace NadekoBot.Modules {
} }
var ids = await Searches.GetVideoIDs(await Searches.GetPlaylistIdByKeyword(e.GetArg("playlist"))); var ids = await Searches.GetVideoIDs(await Searches.GetPlaylistIdByKeyword(e.GetArg("playlist")));
//todo TEMPORARY SOLUTION, USE RESOLVE QUEUE IN THE FUTURE //todo TEMPORARY SOLUTION, USE RESOLVE QUEUE IN THE FUTURE
var msg = await e.Send($":musical_note: Attempting to queue {ids.Count} songs".SnPl(ids.Count)); var msg = await e.Send($":musical_note: Attempting to queue **{ids.Count}** songs".SnPl(ids.Count));
foreach (var id in ids) { foreach (var id in ids) {
Task.Run(async () => await QueueSong(e, id, true)).ConfigureAwait(false); Task.Run(async () => await QueueSong(e, id, true)).ConfigureAwait(false);
await Task.Delay(150); await Task.Delay(150);
} }
msg?.Edit(":musical_note:Playlist queue complete."); msg?.Edit(":musical_note: `Playlist queue complete.`");
}); });
cgb.CreateCommand("radio").Alias("ra") cgb.CreateCommand("radio").Alias("ra")
@ -193,6 +196,8 @@ namespace NadekoBot.Modules {
cgb.CreateCommand("debug") cgb.CreateCommand("debug")
.Description("Writes some music data to console. **BOT OWNER ONLY**") .Description("Writes some music data to console. **BOT OWNER ONLY**")
.Do(e => { .Do(e => {
if (NadekoBot.OwnerID != e.User.Id)
return;
var output = "SERVER_NAME---SERVER_ID-----USERCOUNT----QUEUED\n" + 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)); string.Join("\n", musicPlayers.Select(kvp => kvp.Key.Name + "--" + kvp.Key.Id + " --" + kvp.Key.Users.Count() + "--" + kvp.Value.SongQueue.Count));
Console.WriteLine(output); Console.WriteLine(output);
@ -207,7 +212,7 @@ namespace NadekoBot.Modules {
} }
if (musicPlayers.ContainsKey(e.Server) == false) if (musicPlayers.ContainsKey(e.Server) == false)
if (!musicPlayers.TryAdd(e.Server, new MusicControls(e.User.VoiceChannel, e))) { 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 (query == null || query.Length < 4) if (query == null || query.Length < 4)
@ -226,15 +231,15 @@ namespace NadekoBot.Modules {
Message qmsg = null; Message qmsg = null;
Message msg = null; Message msg = null;
if (!silent) { if (!silent) {
qmsg = await e.Channel.SendMessage(":musical_note: **Searching...**"); qmsg = await e.Channel.SendMessage(":musical_note: `Searching...`");
sr.OnResolving += async () => { sr.OnResolving += async () => {
await qmsg.Edit($":musical_note: **Resolving**... \"{query}\""); await qmsg.Edit($":musical_note: `Resolving`... \"{query}\"");
}; };
sr.OnResolvingFailed += async (err) => { sr.OnResolvingFailed += async (err) => {
await qmsg.Edit($":anger: :musical_note: **Resolving failed** for `{query}`"); await qmsg.Edit($":anger: :musical_note: `Resolving failed` for **{query}**");
}; };
sr.OnQueued += async () => { sr.OnQueued += async () => {
await qmsg.Edit($":musical_note:**Queued** {sr.Title.TrimTo(55)}"); await qmsg.Edit($":musical_note:`Queued`{sr.FullPrettyName}");
}; };
} }
sr.OnCompleted += async () => { sr.OnCompleted += async () => {
@ -243,17 +248,18 @@ namespace NadekoBot.Modules {
if (mc.SongQueue.Count == 0) if (mc.SongQueue.Count == 0)
mc.Stop(); mc.Stop();
} }
await e.Send($":musical_note:**Finished playing** {sr.Title.TrimTo(55)}"); await e.Send($":musical_note:`Finished`{sr.FullPrettyName}");
}; };
sr.OnStarted += async () => { sr.OnStarted += async () => {
var msgTxt = $":musical_note:`Playing`{sr.FullPrettyName} `Vol: {(int)(player.Volume * 100)}%`";
if (msg == null) if (msg == null)
await e.Send($":musical_note:**Playing ** {sr.Title.TrimTo(55)} **Volume:** {(int)(player.Volume * 100)}%"); await e.Send(msgTxt);
else else
await msg.Edit($":musical_note:**Playing ** {sr.Title.TrimTo(55)} **Volume:** {(int)(player.Volume * 100)}%"); await msg.Edit(msgTxt);
qmsg?.Delete(); qmsg?.Delete();
}; };
sr.OnBuffering += async () => { sr.OnBuffering += async () => {
msg = await e.Send($":musical_note:**Buffering...** {sr.Title.TrimTo(55)}"); msg = await e.Send($":musical_note:`Buffering...`{sr.FullPrettyName}");
}; };
} catch (Exception ex) { } catch (Exception ex) {
Console.WriteLine(); Console.WriteLine();

View File

@ -9,6 +9,7 @@ using Discord.Modules;
using Discord.Audio; using Discord.Audio;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using System.Timers; using System.Timers;
using System.Linq;
namespace NadekoBot { namespace NadekoBot {
class NadekoBot { class NadekoBot {
@ -49,7 +50,7 @@ namespace NadekoBot {
Console.WriteLine("Forwarding messages."); Console.WriteLine("Forwarding messages.");
} }
if (string.IsNullOrWhiteSpace(creds.ParseID) || string.IsNullOrWhiteSpace(creds.ParseKey)) { if (string.IsNullOrWhiteSpace(creds.ParseID) || string.IsNullOrWhiteSpace(creds.ParseKey)) {
Console.WriteLine("Parse key and/or ID not found. Those are mandatory."); Console.WriteLine("Parse key and/or ID not found. Some functionality will be missing.");
ParseActive = false; ParseActive = false;
} else ParseActive = true; } else ParseActive = true;
@ -140,7 +141,7 @@ namespace NadekoBot {
static bool repliedRecently = false; static bool repliedRecently = false;
private static async void Client_MessageReceived(object sender, MessageEventArgs e) { private static async void Client_MessageReceived(object sender, MessageEventArgs e) {
if (e.Server != null || e.User.Id == client.CurrentUser.Id) return; if (e.Server != null || e.User.Id == client.CurrentUser.Id) return;
if (PollCommand.ActivePolls.SelectMany(kvp => kvp.Key.Users.Select(u=>u.Id)).Contains(e.User.Id)) return;
//just ban this trash AutoModerator //just ban this trash AutoModerator
if (e.User.Id == 105309315895693312) if (e.User.Id == 105309315895693312)
return; // FU return; // FU