AntiRaid/Antispam now log to .logserver instead of guild owner DM

This commit is contained in:
Kwoth 2016-11-08 22:22:39 +01:00
parent c6bab91c40
commit 19bf146e4a
3 changed files with 89 additions and 60 deletions

View File

@ -87,9 +87,13 @@ namespace NadekoBot.Modules.Administration
} }
foreach (var toOverwrite in guild.GetTextChannels()) foreach (var toOverwrite in guild.GetTextChannels())
{
try
{ {
await toOverwrite.AddPermissionOverwriteAsync(muteRole, new OverwritePermissions(sendMessages: PermValue.Deny, attachFiles: PermValue.Deny)) await toOverwrite.AddPermissionOverwriteAsync(muteRole, new OverwritePermissions(sendMessages: PermValue.Deny, attachFiles: PermValue.Deny))
.ConfigureAwait(false); .ConfigureAwait(false);
}
catch { }
await Task.Delay(200).ConfigureAwait(false); await Task.Delay(200).ConfigureAwait(false);
} }
} }

View File

@ -114,9 +114,9 @@ namespace NadekoBot.Modules.Administration
{ {
if (spamSettings.UserStats.TryRemove(msg.Author.Id, out stats)) if (spamSettings.UserStats.TryRemove(msg.Author.Id, out stats))
{ {
var log = await PunishUser((IGuildUser)msg.Author, spamSettings.Action, spamSettings.MuteRole, ProtectionType.Spamming) await PunishUsers(spamSettings.Action, spamSettings.MuteRole, ProtectionType.Spamming, (IGuildUser)msg.Author)
.ConfigureAwait(false); .ConfigureAwait(false);
await channel.Guild.SendMessageToOwnerAsync(log).ConfigureAwait(false); } }
} }
} }
catch { } catch { }
@ -142,16 +142,11 @@ namespace NadekoBot.Modules.Administration
if (settings.UsersCount >= settings.UserThreshold) if (settings.UsersCount >= settings.UserThreshold)
{ {
var users = settings.RaidUsers.ToList(); var users = settings.RaidUsers.ToArray();
settings.RaidUsers.Clear(); settings.RaidUsers.Clear();
string msg = "";
foreach (var gu in users)
{
msg += await PunishUser(gu, settings.Action, settings.MuteRole, ProtectionType.Raiding).ConfigureAwait(false);
}
try { await usr.Guild.SendMessageToOwnerAsync(msg).ConfigureAwait(false); } catch { }
}
await PunishUsers(settings.Action, settings.MuteRole, ProtectionType.Raiding, users).ConfigureAwait(false);
}
await Task.Delay(1000 * settings.Seconds).ConfigureAwait(false); await Task.Delay(1000 * settings.Seconds).ConfigureAwait(false);
settings.RaidUsers.TryRemove(usr); settings.RaidUsers.TryRemove(usr);
@ -162,7 +157,9 @@ namespace NadekoBot.Modules.Administration
}; };
} }
private async Task<string> PunishUser(IGuildUser gu, PunishmentAction action, IRole muteRole, ProtectionType pt) private async Task PunishUsers(PunishmentAction action, IRole muteRole, ProtectionType pt, params IGuildUser[] gus)
{
foreach (var gu in gus)
{ {
switch (action) switch (action)
{ {
@ -170,7 +167,6 @@ namespace NadekoBot.Modules.Administration
try try
{ {
await gu.AddRolesAsync(muteRole); await gu.AddRolesAsync(muteRole);
return $"{Format.Bold(gu.ToString())} was **MUTED** due to `{pt}` protection on **{gu.Guild.Name}** server.\n";
} }
catch (Exception ex) { _log.Warn(ex, "I can't apply punishement"); } catch (Exception ex) { _log.Warn(ex, "I can't apply punishement"); }
break; break;
@ -188,8 +184,6 @@ namespace NadekoBot.Modules.Administration
// try it twice, really don't want to ban user if // try it twice, really don't want to ban user if
// only kick has been specified as the punishement // only kick has been specified as the punishement
} }
return $"{Format.Bold(gu.ToString())} was **KICKED** due to `{pt}` protection on **{gu.Guild.Name}** server.\n";
} }
catch (Exception ex) { _log.Warn(ex, "I can't apply punishment"); } catch (Exception ex) { _log.Warn(ex, "I can't apply punishment"); }
break; break;
@ -197,14 +191,14 @@ namespace NadekoBot.Modules.Administration
try try
{ {
await gu.Guild.AddBanAsync(gu, 7); await gu.Guild.AddBanAsync(gu, 7);
return $"{Format.Bold(gu.ToString())} was **BANNED** due to `{pt}` protection on **{gu.Guild.Name}** server.\n";
} }
catch (Exception ex) { _log.Warn(ex, "I can't apply punishment"); } catch (Exception ex) { _log.Warn(ex, "I can't apply punishment"); }
break; break;
default: default:
break; break;
} }
return String.Empty; }
await LogCommands.TriggeredAntiProtection(gus, action, pt).ConfigureAwait(false);
} }

View File

@ -22,21 +22,20 @@ namespace NadekoBot.Modules.Administration
[Group] [Group]
public class LogCommands public class LogCommands
{ {
private ShardedDiscordClient _client { get; } private static ShardedDiscordClient _client { get; }
private Logger _log { get; } private static Logger _log { get; }
private string prettyCurrentTime => $"【{DateTime.Now:HH:mm:ss}】"; private static string prettyCurrentTime => $"【{DateTime.Now:HH:mm:ss}】";
public ConcurrentDictionary<ulong, LogSetting> GuildLogSettings { get; } public static ConcurrentDictionary<ulong, LogSetting> GuildLogSettings { get; }
private ConcurrentDictionary<ITextChannel, List<string>> UserPresenceUpdates { get; } = new ConcurrentDictionary<ITextChannel, List<string>>(); private static ConcurrentDictionary<ITextChannel, List<string>> UserPresenceUpdates { get; } = new ConcurrentDictionary<ITextChannel, List<string>>();
private Timer t; private static Timer timerReference { get; }
private IGoogleApiService _google { get; } private IGoogleApiService _google { get; }
public LogCommands(ShardedDiscordClient client, IGoogleApiService google) static LogCommands()
{ {
_client = client; _client = NadekoBot.Client;
_google = google;
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
@ -45,7 +44,7 @@ namespace NadekoBot.Modules.Administration
.ToDictionary(g => g.GuildId, g => g.LogSetting)); .ToDictionary(g => g.GuildId, g => g.LogSetting));
} }
t = new Timer(async (state) => timerReference = new Timer(async (state) =>
{ {
try try
{ {
@ -63,8 +62,10 @@ namespace NadekoBot.Modules.Administration
_log.Warn(ex); _log.Warn(ex);
} }
}, null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10)); }, null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10));
}
public LogCommands(ShardedDiscordClient client)
{
//_client.MessageReceived += _client_MessageReceived; //_client.MessageReceived += _client_MessageReceived;
_client.MessageUpdated += _client_MessageUpdated; _client.MessageUpdated += _client_MessageUpdated;
_client.MessageDeleted += _client_MessageDeleted; _client.MessageDeleted += _client_MessageDeleted;
@ -81,6 +82,36 @@ namespace NadekoBot.Modules.Administration
_client.ChannelUpdated += _client_ChannelUpdated; _client.ChannelUpdated += _client_ChannelUpdated;
} }
public static async Task TriggeredAntiProtection(IGuildUser[] users, PunishmentAction action, ProtectionType protection)
{
if (users.Length == 0)
return;
LogSetting logSetting;
if (!GuildLogSettings.TryGetValue(users.First().Guild.Id, out logSetting)
|| !logSetting.IsLogging)
return;
ITextChannel logChannel;
if ((logChannel = TryGetLogChannel(users.First().Guild, logSetting)) == null)
return;
var punishment = "";
if (action == PunishmentAction.Mute)
{
punishment = "MUTED";
}
else if (action == PunishmentAction.Kick)
{
punishment = "KICKED";
}
else if (action == PunishmentAction.Ban)
{
punishment = "BANNED";
}
await logChannel.SendMessageAsync(String.Join("\n",users.Select(user=>$"{Format.Bold(user.ToString())} was **{punishment}** due to `{protection}` protection on **{user.Guild.Name}** server.")))
.ConfigureAwait(false);
}
private Task _client_UserUpdated(IGuildUser before, IGuildUser after) private Task _client_UserUpdated(IGuildUser before, IGuildUser after)
{ {
LogSetting logSetting; LogSetting logSetting;
@ -461,7 +492,7 @@ namespace NadekoBot.Modules.Administration
// } // }
private enum LogChannelType { Text, Voice, UserPresence }; private enum LogChannelType { Text, Voice, UserPresence };
private ITextChannel TryGetLogChannel(IGuild guild, LogSetting logSetting, LogChannelType logChannelType = LogChannelType.Text) private static ITextChannel TryGetLogChannel(IGuild guild, LogSetting logSetting, LogChannelType logChannelType = LogChannelType.Text)
{ {
ulong id = 0; ulong id = 0;
switch (logChannelType) switch (logChannelType)