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 str;
return string.Join("", str.Take(num - 3)) + "..."; 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> /// <summary>
/// Sends a message to the channel from which this command is called. /// Sends a message to the channel from which this command is called.

View File

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

View File

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

View File

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

View File

@ -14,11 +14,9 @@ namespace NadekoBot.Modules {
public static ConcurrentDictionary<Server, MusicControls> musicPlayers = new ConcurrentDictionary<Server, MusicControls>(); public static ConcurrentDictionary<Server, MusicControls> musicPlayers = new ConcurrentDictionary<Server, MusicControls>();
internal static string GetMusicStats() { internal static string GetMusicStats() {
var servers = 0;
var queued = 0;
var stats = musicPlayers.Where(kvp => kvp.Value?.SongQueue.Count > 0 || kvp.Value?.CurrentSong != null); var stats = musicPlayers.Where(kvp => kvp.Value?.SongQueue.Count > 0 || kvp.Value?.CurrentSong != null);
int cnt;
return $"Playing {stats.Count()} songs, {stats.Sum(kvp => kvp.Value?.SongQueue?.Count ?? 0)} queued."; return $"Playing {cnt = stats.Count()} songs".SnPl(cnt)+$", {stats.Sum(kvp => kvp.Value?.SongQueue?.Count ?? 0)} queued.";
} }
public Music() : base() { public Music() : base() {
@ -164,6 +162,25 @@ 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;
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,6 +225,7 @@ namespace NadekoBot.Modules
Console.WriteLine("ERROR: No google api key found. Playing `Never gonna give you up`."); Console.WriteLine("ERROR: No google api key found. Playing `Never gonna give you up`.");
return @"https://www.youtube.com/watch?v=dQw4w9WgXcQ"; return @"https://www.youtube.com/watch?v=dQw4w9WgXcQ";
} }
try {
WebRequest wr = WebRequest.Create("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=" + Uri.EscapeDataString(v) + "&key=" + NadekoBot.GoogleAPIKey); 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());
@ -232,6 +233,9 @@ namespace NadekoBot.Modules
dynamic obj = JObject.Parse(sr.ReadToEnd()); dynamic obj = JObject.Parse(sr.ReadToEnd());
string toReturn = "http://www.youtube.com/watch?v=" + obj.items[0].id.videoId.ToString(); string toReturn = "http://www.youtube.com/watch?v=" + obj.items[0].id.videoId.ToString();
return toReturn; return toReturn;
} catch (Exception) {
return string.Empty;
}
} }
public string GetDanbooruImageLink(string tag) { public string GetDanbooruImageLink(string tag) {

Binary file not shown.