.slowmodewl implemented and now works properly

This commit is contained in:
Kwoth 2017-04-02 00:25:26 +02:00
parent 3cec4f14d0
commit c512f6360b
4 changed files with 37 additions and 30 deletions

View File

@ -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<ulong, Ratelimiter> RatelimitingChannels = new ConcurrentDictionary<ulong, Ratelimiter>();
public static ConcurrentDictionary<ulong, HashSet<ulong>> IgnoredRoles = new ConcurrentDictionary<ulong, HashSet<ulong>>();
public static ConcurrentDictionary<ulong, HashSet<ulong>> IgnoredUsers = new ConcurrentDictionary<ulong, HashSet<ulong>>();
private new static readonly Logger _log;
public class Ratelimiter
{
public HashSet<ulong> IgnoreUsers { get; set; } = new HashSet<ulong>();
public HashSet<ulong> IgnoreRoles { get; set; } = new HashSet<ulong>();
public class RatelimitedUser
{
public ulong UserId { get; set; }
@ -45,10 +44,13 @@ namespace NadekoBot.Modules.Administration
public ConcurrentDictionary<ulong, RatelimitedUser> Users { get; set; } = new ConcurrentDictionary<ulong, RatelimitedUser>();
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<ulong> ignoreUsers;
HashSet<ulong> 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<ulong, HashSet<ulong>>(
NadekoBot.AllGuildConfigs
.ToDictionary(x => x.GuildId,
x => new HashSet<ulong>(x.SlowmodeIgnoredRoles.Select(y => y.RoleId))));
IgnoredUsers = new ConcurrentDictionary<ulong, HashSet<ulong>>(
NadekoBot.AllGuildConfigs
.ToDictionary(x => x.GuildId,
x => new HashSet<ulong>(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<ulong>(usrs.Select(x => x.UserId));
}
IgnoredUsers.AddOrUpdate(Context.Guild.Id, new HashSet<ulong>(usrs.Select(x => x.UserId)), (key, old) => new HashSet<ulong>(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<ulong>(roles.Select(x => x.RoleId));
}
IgnoredRoles.AddOrUpdate(Context.Guild.Id, new HashSet<ulong>(roles.Select(x => x.RoleId)), (key, old) => new HashSet<ulong>(roles.Select(x => x.RoleId)));
if (removed)
await ReplyConfirmLocalized("slowmodewl_role_stop", Format.Bold(role.ToString())).ConfigureAwait(false);

View File

@ -1513,7 +1513,7 @@ namespace NadekoBot.Resources {
}
/// <summary>
/// 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..
/// </summary>
public static string administration_slowmodewl_role_start {
get {
@ -1522,7 +1522,7 @@ namespace NadekoBot.Resources {
}
/// <summary>
/// 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..
/// </summary>
public static string administration_slowmodewl_role_stop {
get {
@ -1531,7 +1531,7 @@ namespace NadekoBot.Resources {
}
/// <summary>
/// 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}..
/// </summary>
public static string administration_slowmodewl_user_start {
get {
@ -1540,7 +1540,7 @@ namespace NadekoBot.Resources {
}
/// <summary>
/// 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}..
/// </summary>
public static string administration_slowmodewl_user_stop {
get {
@ -6107,7 +6107,7 @@ namespace NadekoBot.Resources {
}
/// <summary>
/// Looks up a localized string similar to You&apos;ve received {0}. Thanks for supporting the project!.
/// Looks up a localized string similar to You&apos;ve received {0} Thanks for supporting the project!.
/// </summary>
public static string utility_clpa_success {
get {

View File

@ -2358,16 +2358,16 @@ Owner ID: {2}</value>
<value>I will apply {0} punishment to users with {1} warnings.</value>
</data>
<data name="administration_slowmodewl_role_start" xml:space="preserve">
<value>Slowmode will now ignore role {0}</value>
<value>Slowmode will now ignore {0} role.</value>
</data>
<data name="administration_slowmodewl_role_stop" xml:space="preserve">
<value>Slowmode will no longer ignore role {0}</value>
<value>Slowmode will no longer ignore {0} role.</value>
</data>
<data name="administration_slowmodewl_user_start" xml:space="preserve">
<value>Slowmode will now ignore user {0}</value>
<value>Slowmode will now ignore user {0}.</value>
</data>
<data name="administration_slowmodewl_user_stop" xml:space="preserve">
<value>Slowmode will no longer ignore user {0}</value>
<value>Slowmode will no longer ignore user {0}.</value>
</data>
<data name="utility_clpa_fail" xml:space="preserve">
<value>Failed claiming rewards due to one of the following reasons:</value>
@ -2397,7 +2397,7 @@ Owner ID: {2}</value>
<value>Wait some time</value>
</data>
<data name="utility_clpa_success" xml:space="preserve">
<value>You've received {0}. Thanks for supporting the project!</value>
<value>You've received {0} Thanks for supporting the project!</value>
</data>
<data name="utility_clpa_too_early" xml:space="preserve">
<value>Rewards can be claimed on or after 5th of each month.</value>

View File

@ -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();