v+t slower but much better and reliable. Need to check ratelimits before speeding it up.

This commit is contained in:
Kwoth 2017-02-02 21:00:38 +01:00
parent dfbabea8b9
commit 9791b5b9e3
3 changed files with 185 additions and 108 deletions

View File

@ -105,74 +105,85 @@ namespace NadekoBot.Modules.Administration
antiSpamGuilds.TryAdd(gc.GuildId, new AntiSpamStats() { AntiSpamSettings = spam }); antiSpamGuilds.TryAdd(gc.GuildId, new AntiSpamStats() { AntiSpamSettings = spam });
} }
NadekoBot.Client.MessageReceived += async (imsg) => NadekoBot.Client.MessageReceived += (imsg) =>
{ {
var msg = imsg as IUserMessage;
if (msg == null || msg.Author.IsBot)
return Task.CompletedTask;
try var channel = msg.Channel as ITextChannel;
if (channel == null)
return Task.CompletedTask;
var _ = Task.Run(async () =>
{ {
var msg = imsg as IUserMessage; try
if (msg == null || msg.Author.IsBot)
return;
var channel = msg.Channel as ITextChannel;
if (channel == null)
return;
AntiSpamStats spamSettings;
if (!antiSpamGuilds.TryGetValue(channel.Guild.Id, out spamSettings) ||
spamSettings.AntiSpamSettings.IgnoredChannels.Contains(new AntiSpamIgnore()
{
ChannelId = channel.Id
}))
return;
var stats = spamSettings.UserStats.AddOrUpdate(msg.Author.Id, new UserSpamStats(msg.Content),
(id, old) => { old.ApplyNextMessage(msg.Content); return old; });
if (stats.Count >= spamSettings.AntiSpamSettings.MessageThreshold)
{ {
if (spamSettings.UserStats.TryRemove(msg.Author.Id, out stats)) AntiSpamStats spamSettings;
if (!antiSpamGuilds.TryGetValue(channel.Guild.Id, out spamSettings) ||
spamSettings.AntiSpamSettings.IgnoredChannels.Contains(new AntiSpamIgnore()
{
ChannelId = channel.Id
}))
return;
var stats = spamSettings.UserStats.AddOrUpdate(msg.Author.Id, new UserSpamStats(msg.Content),
(id, old) =>
{
old.ApplyNextMessage(msg.Content); return old;
});
if (stats.Count >= spamSettings.AntiSpamSettings.MessageThreshold)
{ {
await PunishUsers(spamSettings.AntiSpamSettings.Action, ProtectionType.Spamming, (IGuildUser)msg.Author) if (spamSettings.UserStats.TryRemove(msg.Author.Id, out stats))
.ConfigureAwait(false); {
await PunishUsers(spamSettings.AntiSpamSettings.Action, ProtectionType.Spamming, (IGuildUser)msg.Author)
.ConfigureAwait(false);
}
} }
} }
} catch { }
catch { } });
return Task.CompletedTask;
}; };
NadekoBot.Client.UserJoined += async (usr) => NadekoBot.Client.UserJoined += (usr) =>
{ {
try if (usr.IsBot)
return Task.CompletedTask;
AntiRaidStats settings;
if (!antiRaidGuilds.TryGetValue(usr.Guild.Id, out settings))
return Task.CompletedTask;
if (!settings.RaidUsers.Add(usr))
return Task.CompletedTask;
var _ = Task.Run(async () =>
{ {
if (usr.IsBot) try
return;
AntiRaidStats settings;
if (!antiRaidGuilds.TryGetValue(usr.Guild.Id, out settings))
return;
if (!settings.RaidUsers.Add(usr))
return;
++settings.UsersCount;
if (settings.UsersCount >= settings.AntiRaidSettings.UserThreshold)
{ {
var users = settings.RaidUsers.ToArray(); ++settings.UsersCount;
settings.RaidUsers.Clear();
if (settings.UsersCount >= settings.AntiRaidSettings.UserThreshold)
{
var users = settings.RaidUsers.ToArray();
settings.RaidUsers.Clear();
await PunishUsers(settings.AntiRaidSettings.Action, ProtectionType.Raiding, users).ConfigureAwait(false);
}
await Task.Delay(1000 * settings.AntiRaidSettings.Seconds).ConfigureAwait(false);
settings.RaidUsers.TryRemove(usr);
--settings.UsersCount;
await PunishUsers(settings.AntiRaidSettings.Action, ProtectionType.Raiding, users).ConfigureAwait(false);
} }
await Task.Delay(1000 * settings.AntiRaidSettings.Seconds).ConfigureAwait(false); catch { }
});
settings.RaidUsers.TryRemove(usr); return Task.CompletedTask;
--settings.UsersCount;
}
catch { }
}; };
} }
private static async Task PunishUsers(PunishmentAction action, ProtectionType pt, params IGuildUser[] gus) private static async Task PunishUsers(PunishmentAction action, ProtectionType pt, params IGuildUser[] gus)
{ {
_log.Warn($"[{pt}] - Punishing [{gus.Length}] users with [{action}] in {gus[0].Guild.Name} guild");
foreach (var gu in gus) foreach (var gu in gus)
{ {
switch (action) switch (action)

View File

@ -7,9 +7,11 @@ using NadekoBot.Services;
using NLog; using NLog;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace NadekoBot.Modules.Administration namespace NadekoBot.Modules.Administration
@ -22,6 +24,8 @@ namespace NadekoBot.Modules.Administration
private static Regex channelNameRegex = new Regex(@"[^a-zA-Z0-9 -]", RegexOptions.Compiled); private static Regex channelNameRegex = new Regex(@"[^a-zA-Z0-9 -]", RegexOptions.Compiled);
private static ConcurrentHashSet<ulong> voicePlusTextCache { get; } private static ConcurrentHashSet<ulong> voicePlusTextCache { get; }
private static ConcurrentDictionary<ulong, SemaphoreSlim> guildLockObjects = new ConcurrentDictionary<ulong, SemaphoreSlim>();
static VoicePlusTextCommands() static VoicePlusTextCommands()
{ {
var _log = LogManager.GetCurrentClassLogger(); var _log = LogManager.GetCurrentClassLogger();
@ -36,78 +40,119 @@ namespace NadekoBot.Modules.Administration
_log.Debug($"Loaded in {sw.Elapsed.TotalSeconds:F2}s"); _log.Debug($"Loaded in {sw.Elapsed.TotalSeconds:F2}s");
} }
private static async Task UserUpdatedEventHandler(SocketUser iuser, SocketVoiceState before, SocketVoiceState after) private static Task UserUpdatedEventHandler(SocketUser iuser, SocketVoiceState before, SocketVoiceState after)
{ {
var user = (iuser as SocketGuildUser); var user = (iuser as SocketGuildUser);
var guild = user?.Guild; var guild = user?.Guild;
if (guild == null) if (guild == null)
return; return Task.CompletedTask;
try var botUserPerms = guild.CurrentUser.GuildPermissions;
if (before.VoiceChannel == after.VoiceChannel)
return Task.CompletedTask;
if (!voicePlusTextCache.Contains(guild.Id))
return Task.CompletedTask;
var _ = Task.Run(async () =>
{ {
var botUserPerms = guild.CurrentUser.GuildPermissions; try
if (before.VoiceChannel == after.VoiceChannel) return;
if (!voicePlusTextCache.Contains(guild.Id))
return;
if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles)
{ {
if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles)
{
try
{
await guild.Owner.SendErrorAsync(
"⚠️ I don't have **manage server** and/or **manage channels** permission," +
$" so I cannot run `voice+text` on **{guild.Name}** server.").ConfigureAwait(false);
}
catch { }
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(guild.Id, set => set).VoicePlusTextEnabled = false;
voicePlusTextCache.TryRemove(guild.Id);
await uow.CompleteAsync().ConfigureAwait(false);
}
return;
}
var semaphore = guildLockObjects.GetOrAdd(guild.Id, (key) => new SemaphoreSlim(1, 1));
try try
{ {
await guild.Owner.SendErrorAsync( await semaphore.WaitAsync().ConfigureAwait(false);
"⚠️ I don't have **manage server** and/or **manage channels** permission," +
$" so I cannot run `voice+text` on **{guild.Name}** server.").ConfigureAwait(false);
}
catch { }
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(guild.Id, set => set).VoicePlusTextEnabled = false;
voicePlusTextCache.TryRemove(guild.Id);
await uow.CompleteAsync().ConfigureAwait(false);
}
return;
}
var beforeVch = before.VoiceChannel;
if (beforeVch != null)
{
var beforeRoleName = GetRoleName(beforeVch);
var beforeRole = guild.Roles.FirstOrDefault(x => x.Name == beforeRoleName);
if (beforeRole != null)
try
{
_log.Warn("Removing role " + beforeRoleName + " from user " + user.Username);
await user.RemoveRolesAsync(beforeRole).ConfigureAwait(false);
await Task.Delay(200).ConfigureAwait(false);
}
catch (Exception ex)
{
_log.Warn(ex);
}
}
var afterVch = after.VoiceChannel;
if (afterVch != null && guild.AFKChannel?.Id != afterVch.Id)
{
var roleName = GetRoleName(afterVch);
IRole roleToAdd = guild.Roles.FirstOrDefault(x => x.Name == roleName);
if (roleToAdd == null)
roleToAdd = await guild.CreateRoleAsync(roleName).ConfigureAwait(false);
var beforeVch = before.VoiceChannel; ITextChannel textChannel = guild.TextChannels
if (beforeVch != null) .Where(t => t.Name == GetChannelName(afterVch.Name).ToLowerInvariant())
{ .FirstOrDefault();
var textChannel = guild.TextChannels.Where(t => t.Name == GetChannelName(beforeVch.Name).ToLowerInvariant()).FirstOrDefault(); if (textChannel == null)
if (textChannel != null) {
await textChannel.AddPermissionOverwriteAsync(user, var created = (await guild.CreateTextChannelAsync(GetChannelName(afterVch.Name).ToLowerInvariant()).ConfigureAwait(false));
new OverwritePermissions(readMessages: PermValue.Deny,
sendMessages: PermValue.Deny)).ConfigureAwait(false); try { await guild.CurrentUser.AddRolesAsync(roleToAdd).ConfigureAwait(false); } catch { }
} await Task.Delay(50).ConfigureAwait(false);
var afterVch = after.VoiceChannel; await created.AddPermissionOverwriteAsync(roleToAdd, new OverwritePermissions(
if (afterVch != null && guild.AFKChannel?.Id != afterVch.Id) readMessages: PermValue.Allow,
{ sendMessages: PermValue.Allow))
ITextChannel textChannel = guild.TextChannels .ConfigureAwait(false);
.Where(t => t.Name == GetChannelName(afterVch.Name).ToLowerInvariant()) await Task.Delay(50).ConfigureAwait(false);
.FirstOrDefault(); await created.AddPermissionOverwriteAsync(guild.EveryoneRole, new OverwritePermissions(
if (textChannel == null) readMessages: PermValue.Deny,
{ sendMessages: PermValue.Deny))
textChannel = (await guild.CreateTextChannelAsync(GetChannelName(afterVch.Name).ToLowerInvariant()).ConfigureAwait(false)); .ConfigureAwait(false);
await textChannel.AddPermissionOverwriteAsync(guild.EveryoneRole, await Task.Delay(50).ConfigureAwait(false);
new OverwritePermissions(readMessages: PermValue.Deny, }
sendMessages: PermValue.Deny)).ConfigureAwait(false); _log.Warn("Adding role " + roleToAdd.Name + " to user " + user.Username);
await user.AddRolesAsync(roleToAdd).ConfigureAwait(false);
}
}
finally
{
semaphore.Release();
} }
await textChannel.AddPermissionOverwriteAsync(user,
new OverwritePermissions(readMessages: PermValue.Allow,
sendMessages: PermValue.Allow)).ConfigureAwait(false);
} }
} catch (Exception ex)
catch (Exception ex) {
{ _log.Warn(ex);
Console.WriteLine(ex); }
} });
return Task.CompletedTask;
} }
private static string GetChannelName(string voiceName) => private static string GetChannelName(string voiceName) =>
channelNameRegex.Replace(voiceName, "").Trim().Replace(" ", "-").TrimTo(90, true) + "-voice"; channelNameRegex.Replace(voiceName, "").Trim().Replace(" ", "-").TrimTo(90, true) + "-voice";
private static string GetRoleName(IVoiceChannel ch) =>
"nvoice-" + ch.Id;
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageRoles)] [RequireUserPermission(GuildPermission.ManageRoles)]
@ -127,7 +172,7 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
await Context.Channel.SendErrorAsync("⚠️ You are enabling this feature and **I do not have ADMINISTRATOR permissions**. " + await Context.Channel.SendErrorAsync("⚠️ You are enabling/disabling this feature and **I do not have ADMINISTRATOR permissions**. " +
"`This may cause some issues, and you will have to clean up text channels yourself afterwards.`"); "`This may cause some issues, and you will have to clean up text channels yourself afterwards.`");
} }
catch { } catch { }
@ -147,6 +192,13 @@ namespace NadekoBot.Modules.Administration
foreach (var textChannel in (await guild.GetTextChannelsAsync().ConfigureAwait(false)).Where(c => c.Name.EndsWith("-voice"))) foreach (var textChannel in (await guild.GetTextChannelsAsync().ConfigureAwait(false)).Where(c => c.Name.EndsWith("-voice")))
{ {
try { await textChannel.DeleteAsync().ConfigureAwait(false); } catch { } try { await textChannel.DeleteAsync().ConfigureAwait(false); } catch { }
await Task.Delay(500).ConfigureAwait(false);
}
foreach (var role in guild.Roles.Where(c => c.Name.StartsWith("nvoice-")))
{
try { await role.DeleteAsync().ConfigureAwait(false); } catch { }
await Task.Delay(500).ConfigureAwait(false);
} }
await Context.Channel.SendConfirmAsync(" Successfuly **removed** voice + text feature.").ConfigureAwait(false); await Context.Channel.SendConfirmAsync(" Successfuly **removed** voice + text feature.").ConfigureAwait(false);
return; return;
@ -163,7 +215,9 @@ namespace NadekoBot.Modules.Administration
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageChannels)] [RequireUserPermission(GuildPermission.ManageChannels)]
[RequireBotPermission(GuildPermission.ManageChannels)]
[RequireUserPermission(GuildPermission.ManageRoles)] [RequireUserPermission(GuildPermission.ManageRoles)]
//[RequireBotPermission(GuildPermission.ManageRoles)]
public async Task CleanVPlusT() public async Task CleanVPlusT()
{ {
var guild = Context.Guild; var guild = Context.Guild;
@ -174,15 +228,27 @@ namespace NadekoBot.Modules.Administration
return; return;
} }
var allTxtChannels = (await guild.GetTextChannelsAsync()).Where(c => c.Name.EndsWith("-voice")); var textChannels = await guild.GetTextChannelsAsync().ConfigureAwait(false);
var validTxtChannelNames = (await guild.GetVoiceChannelsAsync()).Select(c => GetChannelName(c.Name).ToLowerInvariant()); var voiceChannels = await guild.GetVoiceChannelsAsync().ConfigureAwait(false);
var invalidTxtChannels = allTxtChannels.Where(c => !validTxtChannelNames.Contains(c.Name)); var boundTextChannels = textChannels.Where(c => c.Name.EndsWith("-voice"));
var validTxtChannelNames = new HashSet<string>(voiceChannels.Select(c => GetChannelName(c.Name).ToLowerInvariant()));
var invalidTxtChannels = boundTextChannels.Where(c => !validTxtChannelNames.Contains(c.Name));
foreach (var c in invalidTxtChannels) foreach (var c in invalidTxtChannels)
{ {
try { await c.DeleteAsync().ConfigureAwait(false); } catch { } try { await c.DeleteAsync().ConfigureAwait(false); } catch { }
await Task.Delay(500); await Task.Delay(500).ConfigureAwait(false);
}
var boundRoles = guild.Roles.Where(r => r.Name.StartsWith("nvoice-"));
var validRoleNames = new HashSet<string>(voiceChannels.Select(c => GetRoleName(c).ToLowerInvariant()));
var invalidRoles = boundRoles.Where(r => !validRoleNames.Contains(r.Name));
foreach (var r in invalidRoles)
{
try { await r.DeleteAsync().ConfigureAwait(false); } catch { }
await Task.Delay(500).ConfigureAwait(false);
} }
await Context.Channel.SendConfirmAsync("Cleaned v+t.").ConfigureAwait(false); await Context.Channel.SendConfirmAsync("Cleaned v+t.").ConfigureAwait(false);

View File

@ -113,7 +113,7 @@ namespace NadekoBot
await CommandHandler.StartHandling().ConfigureAwait(false); await CommandHandler.StartHandling().ConfigureAwait(false);
await CommandService.AddModulesAsync(this.GetType().GetTypeInfo().Assembly).ConfigureAwait(false); var _ = await Task.Run(() => CommandService.AddModulesAsync(this.GetType().GetTypeInfo().Assembly)).ConfigureAwait(false);
#if !GLOBAL_NADEKO #if !GLOBAL_NADEKO
await CommandService.AddModuleAsync<Music>().ConfigureAwait(false); await CommandService.AddModuleAsync<Music>().ConfigureAwait(false);
#endif #endif