diff --git a/HighlightBot/CommandSession.cs b/HighlightBot/CommandSession.cs index 33a1a1c..8837b01 100644 --- a/HighlightBot/CommandSession.cs +++ b/HighlightBot/CommandSession.cs @@ -109,59 +109,52 @@ public async Task ClearHighlights(HighlightCommandContext hcc) { await hcc.RespondAsync("Deleted all your highlights."); } } - - public async Task AddHighlight(HighlightCommandContext hcc, ICollection terms) { - static bool IsRegex(string term, out string regex, out string suffix) { - if (term.StartsWith("/")) { - if (term.EndsWith("/")) { - suffix = ""; - regex = term[1..^1]; - return true; - } else if (term.EndsWith("/c")) { - suffix = "c"; - regex = term[1..^2]; - return true; - } + + private static bool ParseRegex(string term, out string regex, out bool isCaseSensitive) { + if (term.StartsWith("/")) { + if (term.EndsWith("/")) { + isCaseSensitive = false; + regex = term[1..^1]; + return true; + } else if (term.EndsWith("/c")) { + isCaseSensitive = true; + regex = term[1..^2]; + return true; } - - suffix = ""; - regex = term; - return false; } - + + isCaseSensitive = false; + regex = $@"\b{Regex.Escape(term.ToLower())}\b"; + return false; + } + + public async Task AddHighlight(HighlightCommandContext hcc, ICollection terms) { HighlightUser user = await GetOrCreateUserAsync(hcc); int added = 0; foreach (string term in terms) { string display; - RegexOptions regexOptions = RegexOptions.IgnoreCase; - - if (IsRegex(term, out string pattern, out string suffix)) { + if (ParseRegex(term, out string pattern, out bool isCaseSensitive)) { if (!Util.IsValidRegex(pattern)) { await hcc.RespondAsync("Invalid regex 🙁"); return; } display = $"`{term}`"; - - if (suffix == "c") { - regexOptions = RegexOptions.None; - } } else { - pattern = $@"\b{Regex.Escape(term.ToLower())}\b"; - if (!Util.IsValidRegex(pattern)) { throw new Exception("Escaped pattern is not valid: " + pattern); } display = term; } + if (!user.Terms.Any(t => t.Regex == pattern)) { added++; user.Terms.Add(new HighlightTerm() { Display = display, Regex = pattern, - RegexOptions = regexOptions, + IsCaseSensitive = isCaseSensitive, }); } } @@ -192,10 +185,8 @@ public async Task RemoveHighlights(HighlightCommandContext hcc, string highlight if (user == null || user.Terms.Count == 0) { await hcc.RespondAsync("You're not tracking any words."); } else { - if (highlight.StartsWith('/') && highlight.EndsWith('/')) { - highlight = $"`{highlight}`"; - } - HighlightTerm? term = user.Terms.FirstOrDefault(term => term.Display == highlight); + ParseRegex(highlight, out string pattern, out bool isCaseSensitive); + HighlightTerm? term = user.Terms.FirstOrDefault(term => term.Regex == pattern && term.IsCaseSensitive == isCaseSensitive); string content; if (term != null) { user.Terms.Remove(term); diff --git a/HighlightBot/Data/HighlightDbContext.cs b/HighlightBot/Data/HighlightDbContext.cs index 740ee0b..d44c2b5 100644 --- a/HighlightBot/Data/HighlightDbContext.cs +++ b/HighlightBot/Data/HighlightDbContext.cs @@ -36,7 +36,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) { .HasKey(nameof(HighlightUser.DiscordGuildId), nameof(HighlightUser.DiscordUserId)); modelBuilder.Entity() - .Property(term => term.RegexOptions) - .HasDefaultValue(RegexOptions.IgnoreCase); + .Property(term => term.IsCaseSensitive) + .HasDefaultValue(false); } } diff --git a/HighlightBot/Data/HighlightTerm.cs b/HighlightBot/Data/HighlightTerm.cs index 0b800e1..752f467 100644 --- a/HighlightBot/Data/HighlightTerm.cs +++ b/HighlightBot/Data/HighlightTerm.cs @@ -8,5 +8,5 @@ public class HighlightTerm { public string Regex { get; set; } public string Display { get; set; } - public RegexOptions RegexOptions { get; set; } + public bool IsCaseSensitive { get; set; } } diff --git a/HighlightBot/Migrations/20240623174533_ExplicitCaseInsensitive.Designer.cs b/HighlightBot/Migrations/20240623174533_ExplicitCaseInsensitive.Designer.cs new file mode 100644 index 0000000..2dcc3d2 --- /dev/null +++ b/HighlightBot/Migrations/20240623174533_ExplicitCaseInsensitive.Designer.cs @@ -0,0 +1,175 @@ +// +using System; +using HighlightBot; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace HighlightBot.Migrations +{ + [DbContext(typeof(HighlightDbContext))] + [Migration("20240623174533_ExplicitCaseInsensitive")] + partial class ExplicitCaseInsensitive + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.6") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("HighlightBot.HighlightTerm", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Display") + .IsRequired() + .HasColumnType("text"); + + b.Property("IsCaseSensitive") + .ValueGeneratedOnAdd() + .HasColumnType("boolean") + .HasDefaultValue(false); + + b.Property("Regex") + .IsRequired() + .HasColumnType("text"); + + b.Property("user_serverid") + .HasColumnType("numeric(20,0)"); + + b.Property("user_userid") + .HasColumnType("numeric(20,0)"); + + b.HasKey("Id"); + + b.HasIndex("user_serverid", "user_userid"); + + b.ToTable("Terms"); + }); + + modelBuilder.Entity("HighlightBot.HighlightUser", b => + { + b.Property("DiscordGuildId") + .HasColumnType("numeric(20,0)"); + + b.Property("DiscordUserId") + .HasColumnType("numeric(20,0)"); + + b.Property("HighlightDelay") + .HasColumnType("interval"); + + b.Property("IgnoreBots") + .HasColumnType("boolean"); + + b.Property("IgnoreNsfw") + .HasColumnType("boolean"); + + b.Property("LastActivity") + .HasColumnType("timestamp with time zone"); + + b.Property("LastDM") + .HasColumnType("timestamp with time zone"); + + b.HasKey("DiscordGuildId", "DiscordUserId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("HighlightBot.HighlightUserIgnoredChannel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ChannelId") + .HasColumnType("numeric(20,0)"); + + b.Property("UserDiscordGuildId") + .HasColumnType("numeric(20,0)"); + + b.Property("UserDiscordUserId") + .HasColumnType("numeric(20,0)"); + + b.HasKey("Id"); + + b.HasIndex("UserDiscordGuildId", "UserDiscordUserId"); + + b.ToTable("HighlightUserIgnoredChannel"); + }); + + modelBuilder.Entity("HighlightBot.HighlightUserIgnoredUser", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("IgnoredUserId") + .HasColumnType("numeric(20,0)"); + + b.Property("UserDiscordGuildId") + .HasColumnType("numeric(20,0)"); + + b.Property("UserDiscordUserId") + .HasColumnType("numeric(20,0)"); + + b.HasKey("Id"); + + b.HasIndex("UserDiscordGuildId", "UserDiscordUserId"); + + b.ToTable("HighlightUserIgnoredUser"); + }); + + modelBuilder.Entity("HighlightBot.HighlightTerm", b => + { + b.HasOne("HighlightBot.HighlightUser", "User") + .WithMany("Terms") + .HasForeignKey("user_serverid", "user_userid") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("HighlightBot.HighlightUserIgnoredChannel", b => + { + b.HasOne("HighlightBot.HighlightUser", "User") + .WithMany("IgnoredChannels") + .HasForeignKey("UserDiscordGuildId", "UserDiscordUserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("HighlightBot.HighlightUserIgnoredUser", b => + { + b.HasOne("HighlightBot.HighlightUser", "User") + .WithMany("IgnoredUsers") + .HasForeignKey("UserDiscordGuildId", "UserDiscordUserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("HighlightBot.HighlightUser", b => + { + b.Navigation("IgnoredChannels"); + + b.Navigation("IgnoredUsers"); + + b.Navigation("Terms"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/HighlightBot/Migrations/20240623174533_ExplicitCaseInsensitive.cs b/HighlightBot/Migrations/20240623174533_ExplicitCaseInsensitive.cs new file mode 100644 index 0000000..c50e618 --- /dev/null +++ b/HighlightBot/Migrations/20240623174533_ExplicitCaseInsensitive.cs @@ -0,0 +1,37 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace HighlightBot.Migrations +{ + public partial class ExplicitCaseInsensitive : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "RegexOptions", + table: "Terms"); + + migrationBuilder.AddColumn( + name: "IsCaseSensitive", + table: "Terms", + type: "boolean", + nullable: false, + defaultValue: false); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "IsCaseSensitive", + table: "Terms"); + + migrationBuilder.AddColumn( + name: "RegexOptions", + table: "Terms", + type: "integer", + nullable: false, + defaultValue: 1); + } + } +} diff --git a/HighlightBot/Migrations/HighlightDbContextModelSnapshot.cs b/HighlightBot/Migrations/HighlightDbContextModelSnapshot.cs index 7a0c07a..4f7f7b9 100644 --- a/HighlightBot/Migrations/HighlightDbContextModelSnapshot.cs +++ b/HighlightBot/Migrations/HighlightDbContextModelSnapshot.cs @@ -32,15 +32,15 @@ protected override void BuildModel(ModelBuilder modelBuilder) .IsRequired() .HasColumnType("text"); + b.Property("IsCaseSensitive") + .ValueGeneratedOnAdd() + .HasColumnType("boolean") + .HasDefaultValue(false); + b.Property("Regex") .IsRequired() .HasColumnType("text"); - b.Property("RegexOptions") - .ValueGeneratedOnAdd() - .HasColumnType("integer") - .HasDefaultValue(1); - b.Property("user_serverid") .HasColumnType("numeric(20,0)"); diff --git a/HighlightBot/Program.cs b/HighlightBot/Program.cs index ac8b6e1..c5f088e 100644 --- a/HighlightBot/Program.cs +++ b/HighlightBot/Program.cs @@ -158,7 +158,7 @@ private static Task OnMessageCreatedAsync(DiscordClient discord, MessageCreateEv // If it happens because the database was actually concurrently, we also don't care. } - string content = e.Message.Content.ToLowerInvariant(); + string content = e.Message.Content; DateTime currentTime = DateTime.UtcNow; DateTime fiveMinutesAgo = DateTime.UtcNow - TimeSpan.FromMinutes(IsDevelopment ? 0 : 5); allTerms = dbContext.Terms @@ -168,7 +168,7 @@ private static Task OnMessageCreatedAsync(DiscordClient discord, MessageCreateEv (!(e.Author.IsBot && term.User.IgnoreBots)) && (!(e.Channel.IsNSFW && term.User.IgnoreNsfw)) && term.User.LastActivity + term.User.HighlightDelay < currentTime && - term.User.LastDM < fiveMinutesAgo && + //term.User.LastDM < fiveMinutesAgo && !term.User.IgnoredChannels.Any(huic => huic.ChannelId == e.Channel.Id) && !term.User.IgnoredUsers.Any(huiu => huiu.IgnoredUserId == e.Author.Id) ) @@ -177,10 +177,10 @@ private static Task OnMessageCreatedAsync(DiscordClient discord, MessageCreateEv term.User.DiscordGuildId, term.Regex, term.Display, - term.RegexOptions, + term.IsCaseSensitive, }) .AsEnumerable() - .Where(term => Regex.IsMatch(content, term.Regex, term.RegexOptions)) + .Where(term => Regex.IsMatch(content, term.Regex, term.IsCaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase)) .Select(term => new UserIdAndTerm() { DiscordUserId = term.DiscordUserId, Value = term.Display