mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2025-06-02 23:59:50 -04:00
Add support for has:invite
filter that matches messages with guild invites (#1188)
This commit is contained in:
parent
4e69ff317b
commit
d8e43d89be
4 changed files with 56 additions and 15 deletions
|
@ -1,7 +1,7 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using DiscordChatExporter.Core.Discord.Data;
|
||||
using DiscordChatExporter.Core.Markdown.Parsing;
|
||||
|
||||
namespace DiscordChatExporter.Core.Exporting.Filtering;
|
||||
|
||||
|
@ -10,14 +10,19 @@ internal class HasMessageFilter(MessageContentMatchKind kind) : MessageFilter
|
|||
public override bool IsMatch(Message message) =>
|
||||
kind switch
|
||||
{
|
||||
MessageContentMatchKind.Link
|
||||
=> Regex.IsMatch(message.Content, "https?://\\S*[^\\.,:;\"\'\\s]"),
|
||||
MessageContentMatchKind.Link => MarkdownParser.ExtractLinks(message.Content).Any(),
|
||||
MessageContentMatchKind.Embed => message.Embeds.Any(),
|
||||
MessageContentMatchKind.File => message.Attachments.Any(),
|
||||
MessageContentMatchKind.Video => message.Attachments.Any(file => file.IsVideo),
|
||||
MessageContentMatchKind.Image => message.Attachments.Any(file => file.IsImage),
|
||||
MessageContentMatchKind.Sound => message.Attachments.Any(file => file.IsAudio),
|
||||
MessageContentMatchKind.Pin => message.IsPinned,
|
||||
MessageContentMatchKind.Invite
|
||||
=> MarkdownParser
|
||||
.ExtractLinks(message.Content)
|
||||
.Select(l => l.Url)
|
||||
.Select(Invite.TryGetCodeFromUrl)
|
||||
.Any(c => !string.IsNullOrWhiteSpace(c)),
|
||||
_
|
||||
=> throw new InvalidOperationException(
|
||||
$"Unknown message content match kind '{kind}'."
|
||||
|
|
|
@ -8,5 +8,6 @@ internal enum MessageContentMatchKind
|
|||
Video,
|
||||
Image,
|
||||
Sound,
|
||||
Pin
|
||||
Pin,
|
||||
Invite
|
||||
}
|
||||
|
|
|
@ -61,18 +61,28 @@ internal static class FilterGrammar
|
|||
.IgnoreThen(
|
||||
Parse.OneOf(
|
||||
Span.EqualToIgnoreCase("link")
|
||||
.IgnoreThen(Parse.Return(MessageContentMatchKind.Link)),
|
||||
.IgnoreThen(Parse.Return(MessageContentMatchKind.Link))
|
||||
.Try(),
|
||||
Span.EqualToIgnoreCase("embed")
|
||||
.IgnoreThen(Parse.Return(MessageContentMatchKind.Embed)),
|
||||
.IgnoreThen(Parse.Return(MessageContentMatchKind.Embed))
|
||||
.Try(),
|
||||
Span.EqualToIgnoreCase("file")
|
||||
.IgnoreThen(Parse.Return(MessageContentMatchKind.File)),
|
||||
.IgnoreThen(Parse.Return(MessageContentMatchKind.File))
|
||||
.Try(),
|
||||
Span.EqualToIgnoreCase("video")
|
||||
.IgnoreThen(Parse.Return(MessageContentMatchKind.Video)),
|
||||
.IgnoreThen(Parse.Return(MessageContentMatchKind.Video))
|
||||
.Try(),
|
||||
Span.EqualToIgnoreCase("image")
|
||||
.IgnoreThen(Parse.Return(MessageContentMatchKind.Image)),
|
||||
.IgnoreThen(Parse.Return(MessageContentMatchKind.Image))
|
||||
.Try(),
|
||||
Span.EqualToIgnoreCase("sound")
|
||||
.IgnoreThen(Parse.Return(MessageContentMatchKind.Sound)),
|
||||
Span.EqualToIgnoreCase("pin").IgnoreThen(Parse.Return(MessageContentMatchKind.Pin))
|
||||
Span.EqualToIgnoreCase("pin")
|
||||
.IgnoreThen(Parse.Return(MessageContentMatchKind.Pin))
|
||||
.Try(),
|
||||
Span.EqualToIgnoreCase("invite")
|
||||
.IgnoreThen(Parse.Return(MessageContentMatchKind.Invite))
|
||||
.Try()
|
||||
)
|
||||
)
|
||||
.Select(k => (MessageFilter)new HasMessageFilter(k))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue