v+t slower but much better and reliable. Need to check ratelimits before speeding it up.
This commit is contained in:
parent
dfbabea8b9
commit
9791b5b9e3
@ -105,18 +105,19 @@ 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) =>
|
||||||
{
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
var msg = imsg as IUserMessage;
|
var msg = imsg as IUserMessage;
|
||||||
if (msg == null || msg.Author.IsBot)
|
if (msg == null || msg.Author.IsBot)
|
||||||
return;
|
return Task.CompletedTask;
|
||||||
|
|
||||||
var channel = msg.Channel as ITextChannel;
|
var channel = msg.Channel as ITextChannel;
|
||||||
if (channel == null)
|
if (channel == null)
|
||||||
return;
|
return Task.CompletedTask;
|
||||||
|
var _ = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
AntiSpamStats spamSettings;
|
AntiSpamStats spamSettings;
|
||||||
if (!antiSpamGuilds.TryGetValue(channel.Guild.Id, out spamSettings) ||
|
if (!antiSpamGuilds.TryGetValue(channel.Guild.Id, out spamSettings) ||
|
||||||
spamSettings.AntiSpamSettings.IgnoredChannels.Contains(new AntiSpamIgnore()
|
spamSettings.AntiSpamSettings.IgnoredChannels.Contains(new AntiSpamIgnore()
|
||||||
@ -126,7 +127,10 @@ namespace NadekoBot.Modules.Administration
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
var stats = spamSettings.UserStats.AddOrUpdate(msg.Author.Id, new UserSpamStats(msg.Content),
|
var stats = spamSettings.UserStats.AddOrUpdate(msg.Author.Id, new UserSpamStats(msg.Content),
|
||||||
(id, old) => { old.ApplyNextMessage(msg.Content); return old; });
|
(id, old) =>
|
||||||
|
{
|
||||||
|
old.ApplyNextMessage(msg.Content); return old;
|
||||||
|
});
|
||||||
|
|
||||||
if (stats.Count >= spamSettings.AntiSpamSettings.MessageThreshold)
|
if (stats.Count >= spamSettings.AntiSpamSettings.MessageThreshold)
|
||||||
{
|
{
|
||||||
@ -138,20 +142,24 @@ namespace NadekoBot.Modules.Administration
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
|
});
|
||||||
|
return Task.CompletedTask;
|
||||||
};
|
};
|
||||||
|
|
||||||
NadekoBot.Client.UserJoined += async (usr) =>
|
NadekoBot.Client.UserJoined += (usr) =>
|
||||||
|
{
|
||||||
|
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 () =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (usr.IsBot)
|
|
||||||
return;
|
|
||||||
AntiRaidStats settings;
|
|
||||||
if (!antiRaidGuilds.TryGetValue(usr.Guild.Id, out settings))
|
|
||||||
return;
|
|
||||||
if (!settings.RaidUsers.Add(usr))
|
|
||||||
return;
|
|
||||||
|
|
||||||
++settings.UsersCount;
|
++settings.UsersCount;
|
||||||
|
|
||||||
if (settings.UsersCount >= settings.AntiRaidSettings.UserThreshold)
|
if (settings.UsersCount >= settings.AntiRaidSettings.UserThreshold)
|
||||||
@ -168,11 +176,14 @@ namespace NadekoBot.Modules.Administration
|
|||||||
|
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
|
});
|
||||||
|
return Task.CompletedTask;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
@ -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,22 +40,26 @@ 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;
|
var botUserPerms = guild.CurrentUser.GuildPermissions;
|
||||||
|
|
||||||
if (before.VoiceChannel == after.VoiceChannel) return;
|
if (before.VoiceChannel == after.VoiceChannel)
|
||||||
|
return Task.CompletedTask;
|
||||||
|
|
||||||
if (!voicePlusTextCache.Contains(guild.Id))
|
if (!voicePlusTextCache.Contains(guild.Id))
|
||||||
return;
|
return Task.CompletedTask;
|
||||||
|
|
||||||
|
var _ = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles)
|
if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles)
|
||||||
{
|
{
|
||||||
@ -71,43 +79,80 @@ namespace NadekoBot.Modules.Administration
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var semaphore = guildLockObjects.GetOrAdd(guild.Id, (key) => new SemaphoreSlim(1, 1));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await semaphore.WaitAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
var beforeVch = before.VoiceChannel;
|
var beforeVch = before.VoiceChannel;
|
||||||
if (beforeVch != null)
|
if (beforeVch != null)
|
||||||
{
|
{
|
||||||
var textChannel = guild.TextChannels.Where(t => t.Name == GetChannelName(beforeVch.Name).ToLowerInvariant()).FirstOrDefault();
|
var beforeRoleName = GetRoleName(beforeVch);
|
||||||
if (textChannel != null)
|
var beforeRole = guild.Roles.FirstOrDefault(x => x.Name == beforeRoleName);
|
||||||
await textChannel.AddPermissionOverwriteAsync(user,
|
if (beforeRole != null)
|
||||||
new OverwritePermissions(readMessages: PermValue.Deny,
|
try
|
||||||
sendMessages: PermValue.Deny)).ConfigureAwait(false);
|
{
|
||||||
|
_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;
|
var afterVch = after.VoiceChannel;
|
||||||
if (afterVch != null && guild.AFKChannel?.Id != afterVch.Id)
|
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);
|
||||||
|
|
||||||
ITextChannel textChannel = guild.TextChannels
|
ITextChannel textChannel = guild.TextChannels
|
||||||
.Where(t => t.Name == GetChannelName(afterVch.Name).ToLowerInvariant())
|
.Where(t => t.Name == GetChannelName(afterVch.Name).ToLowerInvariant())
|
||||||
.FirstOrDefault();
|
.FirstOrDefault();
|
||||||
if (textChannel == null)
|
if (textChannel == null)
|
||||||
{
|
{
|
||||||
textChannel = (await guild.CreateTextChannelAsync(GetChannelName(afterVch.Name).ToLowerInvariant()).ConfigureAwait(false));
|
var created = (await guild.CreateTextChannelAsync(GetChannelName(afterVch.Name).ToLowerInvariant()).ConfigureAwait(false));
|
||||||
await textChannel.AddPermissionOverwriteAsync(guild.EveryoneRole,
|
|
||||||
new OverwritePermissions(readMessages: PermValue.Deny,
|
try { await guild.CurrentUser.AddRolesAsync(roleToAdd).ConfigureAwait(false); } catch { }
|
||||||
sendMessages: PermValue.Deny)).ConfigureAwait(false);
|
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);
|
||||||
}
|
}
|
||||||
await textChannel.AddPermissionOverwriteAsync(user,
|
_log.Warn("Adding role " + roleToAdd.Name + " to user " + user.Username);
|
||||||
new OverwritePermissions(readMessages: PermValue.Allow,
|
await user.AddRolesAsync(roleToAdd).ConfigureAwait(false);
|
||||||
sendMessages: PermValue.Allow)).ConfigureAwait(false);
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
semaphore.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex);
|
_log.Warn(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);
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user