wordfilter, invitefilter and linkfilter will now properly detect forwarded messages, as forwards were used to circumvent filtering.

Fixed .streamremove - now showing proper youtube name when removing instead of channel id
This commit is contained in:
Toastie 2025-03-20 21:32:15 +13:00
parent 4ec6396c87
commit 91d464b041
Signed by: toastie_t0ast
GPG key ID: 0861BE54AD481DC7
2 changed files with 21 additions and 15 deletions
src/EllieBot/Modules
Permissions/Filter
Searches/StreamNotification

View file

@ -52,12 +52,12 @@ public sealed class FilterService : IExecOnMessage, IReadyExecutor
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
var confs = await uow.GetTable<GuildFilterConfig>() var confs = await uow.GetTable<GuildFilterConfig>()
.Where(x => Queries.GuildOnShard(x.GuildId, _shardData.TotalShards, _shardData.ShardId)) .Where(x => Queries.GuildOnShard(x.GuildId, _shardData.TotalShards, _shardData.ShardId))
.LoadWith(x => x.FilterInvitesChannelIds) .LoadWith(x => x.FilterInvitesChannelIds)
.LoadWith(x => x.FilterWordsChannelIds) .LoadWith(x => x.FilterWordsChannelIds)
.LoadWith(x => x.FilterLinksChannelIds) .LoadWith(x => x.FilterLinksChannelIds)
.LoadWith(x => x.FilteredWords) .LoadWith(x => x.FilteredWords)
.ToListAsyncLinqToDB(); .ToListAsyncLinqToDB();
foreach (var conf in confs) foreach (var conf in confs)
{ {
@ -97,7 +97,7 @@ public sealed class FilterService : IExecOnMessage, IReadyExecutor
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
var fc = uow.FilterConfigForId(guildId, var fc = uow.FilterConfigForId(guildId,
set => set.Include(x => x.FilteredWords) set => set.Include(x => x.FilteredWords)
.Include(x => x.FilterWordsChannelIds)); .Include(x => x.FilterWordsChannelIds));
WordFilteringServers.TryRemove(guildId); WordFilteringServers.TryRemove(guildId);
ServerFilteredWords.TryRemove(guildId, out _); ServerFilteredWords.TryRemove(guildId, out _);
@ -140,7 +140,8 @@ public sealed class FilterService : IExecOnMessage, IReadyExecutor
var filteredChannelWords = var filteredChannelWords =
FilteredWordsForChannel(usrMsg.Channel.Id, guild.Id) ?? new ConcurrentHashSet<string>(); FilteredWordsForChannel(usrMsg.Channel.Id, guild.Id) ?? new ConcurrentHashSet<string>();
var filteredServerWords = FilteredWordsForServer(guild.Id) ?? new ConcurrentHashSet<string>(); var filteredServerWords = FilteredWordsForServer(guild.Id) ?? new ConcurrentHashSet<string>();
var wordsInMessage = usrMsg.Content.ToLowerInvariant().Split(' '); var wordsInMessage = (usrMsg.Content + " " + usrMsg.ForwardedMessages.FirstOrDefault().Message?.Content)
.ToLowerInvariant().Split(' ');
if (filteredChannelWords.Count != 0 || filteredServerWords.Count != 0) if (filteredChannelWords.Count != 0 || filteredServerWords.Count != 0)
{ {
foreach (var word in wordsInMessage) foreach (var word in wordsInMessage)
@ -183,7 +184,8 @@ public sealed class FilterService : IExecOnMessage, IReadyExecutor
return false; return false;
if ((InviteFilteringChannels.Contains(usrMsg.Channel.Id) || InviteFilteringServers.Contains(guild.Id)) if ((InviteFilteringChannels.Contains(usrMsg.Channel.Id) || InviteFilteringServers.Contains(guild.Id))
&& usrMsg.Content.IsDiscordInvite()) && (usrMsg.Content.IsDiscordInvite() ||
usrMsg.ForwardedMessages.Any(x => x.Message?.Content.IsDiscordInvite() ?? false)))
{ {
Log.Information("User {UserName} [{UserId}] sent a filtered invite to {ChannelId} channel", Log.Information("User {UserName} [{UserId}] sent a filtered invite to {ChannelId} channel",
usrMsg.Author.ToString(), usrMsg.Author.ToString(),
@ -219,7 +221,8 @@ public sealed class FilterService : IExecOnMessage, IReadyExecutor
return false; return false;
if ((LinkFilteringChannels.Contains(usrMsg.Channel.Id) || LinkFilteringServers.Contains(guild.Id)) if ((LinkFilteringChannels.Contains(usrMsg.Channel.Id) || LinkFilteringServers.Contains(guild.Id))
&& usrMsg.Content.TryGetUrlPath(out _)) && (usrMsg.Content.TryGetUrlPath(out _) ||
usrMsg.ForwardedMessages.Any(x => x.Message?.Content.TryGetUrlPath(out _) ?? false)))
{ {
Log.Information("User {UserName} [{UserId}] sent a filtered link to {ChannelId} channel", Log.Information("User {UserName} [{UserId}] sent a filtered link to {ChannelId} channel",
usrMsg.Author.ToString(), usrMsg.Author.ToString(),
@ -246,10 +249,10 @@ public sealed class FilterService : IExecOnMessage, IReadyExecutor
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
var conf = await uow.GetTable<GuildFilterConfig>() var conf = await uow.GetTable<GuildFilterConfig>()
.Where(fi => fi.GuildId == guildId) .Where(fi => fi.GuildId == guildId)
.LoadWith(x => x.FilterInvitesChannelIds) .LoadWith(x => x.FilterInvitesChannelIds)
.LoadWith(x => x.FilterLinksChannelIds) .LoadWith(x => x.FilterLinksChannelIds)
.FirstOrDefaultAsyncLinqToDB(); .FirstOrDefaultAsyncLinqToDB();
return new() return new()
{ {

View file

@ -48,7 +48,10 @@ public partial class Searches
return; return;
} }
await Response().Confirm(strs.stream_removed(Format.Bold(fs.Username), fs.Type)).SendAsync(); await Response()
.Confirm(strs.stream_removed(
Format.Bold(string.IsNullOrWhiteSpace(fs.PrettyName) ? fs.Username : fs.PrettyName),
fs.Type)).SendAsync();
} }
[Cmd] [Cmd]