From a54b42ee6725ceadf422b7c64a18403e4bbe08f2 Mon Sep 17 00:00:00 2001 From: Elijah R Date: Wed, 3 Apr 2024 16:15:37 -0400 Subject: [PATCH] add more options to /quote --- EmperorPalpatine/ChatlogQuery.cs | 1 + EmperorPalpatine/Database.cs | 2 ++ EmperorPalpatine/DiscordCommands.cs | 21 +++++++++++++++------ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/EmperorPalpatine/ChatlogQuery.cs b/EmperorPalpatine/ChatlogQuery.cs index 6420360..623616a 100644 --- a/EmperorPalpatine/ChatlogQuery.cs +++ b/EmperorPalpatine/ChatlogQuery.cs @@ -9,4 +9,5 @@ public class ChatlogQuery public int? Count { get; set; } public bool Random { get; set; } public string? Regex { get; set; } + public string[]? CustomWhere { get; set; } } \ No newline at end of file diff --git a/EmperorPalpatine/Database.cs b/EmperorPalpatine/Database.cs index 5153dd1..909e77c 100644 --- a/EmperorPalpatine/Database.cs +++ b/EmperorPalpatine/Database.cs @@ -103,6 +103,8 @@ public class Database where.Add("message REGEXP @regex"); cmd.Parameters.AddWithValue("@regex", q.Regex); } + if (q.CustomWhere != null) + where.AddRange(q.CustomWhere); if (where.Count > 0) { cmd.CommandText += " WHERE " + string.Join(" AND ", where); diff --git a/EmperorPalpatine/DiscordCommands.cs b/EmperorPalpatine/DiscordCommands.cs index 9e7c612..00370d6 100644 --- a/EmperorPalpatine/DiscordCommands.cs +++ b/EmperorPalpatine/DiscordCommands.cs @@ -198,18 +198,27 @@ public class DiscordCommands : ApplicationCommandModule } [SlashCommand("quote", "Quote a user")] - public async Task Quote(InteractionContext ctx, [Option("username", "User to quote")] string username) + public async Task Quote(InteractionContext ctx, [Option("username", "User to quote")] string username = "", [Option("guest", "Should the random quote be from a guest only? Only works if username is not specified")] bool guest = false) { - await ctx.DeferAsync(); - var chat = await Program.Database.GetChatlogsAsync(new ChatlogQuery + if (username != "" && guest) + { + await ctx.CreateResponseAsync("You cannot specify both a username and guest."); + return; + } + await ctx.DeferAsync(); + var q = new ChatlogQuery { - Username = username, Count = 1, Random = true - }); + }; + if (username != "") + q.Username = username; + if (guest) + q.CustomWhere = ["username REGEXP '^guest'"]; + var chat = await Program.Database.GetChatlogsAsync(q); if (chat.Length == 0) { - await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"@No messages found for {username}")); + await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"No messages found for {username}")); return; } await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"> {chat[0].Message}\n\\- {chat[0].Username}, {chat[0].Timestamp} {chat[0].VM}"));