This commit is contained in:
Master Kwoth 2016-02-29 07:16:13 +01:00
parent 695aef54df
commit a1c374cc1d
2 changed files with 85 additions and 42 deletions

View File

@ -19,18 +19,23 @@ namespace NadekoBot.Commands {
public string Name { get; set; } public string Name { get; set; }
} }
private static List<CachedChampion> CachedChampionImages = new List<CachedChampion>(); private static Dictionary<string, CachedChampion> CachedChampionImages = new Dictionary<string, CachedChampion>();
private readonly object cacheLock = new object(); private readonly object cacheLock = new object();
private System.Timers.Timer clearTimer { get; } = new System.Timers.Timer();
public LoLCommands() : base() { public LoLCommands() : base() {
Task.Run(async () => { clearTimer.Interval = new TimeSpan(0, 10, 0).TotalMilliseconds;
while (true) { clearTimer.Start();
lock (cacheLock) { clearTimer.Elapsed += (s, e) => {
CachedChampionImages = CachedChampionImages.Where(cc => DateTime.Now - cc.AddedAt < new TimeSpan(1, 0, 0)).ToList(); try {
lock (cacheLock)
CachedChampionImages = CachedChampionImages
.Where(kvp => DateTime.Now - kvp.Value.AddedAt > new TimeSpan(1, 0, 0))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
} }
await Task.Delay(new TimeSpan(0, 10, 0)); catch { }
} };
});
} }
string[] trashTalk = new[] { "Better ban your counters. You are going to carry the game anyway.", string[] trashTalk = new[] { "Better ban your counters. You are going to carry the game anyway.",
@ -61,10 +66,11 @@ namespace NadekoBot.Commands {
try { try {
//get role //get role
string role = ResolvePos(e.GetArg("position")); string role = ResolvePos(e.GetArg("position"));
string resolvedRole = role;
var name = e.GetArg("champ").Replace(" ", ""); var name = e.GetArg("champ").Replace(" ", "");
CachedChampion champ; CachedChampion champ = null;
lock (cacheLock) { lock (cacheLock) {
champ = CachedChampionImages.Where(cc => cc.Name == name.ToLower()).FirstOrDefault(); CachedChampionImages.TryGetValue(name + "_" + resolvedRole, out champ);
} }
if (champ != null) { if (champ != null) {
Console.WriteLine("Sending lol image from cache."); Console.WriteLine("Sending lol image from cache.");
@ -89,6 +95,16 @@ namespace NadekoBot.Commands {
else { else {
data = allData[0]; data = allData[0];
role = allData[0]["role"].ToString(); 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(); //name = data["title"].ToString();
// get all possible roles, and "select" the shown one // get all possible roles, and "select" the shown one
@ -217,8 +233,8 @@ Assists: {general["assists"]} Ban: {general["banRate"]}%
smallImgSize)); smallImgSize));
} }
} }
var cachedChamp = new CachedChampion { AddedAt = DateTime.Now, ImageStream = img.ToStream(System.Drawing.Imaging.ImageFormat.Png), Name = name.ToLower() }; var cachedChamp = new CachedChampion { AddedAt = DateTime.Now, ImageStream = img.ToStream(System.Drawing.Imaging.ImageFormat.Png), Name = name.ToLower() + "_" + resolvedRole };
CachedChampionImages.Add(cachedChamp); CachedChampionImages.Add(cachedChamp.Name, cachedChamp);
await e.Channel.SendFile(data["title"] + "_stats.png", cachedChamp.ImageStream); await e.Channel.SendFile(data["title"] + "_stats.png", cachedChamp.ImageStream);
} }
catch (Exception ex) { catch (Exception ex) {

View File

@ -55,7 +55,8 @@ namespace NadekoBot.Modules {
try { try {
await usr.AddRoles(new Role[] { role }); await usr.AddRoles(new Role[] { role });
await e.Channel.SendMessage($"Successfully added role **{role.Name}** to user **{usr.Name}**"); 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"); await e.Channel.SendMessage("Failed to add roles. Most likely reason: Insufficient permissions.\n");
Console.WriteLine(ex.ToString()); Console.WriteLine(ex.ToString());
} }
@ -83,8 +84,10 @@ namespace NadekoBot.Modules {
try { try {
await usr.RemoveRoles(new Role[] { role }); await usr.RemoveRoles(new Role[] { role });
await e.Channel.SendMessage($"Successfully removed role **{role.Name}** from user **{usr.Name}**"); 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."); await e.Channel.SendMessage("Failed to remove roles. Most likely reason: Insufficient permissions.");
} }
}); });
@ -99,7 +102,8 @@ namespace NadekoBot.Modules {
try { try {
var r = await e.Server.CreateRole(e.GetArg("role_name")); var r = await e.Server.CreateRole(e.GetArg("role_name"));
await e.Channel.SendMessage($"Successfully created role **{r.Name}**."); await e.Channel.SendMessage($"Successfully created role **{r.Name}**.");
} catch (Exception ex) { }
catch (Exception ex) {
await e.Channel.SendMessage(":warning: Unspecified error."); await e.Channel.SendMessage(":warning: Unspecified error.");
} }
}); });
@ -138,7 +142,8 @@ namespace NadekoBot.Modules {
await role.Edit(color: new Color(red, green, blue)); await role.Edit(color: new Color(red, green, blue));
await e.Channel.SendMessage($"Role {role.Name}'s color has been changed."); 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."); await e.Channel.SendMessage(":warning: Unspecified error, please report this.");
Console.WriteLine($".rolecolor error: {ex}"); Console.WriteLine($".rolecolor error: {ex}");
} }
@ -170,7 +175,8 @@ namespace NadekoBot.Modules {
await usr.Server.Ban(usr); await usr.Server.Ban(usr);
await e.Channel.SendMessage("Banned user " + usr.Name + " Id: " + usr.Id); await e.Channel.SendMessage("Banned user " + usr.Name + " Id: " + usr.Id);
} }
} catch (Exception ex) { } }
catch (Exception ex) { }
}); });
cgb.CreateCommand(".ub").Alias(".unban") cgb.CreateCommand(".ub").Alias(".unban")
@ -183,7 +189,8 @@ namespace NadekoBot.Modules {
await usr.Server.Unban(usr); await usr.Server.Unban(usr);
await e.Channel.SendMessage("Unbanned user " + usr.Name + " Id: " + usr.Id); await e.Channel.SendMessage("Unbanned user " + usr.Name + " Id: " + usr.Id);
} }
} catch { } }
catch { }
}); });
cgb.CreateCommand(".k").Alias(".kick") cgb.CreateCommand(".k").Alias(".kick")
@ -196,7 +203,8 @@ namespace NadekoBot.Modules {
await e.Message.MentionedUsers.First().Kick(); await e.Message.MentionedUsers.First().Kick();
await e.Channel.SendMessage("Kicked user " + usr.Name + " Id: " + usr.Id); await e.Channel.SendMessage("Kicked user " + usr.Name + " Id: " + usr.Id);
} }
} catch { }
catch {
await e.Channel.SendMessage("No sufficient permissions."); await e.Channel.SendMessage("No sufficient permissions.");
} }
}); });
@ -215,7 +223,8 @@ namespace NadekoBot.Modules {
await u.Edit(isMuted: true); await u.Edit(isMuted: true);
} }
await e.Channel.SendMessage("Mute successful"); await e.Channel.SendMessage("Mute successful");
} catch { }
catch {
await e.Channel.SendMessage("I do not have permission to do that most likely."); 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 u.Edit(isMuted: false);
} }
await e.Channel.SendMessage("Unmute successful"); await e.Channel.SendMessage("Unmute successful");
} catch { }
catch {
await e.Channel.SendMessage("I do not have permission to do that most likely."); 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 u.Edit(isDeafened: true);
} }
await e.Channel.SendMessage("Deafen successful"); await e.Channel.SendMessage("Deafen successful");
} catch { }
catch {
await e.Channel.SendMessage("I do not have permission to do that most likely."); 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 u.Edit(isDeafened: false);
} }
await e.Channel.SendMessage("Undeafen successful"); await e.Channel.SendMessage("Undeafen successful");
} catch { }
catch {
await e.Channel.SendMessage("I do not have permission to do that most likely."); 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.Server.FindChannels(e.GetArg("channel_name"), ChannelType.Voice).FirstOrDefault()?.Delete();
await e.Channel.SendMessage($"Removed channel **{e.GetArg("channel_name")}**."); await e.Channel.SendMessage($"Removed channel **{e.GetArg("channel_name")}**.");
} }
} catch { }
catch {
await e.Channel.SendMessage("Insufficient permissions."); 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.Server.CreateChannel(e.GetArg("channel_name"), ChannelType.Voice);
await e.Channel.SendMessage($"Created voice channel **{e.GetArg("channel_name")}**."); await e.Channel.SendMessage($"Created voice channel **{e.GetArg("channel_name")}**.");
} }
} catch { }
catch {
await e.Channel.SendMessage("Insufficient permissions."); 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.Server.FindChannels(e.GetArg("channel_name"), ChannelType.Text).FirstOrDefault()?.Delete();
await e.Channel.SendMessage($"Removed text channel **{e.GetArg("channel_name")}**."); await e.Channel.SendMessage($"Removed text channel **{e.GetArg("channel_name")}**.");
} }
} catch { }
catch {
await e.Channel.SendMessage("Insufficient permissions."); 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.Server.CreateChannel(e.GetArg("channel_name"), ChannelType.Text);
await e.Channel.SendMessage($"Added text channel **{e.GetArg("channel_name")}**."); await e.Channel.SendMessage($"Added text channel **{e.GetArg("channel_name")}**.");
} }
} catch { }
catch {
await e.Channel.SendMessage("Insufficient permissions."); await e.Channel.SendMessage("Insufficient permissions.");
} }
}); });
@ -345,7 +361,8 @@ namespace NadekoBot.Modules {
try { try {
if (e.User.ServerPermissions.ManageChannels) if (e.User.ServerPermissions.ManageChannels)
await e.Channel.Edit(topic: e.GetArg("topic")); await e.Channel.Edit(topic: e.GetArg("topic"));
} catch { } }
catch { }
}); });
cgb.CreateCommand(".uid").Alias(".userid") cgb.CreateCommand(".uid").Alias(".userid")
@ -370,6 +387,14 @@ namespace NadekoBot.Modules {
.Do(async e => { .Do(async e => {
await e.Channel.SendMessage(await NadekoStats.Instance.GetStats()); 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") cgb.CreateCommand(".leaveall")
.Description("Nadeko leaves all servers **OWNER ONLY**") .Description("Nadeko leaves all servers **OWNER ONLY**")
@ -497,7 +522,8 @@ namespace NadekoBot.Modules {
if (commsUser != null) { if (commsUser != null) {
commsChannel = null; commsChannel = null;
await e.Channel.SendMessage("User for comms set."); await e.Channel.SendMessage("User for comms set.");
} else }
else
await e.Channel.SendMessage("No server specified or user."); await e.Channel.SendMessage("No server specified or user.");
}); });
@ -522,7 +548,8 @@ namespace NadekoBot.Modules {
if (commsChannel != null) { if (commsChannel != null) {
commsUser = null; commsUser = null;
await e.Channel.SendMessage("Server for comms set."); await e.Channel.SendMessage("Server for comms set.");
} else }
else
await e.Channel.SendMessage("No server specified or channel is invalid."); await e.Channel.SendMessage("No server specified or channel is invalid.");
}); });
@ -579,10 +606,9 @@ namespace NadekoBot.Modules {
cgb.CreateCommand(".unstuck") cgb.CreateCommand(".unstuck")
.Description("Clears the message queue. **OWNER ONLY**") .Description("Clears the message queue. **OWNER ONLY**")
.Do(async e => { .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly())
if (e.User.Id != NadekoBot.OwnerID) .Do(e => {
return; NadekoBot.client.MessageQueue.Clear();
await Task.Run(() => NadekoBot.client.MessageQueue.Clear());
}); });
cgb.CreateCommand(".donators") cgb.CreateCommand(".donators")
@ -615,7 +641,8 @@ namespace NadekoBot.Modules {
UserId = (long)e.User.Id UserId = (long)e.User.Id
}); });
e.Channel.SendMessage("Successfuly added a new donator. 👑"); e.Channel.SendMessage("Successfuly added a new donator. 👑");
} catch (Exception ex) { }
catch (Exception ex) {
Console.WriteLine(ex); Console.WriteLine(ex);
Console.WriteLine("---------------\nInner error:\n" + ex.InnerException); Console.WriteLine("---------------\nInner error:\n" + ex.InnerException);
} }