diff --git a/src/NadekoBot/Modules/Administration/Commands/ProtectionCommands.cs b/src/NadekoBot/Modules/Administration/Commands/ProtectionCommands.cs index e37f4850..3e3b217a 100644 --- a/src/NadekoBot/Modules/Administration/Commands/ProtectionCommands.cs +++ b/src/NadekoBot/Modules/Administration/Commands/ProtectionCommands.cs @@ -105,74 +105,85 @@ namespace NadekoBot.Modules.Administration 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; - 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) + try { - 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) - .ConfigureAwait(false); + if (spamSettings.UserStats.TryRemove(msg.Author.Id, out stats)) + { + 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) - 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) + try { - var users = settings.RaidUsers.ToArray(); - settings.RaidUsers.Clear(); + ++settings.UsersCount; + + 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); - - settings.RaidUsers.TryRemove(usr); - --settings.UsersCount; - - } - catch { } + catch { } + }); + return Task.CompletedTask; }; } 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) { switch (action) diff --git a/src/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommands.cs b/src/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommands.cs index 9d92ff98..3b68f36f 100644 --- a/src/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommands.cs +++ b/src/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommands.cs @@ -7,9 +7,11 @@ using NadekoBot.Services; using NLog; using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; 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 ConcurrentHashSet voicePlusTextCache { get; } + + private static ConcurrentDictionary guildLockObjects = new ConcurrentDictionary(); static VoicePlusTextCommands() { var _log = LogManager.GetCurrentClassLogger(); @@ -36,78 +40,119 @@ namespace NadekoBot.Modules.Administration _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 guild = user?.Guild; 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; - - if (before.VoiceChannel == after.VoiceChannel) return; - - if (!voicePlusTextCache.Contains(guild.Id)) - return; - - if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles) + try { + + 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 { - 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; - } + await semaphore.WaitAsync().ConfigureAwait(false); + 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; - if (beforeVch != null) - { - var textChannel = guild.TextChannels.Where(t => t.Name == GetChannelName(beforeVch.Name).ToLowerInvariant()).FirstOrDefault(); - if (textChannel != null) - await textChannel.AddPermissionOverwriteAsync(user, - new OverwritePermissions(readMessages: PermValue.Deny, - sendMessages: PermValue.Deny)).ConfigureAwait(false); - } - var afterVch = after.VoiceChannel; - if (afterVch != null && guild.AFKChannel?.Id != afterVch.Id) - { - ITextChannel textChannel = guild.TextChannels - .Where(t => t.Name == GetChannelName(afterVch.Name).ToLowerInvariant()) - .FirstOrDefault(); - if (textChannel == null) - { - textChannel = (await guild.CreateTextChannelAsync(GetChannelName(afterVch.Name).ToLowerInvariant()).ConfigureAwait(false)); - await textChannel.AddPermissionOverwriteAsync(guild.EveryoneRole, - new OverwritePermissions(readMessages: PermValue.Deny, - sendMessages: PermValue.Deny)).ConfigureAwait(false); + ITextChannel textChannel = guild.TextChannels + .Where(t => t.Name == GetChannelName(afterVch.Name).ToLowerInvariant()) + .FirstOrDefault(); + if (textChannel == null) + { + var created = (await guild.CreateTextChannelAsync(GetChannelName(afterVch.Name).ToLowerInvariant()).ConfigureAwait(false)); + + try { await guild.CurrentUser.AddRolesAsync(roleToAdd).ConfigureAwait(false); } catch { } + await Task.Delay(50).ConfigureAwait(false); + await created.AddPermissionOverwriteAsync(roleToAdd, new OverwritePermissions( + readMessages: PermValue.Allow, + sendMessages: PermValue.Allow)) + .ConfigureAwait(false); + await Task.Delay(50).ConfigureAwait(false); + await created.AddPermissionOverwriteAsync(guild.EveryoneRole, new OverwritePermissions( + readMessages: PermValue.Deny, + sendMessages: PermValue.Deny)) + .ConfigureAwait(false); + await Task.Delay(50).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) - { - Console.WriteLine(ex); - } + catch (Exception ex) + { + _log.Warn(ex); + } + }); + return Task.CompletedTask; } private static string GetChannelName(string voiceName) => channelNameRegex.Replace(voiceName, "").Trim().Replace(" ", "-").TrimTo(90, true) + "-voice"; + private static string GetRoleName(IVoiceChannel ch) => + "nvoice-" + ch.Id; + [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] [RequireUserPermission(GuildPermission.ManageRoles)] @@ -127,7 +172,7 @@ namespace NadekoBot.Modules.Administration { 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.`"); } catch { } @@ -147,6 +192,13 @@ namespace NadekoBot.Modules.Administration foreach (var textChannel in (await guild.GetTextChannelsAsync().ConfigureAwait(false)).Where(c => c.Name.EndsWith("-voice"))) { 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); return; @@ -163,7 +215,9 @@ namespace NadekoBot.Modules.Administration [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] [RequireUserPermission(GuildPermission.ManageChannels)] + [RequireBotPermission(GuildPermission.ManageChannels)] [RequireUserPermission(GuildPermission.ManageRoles)] + //[RequireBotPermission(GuildPermission.ManageRoles)] public async Task CleanVPlusT() { var guild = Context.Guild; @@ -174,15 +228,27 @@ namespace NadekoBot.Modules.Administration return; } - var allTxtChannels = (await guild.GetTextChannelsAsync()).Where(c => c.Name.EndsWith("-voice")); - var validTxtChannelNames = (await guild.GetVoiceChannelsAsync()).Select(c => GetChannelName(c.Name).ToLowerInvariant()); + var textChannels = await guild.GetTextChannelsAsync().ConfigureAwait(false); + 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(voiceChannels.Select(c => GetChannelName(c.Name).ToLowerInvariant())); + var invalidTxtChannels = boundTextChannels.Where(c => !validTxtChannelNames.Contains(c.Name)); foreach (var c in invalidTxtChannels) { 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(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); diff --git a/src/NadekoBot/NadekoBot.cs b/src/NadekoBot/NadekoBot.cs index a6842797..ba411cea 100644 --- a/src/NadekoBot/NadekoBot.cs +++ b/src/NadekoBot/NadekoBot.cs @@ -113,7 +113,7 @@ namespace NadekoBot 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 await CommandService.AddModuleAsync().ConfigureAwait(false); #endif