Remove a database call from !quote by delegating the randomness to the database.
This commit is contained in:
parent
1b9d8732d5
commit
447c6d8042
1 changed files with 5 additions and 10 deletions
|
@ -45,22 +45,17 @@ namespace Geekbot.Bot.Commands.Utils.Quote
|
|||
{
|
||||
try
|
||||
{
|
||||
var getTotalSpan = Transaction.StartChild("TotalQuotes");
|
||||
var totalQuotes = await _database.Quotes.CountAsync(e => e.GuildId.Equals(Context.Guild.Id.AsLong()));
|
||||
getTotalSpan.Finish();
|
||||
var getQuoteFromDbSpan = Transaction.StartChild("GetQuoteFromDB");
|
||||
var quote = _database.Quotes.FromSqlInterpolated($"select * from \"Quotes\" where \"GuildId\" = {Context.Guild.Id} order by random() limit 1");
|
||||
getQuoteFromDbSpan.Finish();
|
||||
|
||||
if (totalQuotes == 0)
|
||||
if (!quote.Any())
|
||||
{
|
||||
await ReplyAsync(Localization.Quote.NoQuotesFound);
|
||||
Transaction.Status = SpanStatus.NotFound;
|
||||
return;
|
||||
}
|
||||
|
||||
var getQuoteFromDbSpan = Transaction.StartChild("GetQuoteFromDB");
|
||||
var random = _randomNumberGenerator.Next(0, totalQuotes - 1);
|
||||
var quote = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())).Skip(random).Take(1);
|
||||
getQuoteFromDbSpan.Finish();
|
||||
|
||||
var buildQuoteEmbedSpan = Transaction.StartChild("BuildQuoteEmbed");
|
||||
var embed = QuoteBuilder(quote.FirstOrDefault());
|
||||
buildQuoteEmbedSpan.Finish();
|
||||
|
|
Loading…
Reference in a new issue