From 37f161663b0979a18ef989224eba600bd936aab5 Mon Sep 17 00:00:00 2001 From: barelyprofessional <150058423+barelyprofessional@users.noreply.github.com> Date: Sun, 15 Sep 2024 13:48:14 +0800 Subject: [PATCH] Added fuzzy string matching as a fallback to the whois command --- KfChatDotNetBot/Commands/WhoisCommand.cs | 11 ++++++++--- KfChatDotNetBot/KfChatDotNetBot.csproj | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/KfChatDotNetBot/Commands/WhoisCommand.cs b/KfChatDotNetBot/Commands/WhoisCommand.cs index d1fbc6b..75e7824 100644 --- a/KfChatDotNetBot/Commands/WhoisCommand.cs +++ b/KfChatDotNetBot/Commands/WhoisCommand.cs @@ -2,6 +2,7 @@ using System.Text.RegularExpressions; using KfChatDotNetBot.Models.DbModels; using KfChatDotNetWsClient.Models.Events; using Microsoft.EntityFrameworkCore; +using Raffinert.FuzzySharp; namespace KfChatDotNetBot.Commands; @@ -19,11 +20,15 @@ public class WhoisCommand : ICommand await using var db = new ApplicationDbContext(); var query = arguments["user"].Value.TrimStart('@').TrimEnd(',').TrimEnd(); var queryUser = await db.Users.FirstOrDefaultAsync(u => u.KfUsername == query, cancellationToken: ctx); - if (queryUser == null) + if (queryUser != null) { - botInstance.SendChatMessage($"Requested user '{query}' does not exist. (Note this is case-sensitive)", true); + botInstance.SendChatMessage($"@{message.Author.Username}, {queryUser.KfUsername}'s ID is {queryUser.KfId}", true); return; } - botInstance.SendChatMessage($"@{message.Author.Username}, {queryUser.KfUsername}'s ID is {queryUser.KfId}", true); + + var users = await db.Users.Select(u => u.KfUsername).Distinct().ToListAsync(ctx); + var result = Process.ExtractOne(query, users); + queryUser = await db.Users.FirstOrDefaultAsync(u => u.KfUsername == result.Value, cancellationToken: ctx); + botInstance.SendChatMessage($"@{message.Author.Username}, my guess is you're looking for {queryUser!.KfUsername} whose ID is {queryUser.KfId}", true); } } \ No newline at end of file diff --git a/KfChatDotNetBot/KfChatDotNetBot.csproj b/KfChatDotNetBot/KfChatDotNetBot.csproj index 8d08a7e..9fb5f35 100644 --- a/KfChatDotNetBot/KfChatDotNetBot.csproj +++ b/KfChatDotNetBot/KfChatDotNetBot.csproj @@ -18,6 +18,7 @@ +