From fd729e553d8f4c041c9f015de98a454f6f69a511 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Mon, 10 Oct 2016 06:38:20 +0200 Subject: [PATCH] Project now magically works --- src/NadekoBot/DataStructures/ConcurrentHashSet.cs | 13 +++++++++++++ .../Modules/CustomReactions/CustomReactions.cs | 7 ++----- .../Permissions/Commands/BlacklistCommands.cs | 4 +--- .../Modules/Permissions/Commands/CmdCdsCommands.cs | 2 +- .../Modules/Permissions/Commands/FilterCommands.cs | 10 +++++----- .../Searches/Commands/StreamNotificationCommands.cs | 2 +- src/NadekoBot/Services/Database/NadekoContext.cs | 7 ++++--- src/NadekoBot/_Extensions/Extensions.cs | 8 ++++++++ 8 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/NadekoBot/DataStructures/ConcurrentHashSet.cs b/src/NadekoBot/DataStructures/ConcurrentHashSet.cs index b29a8842..019f7a87 100644 --- a/src/NadekoBot/DataStructures/ConcurrentHashSet.cs +++ b/src/NadekoBot/DataStructures/ConcurrentHashSet.cs @@ -6,6 +6,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Threading; namespace System.Collections.Concurrent @@ -672,6 +673,18 @@ namespace System.Collections.Concurrent } } + public int RemoveWhere(Func predicate) + { + var elems = this.Where(predicate); + var removed = 0; + foreach (var elem in elems) + { + if (this.TryRemove(elem)) + removed++; + } + return removed; + } + private void AcquireAllLocks(ref int locksAcquired) { // First, acquire lock 0 diff --git a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs index 833b7881..a8c71001 100644 --- a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs +++ b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs @@ -150,16 +150,13 @@ namespace NadekoBot.Modules.CustomReactions if (toDelete.GuildId == null && channel == null) { uow.CustomReactions.Remove(toDelete); - var toRemove = GlobalReactions.FirstOrDefault(cr => cr.Id == toDelete.Id); - GlobalReactions.TryRemove(toRemove); + GlobalReactions.RemoveWhere(cr => cr.Id == toDelete.Id); success = true; } else if (toDelete.GuildId != null && channel?.Guild.Id == toDelete.GuildId) { uow.CustomReactions.Remove(toDelete); - var crs = GuildReactions.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet()); - var toRemove = crs.FirstOrDefault(cr => cr.Id == toDelete.Id); - + GuildReactions.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet()).RemoveWhere(cr=>cr.Id == toDelete.Id); success = true; } if(success) diff --git a/src/NadekoBot/Modules/Permissions/Commands/BlacklistCommands.cs b/src/NadekoBot/Modules/Permissions/Commands/BlacklistCommands.cs index 6f5b3862..e538e2a6 100644 --- a/src/NadekoBot/Modules/Permissions/Commands/BlacklistCommands.cs +++ b/src/NadekoBot/Modules/Permissions/Commands/BlacklistCommands.cs @@ -76,9 +76,7 @@ namespace NadekoBot.Modules.Permissions else { uow.BotConfig.GetOrCreate().Blacklist.RemoveWhere(bi => bi.ItemId == id && bi.Type == type); - var toRemove = BlacklistedItems.FirstOrDefault(bi => bi.ItemId == id && bi.Type == type); - if (toRemove != null) - BlacklistedItems.TryRemove(toRemove); + BlacklistedItems.RemoveWhere(bi => bi.ItemId == id && bi.Type == type); } await uow.CompleteAsync().ConfigureAwait(false); } diff --git a/src/NadekoBot/Modules/Permissions/Commands/CmdCdsCommands.cs b/src/NadekoBot/Modules/Permissions/Commands/CmdCdsCommands.cs index 7deaaa1e..6f5b2b4a 100644 --- a/src/NadekoBot/Modules/Permissions/Commands/CmdCdsCommands.cs +++ b/src/NadekoBot/Modules/Permissions/Commands/CmdCdsCommands.cs @@ -34,7 +34,7 @@ namespace NadekoBot.Modules.Permissions using (var uow = DbHandler.UnitOfWork()) { var configs = uow.GuildConfigs.GetAll(); - commandCooldowns = new ConcurrentDictionary>(configs.ToDictionary(k => k.GuildId, v => v.CommandCooldowns)); + commandCooldowns = new ConcurrentDictionary>(configs.ToDictionary(k => k.GuildId, v => new ConcurrentHashSet(v.CommandCooldowns))); } } [NadekoCommand, Usage, Description, Aliases] diff --git a/src/NadekoBot/Modules/Permissions/Commands/FilterCommands.cs b/src/NadekoBot/Modules/Permissions/Commands/FilterCommands.cs index b5eaa5df..a6894929 100644 --- a/src/NadekoBot/Modules/Permissions/Commands/FilterCommands.cs +++ b/src/NadekoBot/Modules/Permissions/Commands/FilterCommands.cs @@ -84,7 +84,7 @@ namespace NadekoBot.Modules.Permissions } else { - InviteFilteringServers.Remove(channel.Guild.Id); + InviteFilteringServers.TryRemove(channel.Guild.Id); await channel.SendMessageAsync("`Invite filtering disabled on this server.`").ConfigureAwait(false); } } @@ -117,7 +117,7 @@ namespace NadekoBot.Modules.Permissions } else { - InviteFilteringChannels.Remove(channel.Id); + InviteFilteringChannels.TryRemove(channel.Id); await channel.SendMessageAsync("`Invite filtering disabled on this channel.`").ConfigureAwait(false); } } @@ -143,7 +143,7 @@ namespace NadekoBot.Modules.Permissions } else { - WordFilteringServers.Remove(channel.Guild.Id); + WordFilteringServers.TryRemove(channel.Guild.Id); await channel.SendMessageAsync("`Word filtering disabled on this server.`").ConfigureAwait(false); } } @@ -176,7 +176,7 @@ namespace NadekoBot.Modules.Permissions } else { - WordFilteringChannels.Remove(channel.Id); + WordFilteringChannels.TryRemove(channel.Id); await channel.SendMessageAsync("`Word filtering disabled on this channel.`").ConfigureAwait(false); } } @@ -215,7 +215,7 @@ namespace NadekoBot.Modules.Permissions } else { - filteredWords.Remove(word); + filteredWords.TryRemove(word); await channel.SendMessageAsync($"Word `{word}` removed from the list of filtered words.") .ConfigureAwait(false); } diff --git a/src/NadekoBot/Modules/Searches/Commands/StreamNotificationCommands.cs b/src/NadekoBot/Modules/Searches/Commands/StreamNotificationCommands.cs index b18bd349..093328bc 100644 --- a/src/NadekoBot/Modules/Searches/Commands/StreamNotificationCommands.cs +++ b/src/NadekoBot/Modules/Searches/Commands/StreamNotificationCommands.cs @@ -195,7 +195,7 @@ namespace NadekoBot.Modules.Searches toRemove = streams.Where(fs => fs.ChannelId == channel.Id && fs.Username.ToUpperInvariant() == username).FirstOrDefault(); if (toRemove != null) { - config.FollowedStreams = streams.Except(new[] { toRemove }).ToList(); + config.FollowedStreams = new HashSet(streams.Except(new[] { toRemove })); await uow.CompleteAsync(); } } diff --git a/src/NadekoBot/Services/Database/NadekoContext.cs b/src/NadekoBot/Services/Database/NadekoContext.cs index 83fc449e..347ad469 100644 --- a/src/NadekoBot/Services/Database/NadekoContext.cs +++ b/src/NadekoBot/Services/Database/NadekoContext.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Threading.Tasks; using JetBrains.Annotations; using NadekoBot.Services.Database.Models; +using NadekoBot.Extensions; namespace NadekoBot.Services.Database { @@ -47,7 +48,7 @@ namespace NadekoBot.Services.Database { var bc = new BotConfig(); - bc.ModulePrefixes.AddRange(new ConcurrentHashSet() + bc.ModulePrefixes.AddRange(new HashSet() { new ModulePrefix() { ModuleName = "Administration", Prefix = "." }, new ModulePrefix() { ModuleName = "Searches", Prefix = "~" }, @@ -64,7 +65,7 @@ namespace NadekoBot.Services.Database new ModulePrefix() { ModuleName = "Utility", Prefix = "." }, new ModulePrefix() { ModuleName = "CustomReactions", Prefix = "." } }); - bc.RaceAnimals.AddRange(new ConcurrentHashSet + bc.RaceAnimals.AddRange(new HashSet { new RaceAnimal { Icon = "🐼", Name = "Panda" }, new RaceAnimal { Icon = "🐻", Name = "Bear" }, @@ -75,7 +76,7 @@ namespace NadekoBot.Services.Database new RaceAnimal { Icon = "🦀", Name = "Crab" }, new RaceAnimal { Icon = "🦄", Name = "Unicorn" } }); - bc.EightBallResponses.AddRange(new ConcurrentHashSet + bc.EightBallResponses.AddRange(new HashSet { new EightBallResponse() { Text = "Most definitely yes" }, new EightBallResponse() { Text = "For sure" }, diff --git a/src/NadekoBot/_Extensions/Extensions.cs b/src/NadekoBot/_Extensions/Extensions.cs index d73eab9b..b1722e12 100644 --- a/src/NadekoBot/_Extensions/Extensions.cs +++ b/src/NadekoBot/_Extensions/Extensions.cs @@ -23,6 +23,14 @@ namespace NadekoBot.Extensions http.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); } + public static void AddRange(this HashSet target, IEnumerable elements) where T : class + { + foreach (var item in elements) + { + target.Add(item); + } + } + public static bool IsInteger(this decimal number) => number == Math.Truncate(number); public static string SanitizeMentions(this string str) =>