2017-01-10 19:40:52 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using NadekoBot.Extensions;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
2017-01-10 19:40:52 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.Attributes;
|
|
|
|
|
using NadekoBot.Modules.Administration.Common;
|
|
|
|
|
using NadekoBot.Modules.Administration.Services;
|
2017-01-10 19:40:52 +00:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.Administration
|
|
|
|
|
{
|
|
|
|
|
public partial class Administration
|
|
|
|
|
{
|
|
|
|
|
[Group]
|
2017-07-15 16:34:34 +00:00
|
|
|
|
public class ProtectionCommands : NadekoSubmodule<ProtectionService>
|
2017-01-10 19:40:52 +00:00
|
|
|
|
{
|
2017-05-27 17:42:23 +00:00
|
|
|
|
private readonly MuteService _mute;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly DbService _db;
|
2017-01-10 19:40:52 +00:00
|
|
|
|
|
2017-07-15 16:34:34 +00:00
|
|
|
|
public ProtectionCommands(MuteService mute, DbService db)
|
2017-01-10 19:40:52 +00:00
|
|
|
|
{
|
2017-05-27 17:42:23 +00:00
|
|
|
|
_mute = mute;
|
|
|
|
|
_db = db;
|
2017-01-10 19:40:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 19:26:48 +00:00
|
|
|
|
private string GetAntiSpamString(AntiSpamStats stats)
|
|
|
|
|
{
|
2017-09-10 20:44:23 +00:00
|
|
|
|
var settings = stats.AntiSpamSettings;
|
|
|
|
|
var ignoredString = string.Join(", ", settings.IgnoredChannels.Select(c => $"<#{c.ChannelId}>"));
|
2017-02-14 19:26:48 +00:00
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(ignoredString))
|
|
|
|
|
ignoredString = "none";
|
2017-08-15 23:12:18 +00:00
|
|
|
|
|
|
|
|
|
string add = "";
|
2017-09-10 20:44:23 +00:00
|
|
|
|
if (settings.Action == PunishmentAction.Mute
|
|
|
|
|
&& settings.MuteTime > 0)
|
2017-08-15 23:12:18 +00:00
|
|
|
|
{
|
2017-09-10 20:44:23 +00:00
|
|
|
|
add = " (" + settings.MuteTime + "s)";
|
2017-08-15 23:12:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 19:26:48 +00:00
|
|
|
|
return GetText("spam_stats",
|
2017-09-10 20:44:23 +00:00
|
|
|
|
Format.Bold(settings.MessageThreshold.ToString()),
|
|
|
|
|
Format.Bold(settings.Action.ToString() + add),
|
2017-02-14 19:26:48 +00:00
|
|
|
|
ignoredString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetAntiRaidString(AntiRaidStats stats) => GetText("raid_stats",
|
|
|
|
|
Format.Bold(stats.AntiRaidSettings.UserThreshold.ToString()),
|
|
|
|
|
Format.Bold(stats.AntiRaidSettings.Seconds.ToString()),
|
|
|
|
|
Format.Bold(stats.AntiRaidSettings.Action.ToString()));
|
2017-01-10 19:40:52 +00:00
|
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
[RequireUserPermission(GuildPermission.Administrator)]
|
|
|
|
|
public async Task AntiRaid(int userThreshold = 5, int seconds = 10, PunishmentAction action = PunishmentAction.Mute)
|
|
|
|
|
{
|
|
|
|
|
if (userThreshold < 2 || userThreshold > 30)
|
|
|
|
|
{
|
2017-02-14 19:26:48 +00:00
|
|
|
|
await ReplyErrorLocalized("raid_cnt", 2, 30).ConfigureAwait(false);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (seconds < 2 || seconds > 300)
|
|
|
|
|
{
|
2017-02-14 19:26:48 +00:00
|
|
|
|
await ReplyErrorLocalized("raid_time", 2, 300).ConfigureAwait(false);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-08-06 16:01:10 +00:00
|
|
|
|
|
2017-07-25 16:31:30 +00:00
|
|
|
|
if (_service.AntiRaidGuilds.TryRemove(Context.Guild.Id, out _))
|
2017-01-10 19:40:52 +00:00
|
|
|
|
{
|
2017-05-27 17:42:23 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-01-10 19:40:52 +00:00
|
|
|
|
{
|
|
|
|
|
var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiRaidSetting));
|
|
|
|
|
|
|
|
|
|
gc.AntiRaidSetting = null;
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
2017-02-14 19:26:48 +00:00
|
|
|
|
await ReplyConfirmLocalized("prot_disable", "Anti-Raid").ConfigureAwait(false);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-05-27 17:42:23 +00:00
|
|
|
|
await _mute.GetMuteRole(Context.Guild).ConfigureAwait(false);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn(ex);
|
2017-02-14 19:26:48 +00:00
|
|
|
|
await ReplyErrorLocalized("prot_error").ConfigureAwait(false);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var stats = new AntiRaidStats()
|
|
|
|
|
{
|
|
|
|
|
AntiRaidSettings = new AntiRaidSetting()
|
|
|
|
|
{
|
|
|
|
|
Action = action,
|
|
|
|
|
Seconds = seconds,
|
|
|
|
|
UserThreshold = userThreshold,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-27 17:42:23 +00:00
|
|
|
|
_service.AntiRaidGuilds.AddOrUpdate(Context.Guild.Id, stats, (key, old) => stats);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
|
2017-05-27 17:42:23 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-01-10 19:40:52 +00:00
|
|
|
|
{
|
|
|
|
|
var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiRaidSetting));
|
|
|
|
|
|
|
|
|
|
gc.AntiRaidSetting = stats.AntiRaidSettings;
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 19:26:48 +00:00
|
|
|
|
await Context.Channel.SendConfirmAsync(GetText("prot_enable", "Anti-Raid"), $"{Context.User.Mention} {GetAntiRaidString(stats)}")
|
2017-01-10 19:40:52 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
[RequireUserPermission(GuildPermission.Administrator)]
|
2017-08-06 16:01:10 +00:00
|
|
|
|
[Priority(1)]
|
|
|
|
|
public async Task AntiSpam()
|
2017-01-10 19:40:52 +00:00
|
|
|
|
{
|
2017-07-25 16:31:30 +00:00
|
|
|
|
if (_service.AntiSpamGuilds.TryRemove(Context.Guild.Id, out var removed))
|
2017-01-10 19:40:52 +00:00
|
|
|
|
{
|
2017-07-25 16:31:30 +00:00
|
|
|
|
removed.UserStats.ForEach(x => x.Value.Dispose());
|
2017-05-27 17:42:23 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-01-10 19:40:52 +00:00
|
|
|
|
{
|
|
|
|
|
var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiSpamSetting)
|
|
|
|
|
.ThenInclude(x => x.IgnoredChannels));
|
|
|
|
|
|
|
|
|
|
gc.AntiSpamSetting = null;
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
2017-02-14 19:26:48 +00:00
|
|
|
|
await ReplyConfirmLocalized("prot_disable", "Anti-Spam").ConfigureAwait(false);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 16:01:10 +00:00
|
|
|
|
await AntiSpam(3).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
[RequireUserPermission(GuildPermission.Administrator)]
|
|
|
|
|
[Priority(0)]
|
|
|
|
|
public async Task AntiSpam(int messageCount, PunishmentAction action = PunishmentAction.Mute, int time = 0)
|
|
|
|
|
{
|
|
|
|
|
if (messageCount < 2 || messageCount > 10)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (time < 0 || time > 60 * 12)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-01-10 19:40:52 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-05-27 17:42:23 +00:00
|
|
|
|
await _mute.GetMuteRole(Context.Guild).ConfigureAwait(false);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn(ex);
|
2017-02-14 19:26:48 +00:00
|
|
|
|
await ReplyErrorLocalized("prot_error").ConfigureAwait(false);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var stats = new AntiSpamStats
|
|
|
|
|
{
|
|
|
|
|
AntiSpamSettings = new AntiSpamSetting()
|
|
|
|
|
{
|
|
|
|
|
Action = action,
|
|
|
|
|
MessageThreshold = messageCount,
|
2017-08-15 23:12:18 +00:00
|
|
|
|
MuteTime = time,
|
2017-01-10 19:40:52 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-09-10 20:44:23 +00:00
|
|
|
|
stats = _service.AntiSpamGuilds.AddOrUpdate(Context.Guild.Id, stats, (key, old) =>
|
2017-08-06 16:01:10 +00:00
|
|
|
|
{
|
2017-09-10 20:44:23 +00:00
|
|
|
|
stats.AntiSpamSettings.IgnoredChannels = old.AntiSpamSettings.IgnoredChannels;
|
2017-08-06 16:01:10 +00:00
|
|
|
|
return stats;
|
|
|
|
|
});
|
2017-01-10 19:40:52 +00:00
|
|
|
|
|
2017-05-27 17:42:23 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-01-10 19:40:52 +00:00
|
|
|
|
{
|
|
|
|
|
var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiSpamSetting));
|
|
|
|
|
|
|
|
|
|
gc.AntiSpamSetting = stats.AntiSpamSettings;
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 19:26:48 +00:00
|
|
|
|
await Context.Channel.SendConfirmAsync(GetText("prot_enable", "Anti-Spam"), $"{Context.User.Mention} {GetAntiSpamString(stats)}").ConfigureAwait(false);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2017-09-24 22:36:31 +00:00
|
|
|
|
[RequireUserPermission(GuildPermission.Administrator)]
|
2017-01-10 19:40:52 +00:00
|
|
|
|
public async Task AntispamIgnore()
|
|
|
|
|
{
|
|
|
|
|
var channel = (ITextChannel)Context.Channel;
|
|
|
|
|
|
|
|
|
|
var obj = new AntiSpamIgnore()
|
|
|
|
|
{
|
|
|
|
|
ChannelId = channel.Id
|
|
|
|
|
};
|
|
|
|
|
bool added;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-01-10 19:40:52 +00:00
|
|
|
|
{
|
|
|
|
|
var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiSpamSetting).ThenInclude(x => x.IgnoredChannels));
|
|
|
|
|
var spam = gc.AntiSpamSetting;
|
|
|
|
|
if (spam == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (spam.IgnoredChannels.Add(obj))
|
|
|
|
|
{
|
|
|
|
|
AntiSpamStats temp;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
if (_service.AntiSpamGuilds.TryGetValue(Context.Guild.Id, out temp))
|
2017-01-10 19:40:52 +00:00
|
|
|
|
temp.AntiSpamSettings.IgnoredChannels.Add(obj);
|
|
|
|
|
added = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
spam.IgnoredChannels.Remove(obj);
|
|
|
|
|
AntiSpamStats temp;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
if (_service.AntiSpamGuilds.TryGetValue(Context.Guild.Id, out temp))
|
2017-01-10 19:40:52 +00:00
|
|
|
|
temp.AntiSpamSettings.IgnoredChannels.Remove(obj);
|
|
|
|
|
added = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
if (added)
|
2017-02-14 19:26:48 +00:00
|
|
|
|
await ReplyConfirmLocalized("spam_ignore", "Anti-Spam").ConfigureAwait(false);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
else
|
2017-02-14 19:26:48 +00:00
|
|
|
|
await ReplyConfirmLocalized("spam_not_ignore", "Anti-Spam").ConfigureAwait(false);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
public async Task AntiList()
|
|
|
|
|
{
|
|
|
|
|
AntiSpamStats spam;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
_service.AntiSpamGuilds.TryGetValue(Context.Guild.Id, out spam);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
|
|
|
|
|
AntiRaidStats raid;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
_service.AntiRaidGuilds.TryGetValue(Context.Guild.Id, out raid);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
|
|
|
|
|
if (spam == null && raid == null)
|
|
|
|
|
{
|
2017-02-14 19:26:48 +00:00
|
|
|
|
await ReplyConfirmLocalized("prot_none").ConfigureAwait(false);
|
2017-01-10 19:40:52 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var embed = new EmbedBuilder().WithOkColor()
|
2017-02-14 19:26:48 +00:00
|
|
|
|
.WithTitle(GetText("prot_active"));
|
2017-01-10 19:40:52 +00:00
|
|
|
|
|
|
|
|
|
if (spam != null)
|
|
|
|
|
embed.AddField(efb => efb.WithName("Anti-Spam")
|
2017-02-14 19:28:43 +00:00
|
|
|
|
.WithValue(GetAntiSpamString(spam))
|
2017-01-10 19:40:52 +00:00
|
|
|
|
.WithIsInline(true));
|
|
|
|
|
|
|
|
|
|
if (raid != null)
|
|
|
|
|
embed.AddField(efb => efb.WithName("Anti-Raid")
|
2017-02-14 19:28:43 +00:00
|
|
|
|
.WithValue(GetAntiRaidString(raid))
|
2017-01-10 19:40:52 +00:00
|
|
|
|
.WithIsInline(true));
|
|
|
|
|
|
|
|
|
|
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|