From a1c374cc1db6d3739c19f142f50b5e40d9f00cfb Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Mon, 29 Feb 2016 07:16:13 +0100 Subject: [PATCH] woop --- NadekoBot/Commands/LoLCommands.cs | 46 ++++++++++------ NadekoBot/Modules/Administration.cs | 81 +++++++++++++++++++---------- 2 files changed, 85 insertions(+), 42 deletions(-) diff --git a/NadekoBot/Commands/LoLCommands.cs b/NadekoBot/Commands/LoLCommands.cs index ccf9c962..59261a2a 100644 --- a/NadekoBot/Commands/LoLCommands.cs +++ b/NadekoBot/Commands/LoLCommands.cs @@ -19,18 +19,23 @@ namespace NadekoBot.Commands { public string Name { get; set; } } - private static List CachedChampionImages = new List(); + private static Dictionary CachedChampionImages = new Dictionary(); private readonly object cacheLock = new object(); + + private System.Timers.Timer clearTimer { get; } = new System.Timers.Timer(); public LoLCommands() : base() { - Task.Run(async () => { - while (true) { - lock (cacheLock) { - CachedChampionImages = CachedChampionImages.Where(cc => DateTime.Now - cc.AddedAt < new TimeSpan(1, 0, 0)).ToList(); - } - await Task.Delay(new TimeSpan(0, 10, 0)); + clearTimer.Interval = new TimeSpan(0, 10, 0).TotalMilliseconds; + clearTimer.Start(); + clearTimer.Elapsed += (s, e) => { + try { + lock (cacheLock) + CachedChampionImages = CachedChampionImages + .Where(kvp => DateTime.Now - kvp.Value.AddedAt > new TimeSpan(1, 0, 0)) + .ToDictionary(kvp => kvp.Key, kvp => kvp.Value); } - }); + catch { } + }; } string[] trashTalk = new[] { "Better ban your counters. You are going to carry the game anyway.", @@ -61,10 +66,11 @@ namespace NadekoBot.Commands { try { //get role string role = ResolvePos(e.GetArg("position")); + string resolvedRole = role; var name = e.GetArg("champ").Replace(" ", ""); - CachedChampion champ; + CachedChampion champ = null; lock (cacheLock) { - champ = CachedChampionImages.Where(cc => cc.Name == name.ToLower()).FirstOrDefault(); + CachedChampionImages.TryGetValue(name + "_" + resolvedRole, out champ); } if (champ != null) { Console.WriteLine("Sending lol image from cache."); @@ -89,6 +95,16 @@ namespace NadekoBot.Commands { else { data = allData[0]; role = allData[0]["role"].ToString(); + resolvedRole = ResolvePos(role); + } + lock (cacheLock) { + CachedChampionImages.TryGetValue(name + "_" + resolvedRole, out champ); + } + if (champ != null) { + Console.WriteLine("Sending lol image from cache."); + champ.ImageStream.Position = 0; + await e.Channel.SendFile("champ.png", champ.ImageStream); + return; } //name = data["title"].ToString(); // get all possible roles, and "select" the shown one @@ -144,7 +160,7 @@ namespace NadekoBot.Commands { var smallFont = new Font("Monaco", 7, FontStyle.Regular); //draw champ image var champName = data["key"].ToString().Replace(" ", ""); - + g.DrawImage(GetImage(champName), new Rectangle(margin, margin, imageSize, imageSize)); //draw champ name if (champName == "MonkeyKing") @@ -217,8 +233,8 @@ Assists: {general["assists"]} Ban: {general["banRate"]}% smallImgSize)); } } - var cachedChamp = new CachedChampion { AddedAt = DateTime.Now, ImageStream = img.ToStream(System.Drawing.Imaging.ImageFormat.Png), Name = name.ToLower() }; - CachedChampionImages.Add(cachedChamp); + var cachedChamp = new CachedChampion { AddedAt = DateTime.Now, ImageStream = img.ToStream(System.Drawing.Imaging.ImageFormat.Png), Name = name.ToLower() + "_" + resolvedRole }; + CachedChampionImages.Add(cachedChamp.Name, cachedChamp); await e.Channel.SendFile(data["title"] + "_stats.png", cachedChamp.ImageStream); } catch (Exception ex) { @@ -230,7 +246,7 @@ Assists: {general["assists"]} Ban: {general["banRate"]}% cgb.CreateCommand("~lolban") .Description("Shows top 6 banned champions ordered by ban rate. Ban these champions and you will be Plat 5 in no time.") .Do(async e => { - + int showCount = 6; //http://api.champion.gg/stats/champs/mostBanned?api_key=YOUR_API_TOKEN&page=1&limit=2 try { @@ -248,7 +264,7 @@ Assists: {general["assists"]} Ban: {general["banRate"]}% sb.Append($"`{i + 1}.` **{data[i]["name"]}** "); //sb.AppendLine($" ({data[i]["general"]["banRate"]}%)"); } - + await e.Channel.SendMessage(sb.ToString()); } catch (Exception ex) { diff --git a/NadekoBot/Modules/Administration.cs b/NadekoBot/Modules/Administration.cs index d1300e39..7b70eecd 100644 --- a/NadekoBot/Modules/Administration.cs +++ b/NadekoBot/Modules/Administration.cs @@ -26,7 +26,7 @@ namespace NadekoBot.Modules { public override void Install(ModuleManager manager) { manager.CreateCommands("", cgb => { - + cgb.AddCheck(Classes.Permissions.PermissionChecker.Instance); var client = manager.Client; @@ -55,7 +55,8 @@ namespace NadekoBot.Modules { try { await usr.AddRoles(new Role[] { role }); await e.Channel.SendMessage($"Successfully added role **{role.Name}** to user **{usr.Name}**"); - } catch (Exception ex) { + } + catch (Exception ex) { await e.Channel.SendMessage("Failed to add roles. Most likely reason: Insufficient permissions.\n"); Console.WriteLine(ex.ToString()); } @@ -83,8 +84,10 @@ namespace NadekoBot.Modules { try { await usr.RemoveRoles(new Role[] { role }); await e.Channel.SendMessage($"Successfully removed role **{role.Name}** from user **{usr.Name}**"); - } catch (InvalidOperationException) { - } catch { + } + catch (InvalidOperationException) { + } + catch { await e.Channel.SendMessage("Failed to remove roles. Most likely reason: Insufficient permissions."); } }); @@ -99,7 +102,8 @@ namespace NadekoBot.Modules { try { var r = await e.Server.CreateRole(e.GetArg("role_name")); await e.Channel.SendMessage($"Successfully created role **{r.Name}**."); - } catch (Exception ex) { + } + catch (Exception ex) { await e.Channel.SendMessage(":warning: Unspecified error."); } }); @@ -138,7 +142,8 @@ namespace NadekoBot.Modules { await role.Edit(color: new Color(red, green, blue)); await e.Channel.SendMessage($"Role {role.Name}'s color has been changed."); - } catch (Exception ex) { + } + catch (Exception ex) { await e.Channel.SendMessage(":warning: Unspecified error, please report this."); Console.WriteLine($".rolecolor error: {ex}"); } @@ -170,7 +175,8 @@ namespace NadekoBot.Modules { await usr.Server.Ban(usr); await e.Channel.SendMessage("Banned user " + usr.Name + " Id: " + usr.Id); } - } catch (Exception ex) { } + } + catch (Exception ex) { } }); cgb.CreateCommand(".ub").Alias(".unban") @@ -183,7 +189,8 @@ namespace NadekoBot.Modules { await usr.Server.Unban(usr); await e.Channel.SendMessage("Unbanned user " + usr.Name + " Id: " + usr.Id); } - } catch { } + } + catch { } }); cgb.CreateCommand(".k").Alias(".kick") @@ -196,7 +203,8 @@ namespace NadekoBot.Modules { await e.Message.MentionedUsers.First().Kick(); await e.Channel.SendMessage("Kicked user " + usr.Name + " Id: " + usr.Id); } - } catch { + } + catch { await e.Channel.SendMessage("No sufficient permissions."); } }); @@ -215,7 +223,8 @@ namespace NadekoBot.Modules { await u.Edit(isMuted: true); } await e.Channel.SendMessage("Mute successful"); - } catch { + } + catch { await e.Channel.SendMessage("I do not have permission to do that most likely."); } }); @@ -235,7 +244,8 @@ namespace NadekoBot.Modules { await u.Edit(isMuted: false); } await e.Channel.SendMessage("Unmute successful"); - } catch { + } + catch { await e.Channel.SendMessage("I do not have permission to do that most likely."); } }); @@ -256,7 +266,8 @@ namespace NadekoBot.Modules { await u.Edit(isDeafened: true); } await e.Channel.SendMessage("Deafen successful"); - } catch { + } + catch { await e.Channel.SendMessage("I do not have permission to do that most likely."); } }); @@ -277,7 +288,8 @@ namespace NadekoBot.Modules { await u.Edit(isDeafened: false); } await e.Channel.SendMessage("Undeafen successful"); - } catch { + } + catch { await e.Channel.SendMessage("I do not have permission to do that most likely."); } }); @@ -291,7 +303,8 @@ namespace NadekoBot.Modules { await e.Server.FindChannels(e.GetArg("channel_name"), ChannelType.Voice).FirstOrDefault()?.Delete(); await e.Channel.SendMessage($"Removed channel **{e.GetArg("channel_name")}**."); } - } catch { + } + catch { await e.Channel.SendMessage("Insufficient permissions."); } }); @@ -305,7 +318,8 @@ namespace NadekoBot.Modules { await e.Server.CreateChannel(e.GetArg("channel_name"), ChannelType.Voice); await e.Channel.SendMessage($"Created voice channel **{e.GetArg("channel_name")}**."); } - } catch { + } + catch { await e.Channel.SendMessage("Insufficient permissions."); } }); @@ -319,7 +333,8 @@ namespace NadekoBot.Modules { await e.Server.FindChannels(e.GetArg("channel_name"), ChannelType.Text).FirstOrDefault()?.Delete(); await e.Channel.SendMessage($"Removed text channel **{e.GetArg("channel_name")}**."); } - } catch { + } + catch { await e.Channel.SendMessage("Insufficient permissions."); } }); @@ -333,7 +348,8 @@ namespace NadekoBot.Modules { await e.Server.CreateChannel(e.GetArg("channel_name"), ChannelType.Text); await e.Channel.SendMessage($"Added text channel **{e.GetArg("channel_name")}**."); } - } catch { + } + catch { await e.Channel.SendMessage("Insufficient permissions."); } }); @@ -345,7 +361,8 @@ namespace NadekoBot.Modules { try { if (e.User.ServerPermissions.ManageChannels) await e.Channel.Edit(topic: e.GetArg("topic")); - } catch { } + } + catch { } }); cgb.CreateCommand(".uid").Alias(".userid") @@ -370,6 +387,14 @@ namespace NadekoBot.Modules { .Do(async e => { await e.Channel.SendMessage(await NadekoStats.Instance.GetStats()); }); + cgb.CreateCommand(".heap") + .Description("Shows allocated memory - OWNER ONLY") + .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly()) + .Do(async e => { + var heap = Task.Run(() => NadekoStats.Instance.Heap()); + await e.Channel.SendMessage($"`Heap Size:` {heap}"); + }); + /* cgb.CreateCommand(".leaveall") .Description("Nadeko leaves all servers **OWNER ONLY**") @@ -409,7 +434,7 @@ namespace NadekoBot.Modules { ConcurrentDictionary clearDictionary = new ConcurrentDictionary(); cgb.CreateCommand(".clr") .Description("Clears some of Nadeko's (or some other user's if supplied) messages from the current channel.\n**Usage**: .clr @X") - .Parameter("user",ParameterType.Unparsed) + .Parameter("user", ParameterType.Unparsed) .Do(async e => { var usrId = NadekoBot.client.CurrentUser.Id; if (!string.IsNullOrWhiteSpace(e.GetArg("user")) && e.User.ServerPermissions.ManageMessages) { @@ -426,7 +451,7 @@ namespace NadekoBot.Modules { catch { } await Task.Delay(200); } - + }); }); @@ -497,7 +522,8 @@ namespace NadekoBot.Modules { if (commsUser != null) { commsChannel = null; await e.Channel.SendMessage("User for comms set."); - } else + } + else await e.Channel.SendMessage("No server specified or user."); }); @@ -522,7 +548,8 @@ namespace NadekoBot.Modules { if (commsChannel != null) { commsUser = null; await e.Channel.SendMessage("Server for comms set."); - } else + } + else await e.Channel.SendMessage("No server specified or channel is invalid."); }); @@ -579,10 +606,9 @@ namespace NadekoBot.Modules { cgb.CreateCommand(".unstuck") .Description("Clears the message queue. **OWNER ONLY**") - .Do(async e => { - if (e.User.Id != NadekoBot.OwnerID) - return; - await Task.Run(() => NadekoBot.client.MessageQueue.Clear()); + .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly()) + .Do(e => { + NadekoBot.client.MessageQueue.Clear(); }); cgb.CreateCommand(".donators") @@ -615,7 +641,8 @@ namespace NadekoBot.Modules { UserId = (long)e.User.Id }); e.Channel.SendMessage("Successfuly added a new donator. 👑"); - } catch (Exception ex) { + } + catch (Exception ex) { Console.WriteLine(ex); Console.WriteLine("---------------\nInner error:\n" + ex.InnerException); }