From 52b485da991004d229fecf2e7401f5c284244122 Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 19 Jul 2024 11:49:34 +1000 Subject: [PATCH] Random quotes --- Commands/RandomQuoteCommand.cs | 41 ++++++++++++++++++++++++++++ Data/Storage/IStorageService.cs | 1 + Data/Storage/SqliteStorageService.cs | 18 ++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 Commands/RandomQuoteCommand.cs diff --git a/Commands/RandomQuoteCommand.cs b/Commands/RandomQuoteCommand.cs new file mode 100644 index 0000000..a1acb76 --- /dev/null +++ b/Commands/RandomQuoteCommand.cs @@ -0,0 +1,41 @@ +using Discord; +using Discord.WebSocket; +using QuoteBot.Data; +using SimpleDiscordNet.Commands; + +namespace QuoteBot.Commands; + +public class RandomQuoteCommand { + + [SlashCommand("random-quote", "Display a random quote from this server.")] + public async Task Execute(SocketSlashCommand cmd, DiscordSocketClient client) { + if (cmd.Channel.GetChannelType() == ChannelType.DM) { + await cmd.RespondWithEmbedAsync("Quote", "You can't do this in your DMs.", ResponseType.Error, ephemeral: false); + return; + } + + Quote? quote = Program.Storage.GetRandomQuote(cmd.GuildId!.Value); + + if (quote == null) { + await cmd.RespondWithEmbedAsync("Quote", + "This server has no quotes yet, quote someone by running /quote, /quote-user to by replying to a message and pinging me.", + ResponseType.Error); + return; + } + + bool hasId = ulong.TryParse(quote.Quotee, out ulong quoteeId); + IUser? quotee = hasId ? await client.GetUserAsync(quoteeId) : null; + string quoteeDisplay = quotee?.Username ?? quote.Quotee; + + IUser? quoter = await client.GetUserAsync(quote.Quoter); + string quoterDisplay = quoter?.Username ?? quote.Quoter.ToString(); + + EmbedBuilder embedBuilder = new(); + embedBuilder.WithTitle(quoteeDisplay); + embedBuilder.WithDescription($"\"{quote.Text}\""); + embedBuilder.WithFooter("Quoted by " + quoterDisplay); + embedBuilder.WithColor(Color.Green); + + await cmd.RespondAsync(embed: embedBuilder.Build()); + } +} \ No newline at end of file diff --git a/Data/Storage/IStorageService.cs b/Data/Storage/IStorageService.cs index 209dfec..c475e31 100644 --- a/Data/Storage/IStorageService.cs +++ b/Data/Storage/IStorageService.cs @@ -8,4 +8,5 @@ public interface IStorageService { void SetQuoteSettings(ulong guild, ulong channel, string form); ulong? GetQuoteChannel(ulong guild); string? GetQuoteForm(ulong guild); + Quote? GetRandomQuote(ulong guild); } diff --git a/Data/Storage/SqliteStorageService.cs b/Data/Storage/SqliteStorageService.cs index 2e9f87a..5cbd8ee 100644 --- a/Data/Storage/SqliteStorageService.cs +++ b/Data/Storage/SqliteStorageService.cs @@ -77,4 +77,22 @@ public void SetQuoteSettings(ulong guild, ulong channel, string form) { } return reader.GetString(0); } + + public Quote? GetRandomQuote(ulong guild) { + using SQLiteCommand cmd = new("SELECT quoter, quotee, message_id, quote, quoted_message_channel, quoted_message_id FROM quotes WHERE guild = @guild ORDER BY RANDOM() LIMIT 1;", _connection); + cmd.Parameters.AddWithValue("guild", guild.ToString()); + using SQLiteDataReader reader = cmd.ExecuteReader(); + if (!reader.Read()) { + return null; + } + return new Quote( + ulong.Parse(reader.GetString(0)), + reader.GetString(1), + guild, + ulong.Parse(reader.GetString(2)), + reader.GetString(3), + ulong.Parse(reader.GetString(4)), + ulong.Parse(reader.GetString(5)) + ); + } } \ No newline at end of file