From 58bd4d17d0ecaa09ca0d64219c601cae021a6163 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 23 Sep 2020 16:28:50 +0200 Subject: [PATCH] Optimize the !quote database query --- src/Bot/Commands/Utils/Quote/Quote.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index 6f4cd1c..db6b1ac 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -33,23 +33,23 @@ namespace Geekbot.Bot.Commands.Utils.Quote } [Command] - [Summary("Return a random quoute from the database")] + [Summary("Return a random quote from the database")] public async Task GetRandomQuote() { try { - var s = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())).ToList(); + var totalQuotes = await _database.Quotes.CountAsync(e => e.GuildId.Equals(Context.Guild.Id.AsLong())); - if (!s.Any()) + if (totalQuotes == 0) { await ReplyAsync(Localization.Quote.NoQuotesFound); return; } - var random = _randomNumberGenerator.Next(0, s.Count - 1); - var quote = s[random]; - - var embed = QuoteBuilder(quote); + var random = _randomNumberGenerator.Next(0, totalQuotes - 1); + var quote = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())).Skip(random).Take(1); + + var embed = QuoteBuilder(quote.FirstOrDefault()); await ReplyAsync("", false, embed.Build()); } catch (Exception e)