'!quote stats' bug fixes

This commit is contained in:
runebaas 2020-05-12 00:04:20 +02:00
parent 5cf1248bf0
commit 656393cc7b
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6

View file

@ -198,6 +198,7 @@ namespace Geekbot.net.Commands.Utils.Quote
{ {
try try
{ {
// setup
var transContext = await _translationHandler.GetGuildContext(Context); var transContext = await _translationHandler.GetGuildContext(Context);
var eb = new EmbedBuilder(); var eb = new EmbedBuilder();
eb.Author = new EmbedAuthorBuilder() eb.Author = new EmbedAuthorBuilder()
@ -206,25 +207,33 @@ namespace Geekbot.net.Commands.Utils.Quote
Name = $"{Context.Guild.Name} - {transContext.GetString("QuoteStats")}" Name = $"{Context.Guild.Name} - {transContext.GetString("QuoteStats")}"
}; };
// gather data
var totalQuotes = _database.Quotes.Count(row => row.GuildId == Context.Guild.Id.AsLong()); var totalQuotes = _database.Quotes.Count(row => row.GuildId == Context.Guild.Id.AsLong());
if (totalQuotes == 0) if (totalQuotes == 0)
{ {
// no quotes, no stats, end of the road
await ReplyAsync(transContext.GetString("NoQuotesFound")); await ReplyAsync(transContext.GetString("NoQuotesFound"));
return; return;
} }
eb.AddInlineField(transContext.GetString("TotalQuotes"), totalQuotes);
var mostQuotedPerson = _database.Quotes var mostQuotedPerson = _database.Quotes
.Where(row => row.GuildId == Context.Guild.Id.AsLong()) .Where(row => row.GuildId == Context.Guild.Id.AsLong())
.GroupBy(row => row.UserId) .GroupBy(row => row.UserId)
.Max(row => row.Key); .Select(row => new { userId = row.Key, amount = row.Count()})
var user = Context.Client.GetUserAsync(mostQuotedPerson.AsUlong()).Result ?? new UserPolyfillDto {Username = "Unknown User"}; .OrderBy(row => row.amount)
eb.AddInlineField(transContext.GetString("MostQuotesPerson"), user); .Last();
var mostQuotedPersonUser = Context.Client.GetUserAsync(mostQuotedPerson.userId.AsUlong()).Result ?? new UserPolyfillDto {Username = "Unknown User"};
var quotesByYear = _database.Quotes var quotesByYear = _database.Quotes
.Where(row => row.GuildId == Context.Guild.Id.AsLong()) .Where(row => row.GuildId == Context.Guild.Id.AsLong())
.GroupBy(row => row.Time.Year) .GroupBy(row => row.Time.Year)
.Select(row => new { year = row.Key, amount = row.Count()}); .Select(row => new { year = row.Key, amount = row.Count()})
.OrderBy(row => row.year);
// add data to the embed
eb.AddField(transContext.GetString("MostQuotesPerson"), $"{mostQuotedPersonUser.Username} ({mostQuotedPerson.amount})");
eb.AddInlineField(transContext.GetString("TotalQuotes"), totalQuotes);
foreach (var year in quotesByYear) foreach (var year in quotesByYear)
{ {
eb.AddInlineField(year.year.ToString(), year.amount); eb.AddInlineField(year.year.ToString(), year.amount);