Port rank for slash commands

This commit is contained in:
Daan Boerlage 2021-10-31 23:21:15 +01:00
parent 772557978b
commit e20faa43e1
Signed by: daan
GPG key ID: FCE070E1E4956606
5 changed files with 225 additions and 86 deletions

View file

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.Core;
@ -9,10 +6,8 @@ using Geekbot.Core.CommandPreconditions;
using Geekbot.Core.Converters;
using Geekbot.Core.Database;
using Geekbot.Core.ErrorHandling;
using Geekbot.Core.Extensions;
using Geekbot.Core.GuildSettingsManager;
using Geekbot.Core.Highscores;
using Localization = Geekbot.Core.Localization;
namespace Geekbot.Bot.Commands.User.Ranking
{
@ -40,85 +35,9 @@ namespace Geekbot.Bot.Commands.User.Ranking
{
try
{
HighscoreTypes type;
try
{
type = Enum.Parse<HighscoreTypes>(typeUnformated, true);
if (!Enum.IsDefined(typeof(HighscoreTypes), type)) throw new Exception();
}
catch
{
await ReplyAsync(Localization.Rank.InvalidType);
return;
}
var replyBuilder = new StringBuilder();
if (amount > 20)
{
await ReplyAsync(Localization.Rank.LimitingTo20Warning);
amount = 20;
}
var guildId = Context.Guild.Id;
Dictionary<HighscoreUserDto, int> highscoreUsers;
try
{
highscoreUsers = _highscoreManager.GetHighscoresWithUserData(type, guildId, amount, season);
}
catch (HighscoreListEmptyException)
{
await ReplyAsync(string.Format(Localization.Rank.NoTypeFoundForServer, type));
return;
}
var guildMessages = 0;
if (type == HighscoreTypes.messages)
{
guildMessages = _database.Messages
.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong()))
.Select(e => e.MessageCount)
.Sum();
}
var failedToRetrieveUser = highscoreUsers.Any(e => string.IsNullOrEmpty(e.Key.Username));
if (failedToRetrieveUser) replyBuilder.AppendLine(Localization.Rank.FailedToResolveAllUsernames).AppendLine();
if (type == HighscoreTypes.seasons)
{
if (string.IsNullOrEmpty(season))
{
season = SeasonsUtils.GetCurrentSeason();
}
replyBuilder.AppendLine(string.Format(Localization.Rank.HighscoresFor, $"{type.ToString().CapitalizeFirst()} ({season})", Context.Guild.Name));
}
else
{
replyBuilder.AppendLine(string.Format(Localization.Rank.HighscoresFor, type.ToString().CapitalizeFirst(), Context.Guild.Name));
}
var highscorePlace = 1;
foreach (var (user, value) in highscoreUsers)
{
replyBuilder.Append(highscorePlace < 11
? $"{_emojiConverter.NumberToEmoji(highscorePlace)} "
: $"`{highscorePlace}.` ");
replyBuilder.Append(user.Username != null
? $"**{user.Username}#{user.Discriminator}**"
: $"**{user.Id}**");
replyBuilder.Append(type switch
{
HighscoreTypes.messages => $" - {value} {HighscoreTypes.messages} - {Math.Round((double) (100 * value) / guildMessages, 2)}%\n",
HighscoreTypes.seasons => $" - {value} {HighscoreTypes.messages}\n",
_ => $" - {value} {type}\n"
});
highscorePlace++;
}
await ReplyAsync(replyBuilder.ToString());
var res = new Geekbot.Commands.Rank(_database, _emojiConverter, _highscoreManager)
.Run(typeUnformated, amount, season, Context.Guild.Id, Context.Guild.Name);
await ReplyAsync(res);
}
catch (Exception e)
{