From 55221b7bf5c78d9f71045f9624388c7a0cebd1e0 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Mon, 5 Sep 2016 21:34:59 +0200 Subject: [PATCH] cleanup --- NadekoBot/Classes/ServerSpecificConfig.cs | 2 +- .../Administration/Commands/LogCommand.cs | 55 ++++++++++--------- .../ClashOfClans/ClashOfClansModule.cs | 5 +- .../Permissions/Classes/PermissionChecker.cs | 19 ++++--- .../Permissions/Classes/PermissionsHandler.cs | 12 ++-- 5 files changed, 52 insertions(+), 41 deletions(-) diff --git a/NadekoBot/Classes/ServerSpecificConfig.cs b/NadekoBot/Classes/ServerSpecificConfig.cs index c2680e4a..1642f75e 100644 --- a/NadekoBot/Classes/ServerSpecificConfig.cs +++ b/NadekoBot/Classes/ServerSpecificConfig.cs @@ -278,7 +278,7 @@ namespace NadekoBot.Classes } public bool Equals(StreamNotificationConfig other) => - this.Username.ToLower().Trim() == other.Username.ToLower().Trim() && + this.Username.ToUpperInvariant().Trim() == other.Username.ToUpperInvariant().Trim() && this.Type == other.Type && this.ServerId == other.ServerId; diff --git a/NadekoBot/Modules/Administration/Commands/LogCommand.cs b/NadekoBot/Modules/Administration/Commands/LogCommand.cs index 5956840c..87a0723e 100644 --- a/NadekoBot/Modules/Administration/Commands/LogCommand.cs +++ b/NadekoBot/Modules/Administration/Commands/LogCommand.cs @@ -56,33 +56,36 @@ namespace NadekoBot.Modules.Administration.Commands // start the userpresence queue - NadekoBot.OnReady += () => Task.Run(async () => - { - while (true) - { - var toSend = new Dictionary(); - //take everything from the queue and merge the messages which are going to the same channel - KeyValuePair item; - while (voicePresenceUpdates.TryTake(out item)) - { - if (toSend.ContainsKey(item.Key)) - { - toSend[item.Key] = toSend[item.Key] + Environment.NewLine + item.Value; - } - else - { - toSend.Add(item.Key, item.Value); - } - } - //send merged messages to each channel - foreach (var k in toSend) - { - try { await k.Key.SendMessage(Environment.NewLine + k.Value).ConfigureAwait(false); } catch { } - } + NadekoBot.OnReady += () => + { + Task.Run(async () => + { + while (true) + { + var toSend = new Dictionary(); + //take everything from the queue and merge the messages which are going to the same channel + KeyValuePair item; + while (voicePresenceUpdates.TryTake(out item)) + { + if (toSend.ContainsKey(item.Key)) + { + toSend[item.Key] = toSend[item.Key] + Environment.NewLine + item.Value; + } + else + { + toSend.Add(item.Key, item.Value); + } + } + //send merged messages to each channel + foreach (var k in toSend) + { + try { await k.Key.SendMessage(Environment.NewLine + k.Value).ConfigureAwait(false); } catch { } + } - await Task.Delay(5000); - } - }); + await Task.Delay(5000); + } + }); + }; } private async void ChannelUpdated(object sender, ChannelUpdatedEventArgs e) diff --git a/NadekoBot/Modules/ClashOfClans/ClashOfClansModule.cs b/NadekoBot/Modules/ClashOfClans/ClashOfClansModule.cs index e14e48b4..07db4f98 100644 --- a/NadekoBot/Modules/ClashOfClans/ClashOfClansModule.cs +++ b/NadekoBot/Modules/ClashOfClans/ClashOfClansModule.cs @@ -23,7 +23,9 @@ namespace NadekoBot.Modules.ClashOfClans public ClashOfClansModule() { - NadekoBot.OnReady += () => Task.Run(async () => + NadekoBot.OnReady += () => + { + Task.Run(async () => { if (File.Exists("data/clashofclans/wars.json")) { @@ -109,6 +111,7 @@ namespace NadekoBot.Modules.ClashOfClans await Task.Delay(5000); } }); + }; } private static void Save() diff --git a/NadekoBot/Modules/Permissions/Classes/PermissionChecker.cs b/NadekoBot/Modules/Permissions/Classes/PermissionChecker.cs index 0c6aa79b..53c874c1 100644 --- a/NadekoBot/Modules/Permissions/Classes/PermissionChecker.cs +++ b/NadekoBot/Modules/Permissions/Classes/PermissionChecker.cs @@ -4,7 +4,6 @@ using Discord.Commands.Permissions; using NadekoBot.Classes.JSONModels; using System; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Threading.Tasks; namespace NadekoBot.Modules.Permissions.Classes @@ -14,10 +13,8 @@ namespace NadekoBot.Modules.Permissions.Classes { public static PermissionChecker Instance { get; } = new PermissionChecker(); - //key - sid:command - //value - userid private ConcurrentDictionary commandCooldowns = new ConcurrentDictionary(); - private HashSet timeBlackList { get; } = new HashSet(); + private ConcurrentDictionary timeBlackList { get; } = new ConcurrentDictionary(); static PermissionChecker() { } private PermissionChecker() @@ -26,7 +23,6 @@ namespace NadekoBot.Modules.Permissions.Classes { while (true) { - //blacklist is cleared every 1.00 seconds. That is the most time anyone will be blocked await Task.Delay(1000).ConfigureAwait(false); timeBlackList.Clear(); } @@ -43,21 +39,28 @@ namespace NadekoBot.Modules.Permissions.Classes if (channel.IsPrivate || channel.Server == null) return command.Category == "Help"; + if (user == null) + return false; + if (ConfigHandler.IsUserBlacklisted(user.Id) || (!channel.IsPrivate && (ConfigHandler.IsServerBlacklisted(channel.Server.Id) || ConfigHandler.IsChannelBlacklisted(channel.Id)))) { return false; } - if (timeBlackList.Contains(user.Id)) - return false; + try + { + if (timeBlackList.ContainsKey(user.Id)) + return false; + } + catch { return false; } if (!channel.IsPrivate && !channel.Server.CurrentUser.GetPermissions(channel).SendMessages) { return false; } - timeBlackList.Add(user.Id); + timeBlackList.TryAdd(user.Id, true); ServerPermissions perms; PermissionsHandler.PermissionsDict.TryGetValue(user.Server.Id, out perms); diff --git a/NadekoBot/Modules/Permissions/Classes/PermissionsHandler.cs b/NadekoBot/Modules/Permissions/Classes/PermissionsHandler.cs index d7c9e784..9c00c7c6 100644 --- a/NadekoBot/Modules/Permissions/Classes/PermissionsHandler.cs +++ b/NadekoBot/Modules/Permissions/Classes/PermissionsHandler.cs @@ -22,7 +22,7 @@ namespace NadekoBot.Modules.Permissions.Classes } - public static void Initialize() + public static Task Initialize() => Task.Run(() => { Console.WriteLine("Reading from the permission files."); Directory.CreateDirectory("data/permissions"); @@ -39,7 +39,7 @@ namespace NadekoBot.Modules.Permissions.Classes catch { } } Console.WriteLine("Permission initialization complete."); - } + }); internal static Permissions GetRolePermissionsById(Server server, ulong id) { @@ -157,7 +157,7 @@ namespace NadekoBot.Modules.Permissions.Classes Newtonsoft.Json.JsonConvert.SerializeObject(serverPerms, Newtonsoft.Json.Formatting.Indented)); }); - public static Task WriteToJson() => Task.Run(() => + public static Task WriteToJson() => Task.Run(() => { Directory.CreateDirectory("data/permissions/"); foreach (var kvp in PermissionsDict) @@ -428,11 +428,13 @@ namespace NadekoBot.Modules.Permissions.Classes { var serverPerms = PermissionsDict.GetOrAdd(server.Id, new ServerPermissions(server.Id, server.Name)); - if (value == 0) { + if (value == 0) + { int throwaway; serverPerms.CommandCooldowns.TryRemove(commandName, out throwaway); } - else { + else + { serverPerms.CommandCooldowns.AddOrUpdate(commandName, value, (str, v) => value); }