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

View File

@ -52,9 +52,9 @@ namespace NadekoBot.Modules {
.Do(async e => {
if (musicPlayers.ContainsKey(e.Server) == false) return;
if (musicPlayers[e.Server].TogglePause())
await e.Send("Music player paused.");
await e.Send(":musical_note:`Music player paused.`");
else
await e.Send("Music player unpaused.");
await e.Send(":musical_note:`Music player unpaused.`");
});
cgb.CreateCommand("q")
@ -67,14 +67,17 @@ namespace NadekoBot.Modules {
.Alias("ls").Alias("lp")
.Description("Lists up to 10 currently queued songs.")
.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];
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)
toSend += "**Song queue is full!**\n";
await e.Send(toSend);
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")
@ -83,7 +86,7 @@ namespace NadekoBot.Modules {
.Do(async e => {
if (musicPlayers.ContainsKey(e.Server) == false) return;
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")
@ -99,7 +102,7 @@ namespace NadekoBot.Modules {
return;
}
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")
@ -137,7 +140,7 @@ namespace NadekoBot.Modules {
}
player.SongQueue.Shuffle();
await e.Send(":musical_note: Songs shuffled!");
await e.Send(":musical_note: `Songs shuffled.`");
});
bool setgameEnabled = false;
@ -158,7 +161,7 @@ namespace NadekoBot.Modules {
else
setgameTimer.Stop();
await e.Send("Music status " + (setgameEnabled ? "enabled" : "disabled"));
await e.Send("`Music status " + (setgameEnabled ? "enabled`" : "disabled`"));
});
cgb.CreateCommand("pl")
@ -171,12 +174,12 @@ namespace NadekoBot.Modules {
}
var ids = await Searches.GetVideoIDs(await Searches.GetPlaylistIdByKeyword(e.GetArg("playlist")));
//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) {
Task.Run(async () => await QueueSong(e, id, true)).ConfigureAwait(false);
await Task.Delay(150);
}
msg?.Edit(":musical_note:Playlist queue complete.");
msg?.Edit(":musical_note: `Playlist queue complete.`");
});
cgb.CreateCommand("radio").Alias("ra")
@ -193,6 +196,8 @@ namespace NadekoBot.Modules {
cgb.CreateCommand("debug")
.Description("Writes some music data to console. **BOT OWNER ONLY**")
.Do(e => {
if (NadekoBot.OwnerID != e.User.Id)
return;
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);
@ -207,7 +212,7 @@ namespace NadekoBot.Modules {
}
if (musicPlayers.ContainsKey(e.Server) == false)
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;
}
if (query == null || query.Length < 4)
@ -226,15 +231,15 @@ namespace NadekoBot.Modules {
Message qmsg = null;
Message msg = null;
if (!silent) {
qmsg = await e.Channel.SendMessage(":musical_note: **Searching...**");
qmsg = await e.Channel.SendMessage(":musical_note: `Searching...`");
sr.OnResolving += async () => {
await qmsg.Edit($":musical_note: **Resolving**... \"{query}\"");
await qmsg.Edit($":musical_note: `Resolving`... \"{query}\"");
};
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 () => {
await qmsg.Edit($":musical_note:**Queued** {sr.Title.TrimTo(55)}");
await qmsg.Edit($":musical_note:`Queued`{sr.FullPrettyName}");
};
}
sr.OnCompleted += async () => {
@ -243,17 +248,18 @@ namespace NadekoBot.Modules {
if (mc.SongQueue.Count == 0)
mc.Stop();
}
await e.Send($":musical_note:**Finished playing** {sr.Title.TrimTo(55)}");
await e.Send($":musical_note:`Finished`{sr.FullPrettyName}");
};
sr.OnStarted += async () => {
var msgTxt = $":musical_note:`Playing`{sr.FullPrettyName} `Vol: {(int)(player.Volume * 100)}%`";
if (msg == null)
await e.Send($":musical_note:**Playing ** {sr.Title.TrimTo(55)} **Volume:** {(int)(player.Volume * 100)}%");
await e.Send(msgTxt);
else
await msg.Edit($":musical_note:**Playing ** {sr.Title.TrimTo(55)} **Volume:** {(int)(player.Volume * 100)}%");
await msg.Edit(msgTxt);
qmsg?.Delete();
};
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) {
Console.WriteLine();

View File

@ -9,6 +9,7 @@ using Discord.Modules;
using Discord.Audio;
using NadekoBot.Extensions;
using System.Timers;
using System.Linq;
namespace NadekoBot {
class NadekoBot {
@ -49,7 +50,7 @@ namespace NadekoBot {
Console.WriteLine("Forwarding messages.");
}
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;
} else ParseActive = true;
@ -140,7 +141,7 @@ namespace NadekoBot {
static bool repliedRecently = false;
private static async void Client_MessageReceived(object sender, MessageEventArgs e) {
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
if (e.User.Id == 105309315895693312)
return; // FU