Skip to content

Commit

Permalink
fixed for real
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxite committed Jun 23, 2024
1 parent 5fa0e82 commit 71dcb34
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 44 deletions.
55 changes: 23 additions & 32 deletions HighlightBot/CommandSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,59 +109,52 @@ public async Task ClearHighlights(HighlightCommandContext hcc) {
await hcc.RespondAsync("Deleted all your highlights.");
}
}

public async Task AddHighlight(HighlightCommandContext hcc, ICollection<string> 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<string> 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,
});
}
}
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions HighlightBot/Data/HighlightDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) {
.HasKey(nameof(HighlightUser.DiscordGuildId), nameof(HighlightUser.DiscordUserId));

modelBuilder.Entity<HighlightTerm>()
.Property(term => term.RegexOptions)
.HasDefaultValue(RegexOptions.IgnoreCase);
.Property(term => term.IsCaseSensitive)
.HasDefaultValue(false);
}
}
2 changes: 1 addition & 1 deletion HighlightBot/Data/HighlightTerm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions HighlightBot/Migrations/20240623174533_ExplicitCaseInsensitive.cs
Original file line number Diff line number Diff line change
@@ -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<bool>(
name: "IsCaseSensitive",
table: "Terms",
type: "boolean",
nullable: false,
defaultValue: false);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "IsCaseSensitive",
table: "Terms");

migrationBuilder.AddColumn<int>(
name: "RegexOptions",
table: "Terms",
type: "integer",
nullable: false,
defaultValue: 1);
}
}
}
10 changes: 5 additions & 5 deletions HighlightBot/Migrations/HighlightDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.IsRequired()
.HasColumnType("text");

b.Property<bool>("IsCaseSensitive")
.ValueGeneratedOnAdd()
.HasColumnType("boolean")
.HasDefaultValue(false);

b.Property<string>("Regex")
.IsRequired()
.HasColumnType("text");

b.Property<int>("RegexOptions")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasDefaultValue(1);

b.Property<decimal>("user_serverid")
.HasColumnType("numeric(20,0)");

Expand Down
8 changes: 4 additions & 4 deletions HighlightBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
)
Expand All @@ -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
Expand Down

0 comments on commit 71dcb34

Please sign in to comment.