Skip to content

Commit

Permalink
Random quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
CoPokBl committed Jul 19, 2024
1 parent 0ea7981 commit 52b485d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Commands/RandomQuoteCommand.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
1 change: 1 addition & 0 deletions Data/Storage/IStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
18 changes: 18 additions & 0 deletions Data/Storage/SqliteStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
}
}

0 comments on commit 52b485d

Please sign in to comment.