new libs, crashfix, !m setgame, ripyear...

also better strings for music (plural and singular)
This commit is contained in:
Master Kwoth 2016-01-31 09:22:47 +01:00
parent 73309c723e
commit e71e390cca
10 changed files with 64 additions and 35 deletions

View File

@ -44,6 +44,20 @@ namespace NadekoBot.Extensions {
return str;
return string.Join("", str.Take(num - 3)) + "...";
}
/// <summary>
/// Removes trailing S or ES (if specified) on the given string if the num is 1
/// </summary>
/// <param name="str"></param>
/// <param name="num"></param>
/// <param name="es"></param>
/// <returns>String with the correct singular/plural form</returns>
public static string SnPl(this string str, int? num,bool es = false) {
if (str == null)
throw new ArgumentNullException(nameof(str));
if (num == null)
throw new ArgumentNullException(nameof(num));
return num == 1 ? str.Remove(str.Length - 1, es ? 2 : 1) : str;
}
/// <summary>
/// Sends a message to the channel from which this command is called.

View File

@ -24,21 +24,17 @@ namespace NadekoBot.Classes.Music {
public MusicControls() {
Task.Run(async () => {
while (!Stopped) {
try {
lock (_voiceLock) {
if (CurrentSong == null) {
if (SongQueue.Count > 0)
LoadNextSong();
} else if (CurrentSong.State == StreamState.Completed || NextSong) {
NextSong = false;
lock (_voiceLock) {
if (CurrentSong == null) {
if (SongQueue.Count > 0)
LoadNextSong();
}
} else if (CurrentSong.State == StreamState.Completed || NextSong) {
NextSong = false;
LoadNextSong();
}
} catch (Exception e) {
Console.WriteLine("Bug in music task run. " + e);
}
await Task.Delay(500);
await Task.Delay(1000);
}
});
}
@ -74,7 +70,7 @@ namespace NadekoBot.Classes.Music {
Stopped = true;
foreach (var kvp in SongQueue) {
if(kvp != null)
kvp.Cancel();
kvp.Stop();
}
SongQueue.Clear();
CurrentSong?.Stop();

View File

@ -239,7 +239,7 @@ namespace NadekoBot.Classes.Music {
// prebuffering wait stuff start
int bufferAttempts = 0;
int waitPerAttempt = 500;
while (!prebufferingComplete && bufferAttempts++ < 10) {
while (!prebufferingComplete && bufferAttempts++ < 15) {
await Task.Delay(waitPerAttempt);
}
if (prebufferingComplete) {
@ -299,8 +299,9 @@ 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

@ -222,17 +222,14 @@ namespace NadekoBot.Modules {
});
cgb.CreateCommand("rip")
.Description("Shows a grave image.Optional parameter [@X] instructs her to put X's name on the grave.\n**Usage**: @NadekoBot rip [@X]")
.Parameter("user", ParameterType.Unparsed)
.Description("Shows a grave image of someone with a start year\n**Usage**: @NadekoBot rip @Someone 2000")
.Parameter("user", ParameterType.Optional)
.Parameter("year", ParameterType.Optional)
.Do(async e => {
var usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault();
string text = "";
if (usr == null) {
text = e.GetArg("user");
} else {
text = usr.Name;
}
await e.Channel.SendFile("ripzor_m8.png", RipName(text));
text = usr?.Name;
await e.Channel.SendFile("ripzor_m8.png", RipName(text, e.GetArg("year") == "" ? null : e.GetArg("year")));
});
cgb.CreateCommand("j")
@ -324,7 +321,7 @@ namespace NadekoBot.Modules {
.Do(async e => {
string str = "Bye";
foreach (var u in e.Message.MentionedUsers) {
if(u.Id != NadekoBot.client.CurrentUser.Id)
if (u.Id != NadekoBot.client.CurrentUser.Id)
str += " " + u.Mention;
}
await e.Send(str);
@ -406,7 +403,7 @@ namespace NadekoBot.Modules {
});
}
public Stream RipName(string name) {
public Stream RipName(string name, string year = null) {
Bitmap bm = Resources.rip;
int offset = name.Length * 5;
@ -420,7 +417,7 @@ namespace NadekoBot.Modules {
//TODO use measure string
Graphics g = Graphics.FromImage(bm);
g.DrawString(name, new Font("Comic Sans MS", fontSize, FontStyle.Bold), Brushes.Black, 100 - offset, 200);
g.DrawString("? - " + DateTime.Now.Year, new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, 80, 235);
g.DrawString((year == null ? "?" : year) + " - " + DateTime.Now.Year, new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, 80, 235);
g.Flush();
g.Dispose();

View File

@ -14,11 +14,9 @@ namespace NadekoBot.Modules {
public static ConcurrentDictionary<Server, MusicControls> musicPlayers = new ConcurrentDictionary<Server, MusicControls>();
internal static string GetMusicStats() {
var servers = 0;
var queued = 0;
var stats = musicPlayers.Where(kvp => kvp.Value?.SongQueue.Count > 0 || kvp.Value?.CurrentSong != null);
return $"Playing {stats.Count()} songs, {stats.Sum(kvp => kvp.Value?.SongQueue?.Count ?? 0)} queued.";
int cnt;
return $"Playing {cnt = stats.Count()} songs".SnPl(cnt)+$", {stats.Sum(kvp => kvp.Value?.SongQueue?.Count ?? 0)} queued.";
}
public Music() : base() {
@ -164,6 +162,25 @@ namespace NadekoBot.Modules {
player.SongQueue.Shuffle();
await e.Send(":musical_note: Songs shuffled!");
});
bool setgameEnabled = false;
Timer setgameTimer = new Timer();
setgameTimer.Interval = 20000;
setgameTimer.Elapsed += (s, e) => {
int num = musicPlayers.Count;
NadekoBot.client.SetGame($"{num} songs".SnPl(num) + $", {musicPlayers.Sum(kvp => kvp.Value.SongQueue.Count())} queued");
};
setgameTimer.Start();
cgb.CreateCommand("setgame")
.Description("Sets the game of the bot to the number of songs playing.**Owner only**")
.Do(e => {
if (NadekoBot.OwnerID != e.User.Id)
return;
setgameEnabled = !setgameEnabled;
if (setgameEnabled)
setgameTimer.Start();
else
setgameTimer.Stop();
});
});
}
}

View File

@ -225,13 +225,17 @@ namespace NadekoBot.Modules
Console.WriteLine("ERROR: No google api key found. Playing `Never gonna give you up`.");
return @"https://www.youtube.com/watch?v=dQw4w9WgXcQ";
}
WebRequest wr = WebRequest.Create("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=" + Uri.EscapeDataString(v) + "&key=" + NadekoBot.GoogleAPIKey);
try {
WebRequest wr = WebRequest.Create("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=" + Uri.EscapeDataString(v) + "&key=" + NadekoBot.GoogleAPIKey);
var sr = new StreamReader(wr.GetResponse().GetResponseStream());
var sr = new StreamReader(wr.GetResponse().GetResponseStream());
dynamic obj = JObject.Parse(sr.ReadToEnd());
string toReturn = "http://www.youtube.com/watch?v=" + obj.items[0].id.videoId.ToString();
return toReturn;
dynamic obj = JObject.Parse(sr.ReadToEnd());
string toReturn = "http://www.youtube.com/watch?v=" + obj.items[0].id.videoId.ToString();
return toReturn;
} catch (Exception) {
return string.Empty;
}
}
public string GetDanbooruImageLink(string tag) {

Binary file not shown.