Invite filtering works

This commit is contained in:
Kwoth 2016-10-03 19:21:05 +02:00
parent 0df0eea6c0
commit f7b3b67197
6 changed files with 201 additions and 76 deletions

View File

@ -177,6 +177,42 @@ namespace NadekoBot.Migrations
b.ToTable("EightBallResponses");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.FilterChannelId", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<ulong>("ChannelId");
b.Property<int?>("GuildConfigId");
b.Property<int?>("GuildConfigId1");
b.HasKey("Id");
b.HasIndex("GuildConfigId");
b.HasIndex("GuildConfigId1");
b.ToTable("FilterChannelId");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.FilteredWord", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<int?>("GuildConfigId");
b.Property<string>("Word");
b.HasKey("Id");
b.HasIndex("GuildConfigId");
b.ToTable("FilteredWord");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.FollowedStream", b =>
{
b.Property<int>("Id")
@ -230,6 +266,10 @@ namespace NadekoBot.Migrations
b.Property<bool>("ExclusiveSelfAssignedRoles");
b.Property<bool>("FilterInvites");
b.Property<bool>("FilterWords");
b.Property<ulong?>("GenerateCurrencyChannelId");
b.Property<ulong>("GreetMessageChannelId");
@ -576,6 +616,24 @@ namespace NadekoBot.Migrations
.HasForeignKey("BotConfigId");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.FilterChannelId", b =>
{
b.HasOne("NadekoBot.Services.Database.Models.GuildConfig")
.WithMany("FilterInvitesChannelIds")
.HasForeignKey("GuildConfigId");
b.HasOne("NadekoBot.Services.Database.Models.GuildConfig")
.WithMany("FilterWordsChannelIds")
.HasForeignKey("GuildConfigId1");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.FilteredWord", b =>
{
b.HasOne("NadekoBot.Services.Database.Models.GuildConfig")
.WithMany("FilteredWords")
.HasForeignKey("GuildConfigId");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.FollowedStream", b =>
{
b.HasOne("NadekoBot.Services.Database.Models.GuildConfig")

View File

@ -13,6 +13,7 @@ using NadekoBot.Services.Database.Models;
using NadekoBot.Modules.Permissions;
using Microsoft.Data.Sqlite;
using Discord.Net;
using NadekoBot.Extensions;
namespace NadekoBot.Services
{
@ -33,14 +34,14 @@ namespace NadekoBot.Services
_client.MessageReceived += MessageReceivedHandler;
}
private Task MessageReceivedHandler(IMessage msg)
private async Task MessageReceivedHandler(IMessage msg)
{
var usrMsg = msg as IUserMessage;
if (usrMsg == null)
return Task.CompletedTask;
return;
if (usrMsg.Author.IsBot) //no bots
return Task.CompletedTask;
return;
var guild = (msg.Channel as ITextChannel)?.Guild;
@ -51,17 +52,45 @@ namespace NadekoBot.Services
(bi.Type == BlacklistItem.BlacklistType.User && bi.ItemId == usrMsg.Author.Id))) != null)
{
_log.Warn("Attempt was made to run a command by a blacklisted {0}, id: {1}", blacklistedItem.Type, blacklistedItem.ItemId);
return Task.CompletedTask;
return;
}
var throwaway = Task.Run(async () =>
if (guild != null)
{
var sw = new Stopwatch();
sw.Start();
if (Permissions.FilterCommands.InviteFilteringChannels.Contains(usrMsg.Channel.Id) ||
Permissions.FilterCommands.InviteFilteringServers.Contains(guild.Id))
{
if (usrMsg.Content.IsDiscordInvite())
{
try
{
await usrMsg.DeleteAsync().ConfigureAwait(false);
return;
}
catch (HttpException ex)
{
_log.Warn("I do not have permission to filter invites in channel with id " + usrMsg.Channel.Id, ex);
}
}
}
var filteredWords = Permissions.FilterCommands.FilteredWordsForChannel(usrMsg.Channel.Id, guild.Id).Concat(Permissions.FilterCommands.FilteredWordsForServer(guild.Id));
var wordsInMessage = usrMsg.Content.ToLowerInvariant().Split(' ');
if (filteredWords.Any())
{
try
{
await usrMsg.DeleteAsync().ConfigureAwait(false);
return;
}
catch (HttpException ex)
{
_log.Warn("I do not have permission to filter words in channel with id " + usrMsg.Channel.Id, ex);
}
}
}
try
{
bool verbose = false;
Permission rootPerm = null;
string permRole = "";
@ -74,9 +103,17 @@ namespace NadekoBot.Services
rootPerm = config.RootPermission;
permRole = config.PermissionRole.Trim().ToLowerInvariant();
}
}
var throwaway = Task.Run(async () =>
{
var sw = new Stopwatch();
sw.Start();
try
{
var t = await ExecuteCommand(usrMsg, usrMsg.Content, guild, usrMsg.Author, rootPerm, permRole, MultiMatchHandling.Best);
var command = t.Item1;
var result = t.Item2;
@ -122,12 +159,17 @@ namespace NadekoBot.Services
catch (Exception ex)
{
_log.Warn(ex, "Error in CommandHandler");
if(ex.InnerException != null)
if (ex.InnerException != null)
_log.Warn(ex.InnerException, "Inner Exception of the error in CommandHandler");
}
});
return Task.CompletedTask;
}
catch (Exception ex)
{
_log.Error(ex, "Error in the outter scope of the commandhandler.");
if (ex.InnerException != null)
_log.Error(ex.InnerException, "Inner exception: ");
}
}
public async Task<Tuple<Command,IResult>> ExecuteCommand(IUserMessage message, string input, IGuild guild, IUser user, Permission rootPerm, string permRole, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Best) {

View File

@ -46,5 +46,23 @@ namespace NadekoBot.Services.Database.Models
public Permission RootPermission { get; set; }
public bool VerbosePermissions { get; set; }
public string PermissionRole { get; set; } = "Nadeko";
//filtering
public bool FilterInvites { get; set; }
public HashSet<FilterChannelId> FilterInvitesChannelIds { get; set; }
public bool FilterWords { get; set; }
public HashSet<FilteredWord> FilteredWords { get; set; }
public HashSet<FilterChannelId> FilterWordsChannelIds { get; set; }
}
public class FilterChannelId :DbEntity
{
public ulong ChannelId { get; set; }
}
public class FilteredWord :DbEntity
{
public string Word { get; set; }
}
}

View File

@ -23,6 +23,9 @@ namespace NadekoBot.Services.Database.Repositories.Impl
.ThenInclude(gc => gc.Previous)
.Include(gc => gc.RootPermission)
.ThenInclude(gc => gc.Next)
.Include(gc => gc.FilterInvitesChannelIds)
.Include(gc => gc.FilterWordsChannelIds)
.Include(gc => gc.FilteredWords)
.ToList();
/// <summary>
@ -37,6 +40,9 @@ namespace NadekoBot.Services.Database.Repositories.Impl
.ThenInclude(ls => ls.IgnoredChannels)
.Include(gc => gc.LogSetting)
.ThenInclude(ls => ls.IgnoredVoicePresenceChannelIds)
.Include(gc => gc.FilterInvitesChannelIds)
.Include(gc => gc.FilterWordsChannelIds)
.Include(gc => gc.FilteredWords)
.FirstOrDefault(c => c.GuildId == guildId);
if (config == null)

View File

@ -298,5 +298,10 @@ namespace NadekoBot.Extensions
imageStream.Position = 0;
return imageStream;
}
private static readonly Regex filterRegex = new Regex(@"(?:discord(?:\.gg|app\.com\/invite)\/(?<id>([\w]{16}|(?:[\w]+-?){3})))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public static bool IsDiscordInvite(this string str)
=> filterRegex.IsMatch(str);
}
}

View File

@ -40,9 +40,11 @@ namespace Tests
root.Prepend(new Permission() { SecondaryTargetName = "Added" });
root = root.GetRoot();
Assert.Equal(11, root.Count());
Assert.Equal("Added", root.AsEnumerable().Last().SecondaryTargetName);
Assert.Equal("Added", root.AsEnumerable().First().SecondaryTargetName);
}
[Fact]
@ -87,15 +89,9 @@ namespace Tests
Assert.Equal("3", removed.SecondaryTargetName);
Assert.Equal(9, root.Count());
var temp = root.Next;
removed = root.RemoveAt(0);
Assert.Equal(8, temp.Count());
Assert.Equal(null, temp.Previous);
Assert.Throws(typeof(IndexOutOfRangeException), () => { temp.RemoveAt(8); });
Assert.Throws(typeof(IndexOutOfRangeException), () => { temp.RemoveAt(-1); });
Assert.Throws(typeof(IndexOutOfRangeException), () => { root.RemoveAt(0); });
Assert.Throws(typeof(IndexOutOfRangeException), () => { root.RemoveAt(9); });
Assert.Throws(typeof(IndexOutOfRangeException), () => { root.RemoveAt(-1); });
}
[Fact]