From c512f6360b0fe32d312fde5f28b89addfae5ea7b Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sun, 2 Apr 2017 00:25:26 +0200 Subject: [PATCH] .slowmodewl implemented and now works properly --- .../Commands/RatelimitCommand.cs | 45 ++++++++++--------- .../Resources/ResponseStrings.Designer.cs | 10 ++--- src/NadekoBot/Resources/ResponseStrings.resx | 10 ++--- .../Impl/GuildConfigRepository.cs | 2 + 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/NadekoBot/Modules/Administration/Commands/RatelimitCommand.cs b/src/NadekoBot/Modules/Administration/Commands/RatelimitCommand.cs index 0c0f91be..57dd4c89 100644 --- a/src/NadekoBot/Modules/Administration/Commands/RatelimitCommand.cs +++ b/src/NadekoBot/Modules/Administration/Commands/RatelimitCommand.cs @@ -5,7 +5,6 @@ using Microsoft.EntityFrameworkCore; using NadekoBot.Attributes; using NadekoBot.Extensions; using NadekoBot.Services; -using NadekoBot.Services.Database; using NadekoBot.Services.Database.Models; using NLog; using System; @@ -20,16 +19,16 @@ namespace NadekoBot.Modules.Administration public partial class Administration { [Group] - public class RatelimitCommand : NadekoSubmodule + public class RatelimitCommands : NadekoSubmodule { public static ConcurrentDictionary RatelimitingChannels = new ConcurrentDictionary(); + public static ConcurrentDictionary> IgnoredRoles = new ConcurrentDictionary>(); + public static ConcurrentDictionary> IgnoredUsers = new ConcurrentDictionary>(); + private new static readonly Logger _log; public class Ratelimiter { - public HashSet IgnoreUsers { get; set; } = new HashSet(); - public HashSet IgnoreRoles { get; set; } = new HashSet(); - public class RatelimitedUser { public ulong UserId { get; set; } @@ -45,10 +44,13 @@ namespace NadekoBot.Modules.Administration public ConcurrentDictionary Users { get; set; } = new ConcurrentDictionary(); - public bool CheckUserRatelimit(ulong id, SocketGuildUser optUser) + public bool CheckUserRatelimit(ulong id, ulong guildId, SocketGuildUser optUser) { - if (IgnoreUsers.Contains(id) || - (optUser != null && optUser.RoleIds.Any(x => IgnoreRoles.Contains(x)))) + HashSet ignoreUsers; + HashSet ignoreRoles; + + if ((IgnoredUsers.TryGetValue(guildId, out ignoreUsers) && ignoreUsers.Contains(id)) || + (optUser != null && IgnoredRoles.TryGetValue(guildId, out ignoreRoles) && optUser.RoleIds.Any(x => ignoreRoles.Contains(x)))) return false; var usr = Users.GetOrAdd(id, (key) => new RatelimitedUser() { UserId = id }); @@ -70,10 +72,20 @@ namespace NadekoBot.Modules.Administration } } - static RatelimitCommand() + static RatelimitCommands() { _log = LogManager.GetCurrentClassLogger(); + IgnoredRoles = new ConcurrentDictionary>( + NadekoBot.AllGuildConfigs + .ToDictionary(x => x.GuildId, + x => new HashSet(x.SlowmodeIgnoredRoles.Select(y => y.RoleId)))); + + IgnoredUsers = new ConcurrentDictionary>( + NadekoBot.AllGuildConfigs + .ToDictionary(x => x.GuildId, + x => new HashSet(x.SlowmodeIgnoredUsers.Select(y => y.UserId)))); + NadekoBot.Client.MessageReceived += async (umsg) => { try @@ -87,7 +99,7 @@ namespace NadekoBot.Modules.Administration if (!RatelimitingChannels.TryGetValue(channel.Id, out limiter)) return; - if (limiter.CheckUserRatelimit(usrMsg.Author.Id, usrMsg.Author as SocketGuildUser)) + if (limiter.CheckUserRatelimit(usrMsg.Author.Id, channel.Guild.Id, usrMsg.Author as SocketGuildUser)) await usrMsg.DeleteAsync(); } catch (Exception ex) { _log.Warn(ex); } @@ -157,11 +169,8 @@ namespace NadekoBot.Modules.Administration await uow.CompleteAsync().ConfigureAwait(false); } - Ratelimiter rl; - if (RatelimitingChannels.TryGetValue(Context.Guild.Id, out rl)) - { - rl.IgnoreUsers = new HashSet(usrs.Select(x => x.UserId)); - } + IgnoredUsers.AddOrUpdate(Context.Guild.Id, new HashSet(usrs.Select(x => x.UserId)), (key, old) => new HashSet(usrs.Select(x => x.UserId))); + if(removed) await ReplyConfirmLocalized("slowmodewl_user_stop", Format.Bold(user.ToString())).ConfigureAwait(false); else @@ -192,11 +201,7 @@ namespace NadekoBot.Modules.Administration await uow.CompleteAsync().ConfigureAwait(false); } - Ratelimiter rl; - if (RatelimitingChannels.TryGetValue(Context.Guild.Id, out rl)) - { - rl.IgnoreRoles = new HashSet(roles.Select(x => x.RoleId)); - } + IgnoredRoles.AddOrUpdate(Context.Guild.Id, new HashSet(roles.Select(x => x.RoleId)), (key, old) => new HashSet(roles.Select(x => x.RoleId))); if (removed) await ReplyConfirmLocalized("slowmodewl_role_stop", Format.Bold(role.ToString())).ConfigureAwait(false); diff --git a/src/NadekoBot/Resources/ResponseStrings.Designer.cs b/src/NadekoBot/Resources/ResponseStrings.Designer.cs index 445e025b..e13d8513 100644 --- a/src/NadekoBot/Resources/ResponseStrings.Designer.cs +++ b/src/NadekoBot/Resources/ResponseStrings.Designer.cs @@ -1513,7 +1513,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to Slowmode will now ignore role {0}. + /// Looks up a localized string similar to Slowmode will now ignore {0} role.. /// public static string administration_slowmodewl_role_start { get { @@ -1522,7 +1522,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to Slowmode will no longer ignore role {0}. + /// Looks up a localized string similar to Slowmode will no longer ignore {0} role.. /// public static string administration_slowmodewl_role_stop { get { @@ -1531,7 +1531,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to Slowmode will now ignore user {0}. + /// Looks up a localized string similar to Slowmode will now ignore user {0}.. /// public static string administration_slowmodewl_user_start { get { @@ -1540,7 +1540,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to Slowmode will no longer ignore user {0}. + /// Looks up a localized string similar to Slowmode will no longer ignore user {0}.. /// public static string administration_slowmodewl_user_stop { get { @@ -6107,7 +6107,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to You've received {0}. Thanks for supporting the project!. + /// Looks up a localized string similar to You've received {0} Thanks for supporting the project!. /// public static string utility_clpa_success { get { diff --git a/src/NadekoBot/Resources/ResponseStrings.resx b/src/NadekoBot/Resources/ResponseStrings.resx index 4d605da8..83d701f3 100644 --- a/src/NadekoBot/Resources/ResponseStrings.resx +++ b/src/NadekoBot/Resources/ResponseStrings.resx @@ -2358,16 +2358,16 @@ Owner ID: {2} I will apply {0} punishment to users with {1} warnings. - Slowmode will now ignore role {0} + Slowmode will now ignore {0} role. - Slowmode will no longer ignore role {0} + Slowmode will no longer ignore {0} role. - Slowmode will now ignore user {0} + Slowmode will now ignore user {0}. - Slowmode will no longer ignore user {0} + Slowmode will no longer ignore user {0}. Failed claiming rewards due to one of the following reasons: @@ -2397,7 +2397,7 @@ Owner ID: {2} Wait some time - You've received {0}. Thanks for supporting the project! + You've received {0} Thanks for supporting the project! Rewards can be claimed on or after 5th of each month. diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/GuildConfigRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/GuildConfigRepository.cs index 01a44677..ebe41406 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/GuildConfigRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/GuildConfigRepository.cs @@ -38,6 +38,8 @@ namespace NadekoBot.Services.Database.Repositories.Impl .Include(gc => gc.CommandCooldowns) .Include(gc => gc.GuildRepeaters) .Include(gc => gc.AntiRaidSetting) + .Include(gc => gc.SlowmodeIgnoredRoles) + .Include(gc => gc.SlowmodeIgnoredUsers) .Include(gc => gc.AntiSpamSetting) .ThenInclude(x => x.IgnoredChannels) .ToList();