2017-10-03 00:22:26 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord.Commands;
|
2018-01-15 20:01:13 +01:00
|
|
|
|
using Discord.WebSocket;
|
2017-10-03 00:22:26 +02:00
|
|
|
|
using Geekbot.net.Lib;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.Converters;
|
|
|
|
|
using Geekbot.net.Lib.ErrorHandling;
|
|
|
|
|
using Geekbot.net.Lib.Logger;
|
|
|
|
|
using Geekbot.net.Lib.UserRepository;
|
2017-10-03 00:22:26 +02:00
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
|
2018-05-03 21:20:49 +02:00
|
|
|
|
namespace Geekbot.net.Commands.User.Ranking
|
2017-10-03 00:22:26 +02:00
|
|
|
|
{
|
|
|
|
|
public class Rank : ModuleBase
|
|
|
|
|
{
|
2017-12-29 01:53:50 +01:00
|
|
|
|
private readonly IEmojiConverter _emojiConverter;
|
2017-10-03 00:22:26 +02:00
|
|
|
|
private readonly IErrorHandler _errorHandler;
|
2018-01-20 01:38:49 +01:00
|
|
|
|
private readonly IGeekbotLogger _logger;
|
2017-12-29 01:53:50 +01:00
|
|
|
|
private readonly IDatabase _redis;
|
2017-10-03 00:22:26 +02:00
|
|
|
|
private readonly IUserRepository _userRepository;
|
2018-01-15 20:01:13 +01:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2018-01-20 01:38:49 +01:00
|
|
|
|
public Rank(IDatabase redis, IErrorHandler errorHandler, IGeekbotLogger logger, IUserRepository userRepository,
|
2018-01-15 20:01:13 +01:00
|
|
|
|
IEmojiConverter emojiConverter, DiscordSocketClient client)
|
2017-10-03 00:22:26 +02:00
|
|
|
|
{
|
|
|
|
|
_redis = redis;
|
|
|
|
|
_errorHandler = errorHandler;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_userRepository = userRepository;
|
2017-11-06 23:55:28 +01:00
|
|
|
|
_emojiConverter = emojiConverter;
|
2018-01-15 20:01:13 +01:00
|
|
|
|
_client = client;
|
2017-10-03 00:22:26 +02:00
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-10-03 00:22:26 +02:00
|
|
|
|
[Command("rank", RunMode = RunMode.Async)]
|
2017-10-12 16:34:10 +02:00
|
|
|
|
[Remarks(CommandCategories.Statistics)]
|
2017-12-28 22:38:49 +01:00
|
|
|
|
[Summary("get user top 10 in messages or karma")]
|
2018-05-03 21:20:49 +02:00
|
|
|
|
public async Task RankCmd([Summary("type")] string typeUnformated = "messages", [Summary("amount")] int amount = 10)
|
2017-10-03 00:22:26 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-12-28 22:38:49 +01:00
|
|
|
|
var type = typeUnformated.ToCharArray().First().ToString().ToUpper() + typeUnformated.Substring(1);
|
|
|
|
|
|
2017-12-29 01:03:38 +01:00
|
|
|
|
if (!type.Equals("Messages") && !type.Equals("Karma") && !type.Equals("Rolls"))
|
2017-12-28 22:38:49 +01:00
|
|
|
|
{
|
2017-12-29 01:03:38 +01:00
|
|
|
|
await ReplyAsync("Valid types are '`messages`' '`karma`', '`rolls`'");
|
2017-12-28 22:38:49 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-12-28 22:38:49 +01:00
|
|
|
|
var replyBuilder = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
if (amount > 20)
|
|
|
|
|
{
|
|
|
|
|
replyBuilder.AppendLine(":warning: Limiting to 20");
|
|
|
|
|
amount = 20;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-12-28 22:38:49 +01:00
|
|
|
|
var messageList = _redis.HashGetAll($"{Context.Guild.Id}:{type}");
|
2018-02-15 00:07:11 +01:00
|
|
|
|
if (messageList.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync($"No {type.ToLowerInvariant()} found on this server");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-10-03 00:22:26 +02:00
|
|
|
|
var sortedList = messageList.OrderByDescending(e => e.Value).ToList();
|
|
|
|
|
var guildMessages = (int) sortedList.First().Value;
|
2018-02-15 00:07:11 +01:00
|
|
|
|
var theBot = sortedList.FirstOrDefault(e => e.Name.ToString().Equals(_client.CurrentUser.Id.ToString()));
|
|
|
|
|
if (!string.IsNullOrEmpty(theBot.Name))
|
|
|
|
|
{
|
|
|
|
|
sortedList.Remove(theBot);
|
|
|
|
|
}
|
2017-12-28 22:58:29 +01:00
|
|
|
|
if (type == "Messages") sortedList.RemoveAt(0);
|
2017-10-03 00:22:26 +02:00
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
var highscoreUsers = new Dictionary<RankUserPolyfillDto, int>();
|
2017-10-03 00:22:26 +02:00
|
|
|
|
var listLimiter = 1;
|
|
|
|
|
var failedToRetrieveUser = false;
|
|
|
|
|
foreach (var user in sortedList)
|
|
|
|
|
{
|
2017-12-28 22:38:49 +01:00
|
|
|
|
if (listLimiter > amount) break;
|
2017-10-03 00:22:26 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-12-29 01:53:50 +01:00
|
|
|
|
var guildUser = _userRepository.Get((ulong) user.Name);
|
2017-10-03 00:22:26 +02:00
|
|
|
|
if (guildUser.Username != null)
|
|
|
|
|
{
|
2018-05-03 00:56:06 +02:00
|
|
|
|
highscoreUsers.Add(new RankUserPolyfillDto
|
2017-10-03 00:22:26 +02:00
|
|
|
|
{
|
|
|
|
|
Username = guildUser.Username,
|
|
|
|
|
Discriminator = guildUser.Discriminator
|
|
|
|
|
}, (int) user.Value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-05-03 00:56:06 +02:00
|
|
|
|
highscoreUsers.Add(new RankUserPolyfillDto
|
2017-10-03 00:22:26 +02:00
|
|
|
|
{
|
|
|
|
|
Id = user.Name
|
|
|
|
|
}, (int) user.Value);
|
|
|
|
|
failedToRetrieveUser = true;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-10-03 00:22:26 +02:00
|
|
|
|
listLimiter++;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-01-20 01:38:49 +01:00
|
|
|
|
_logger.Warning("Geekbot", $"Could not retrieve user {user.Name}", e);
|
2017-10-03 00:22:26 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-12-28 22:38:49 +01:00
|
|
|
|
if (failedToRetrieveUser) replyBuilder.AppendLine(":warning: Couldn't get all userdata\n");
|
|
|
|
|
replyBuilder.AppendLine($":bar_chart: **{type} Highscore for {Context.Guild.Name}**");
|
2017-10-03 00:22:26 +02:00
|
|
|
|
var highscorePlace = 1;
|
|
|
|
|
foreach (var user in highscoreUsers)
|
|
|
|
|
{
|
2017-12-28 22:38:49 +01:00
|
|
|
|
replyBuilder.Append(highscorePlace < 11
|
2018-04-30 23:44:19 +02:00
|
|
|
|
? $"{_emojiConverter.NumberToEmoji(highscorePlace)} "
|
2017-12-28 22:38:49 +01:00
|
|
|
|
: $"`{highscorePlace}.` ");
|
|
|
|
|
|
|
|
|
|
replyBuilder.Append(user.Key.Username != null
|
|
|
|
|
? $"**{user.Key.Username}#{user.Key.Discriminator}**"
|
|
|
|
|
: $"**{user.Key.Id}**");
|
|
|
|
|
|
|
|
|
|
switch (type)
|
2017-10-03 00:22:26 +02:00
|
|
|
|
{
|
2017-12-28 22:38:49 +01:00
|
|
|
|
case "Messages":
|
|
|
|
|
var percent = Math.Round((double) (100 * user.Value) / guildMessages, 2);
|
|
|
|
|
replyBuilder.Append($" - {percent}% of total - {user.Value} messages");
|
|
|
|
|
break;
|
|
|
|
|
case "Karma":
|
|
|
|
|
replyBuilder.Append($" - {user.Value} Karma");
|
|
|
|
|
break;
|
2017-12-29 01:03:38 +01:00
|
|
|
|
case "Rolls":
|
|
|
|
|
replyBuilder.Append($" - {user.Value} Guessed");
|
2017-12-29 01:53:50 +01:00
|
|
|
|
break;
|
2017-10-03 00:22:26 +02:00
|
|
|
|
}
|
2017-12-28 22:38:49 +01:00
|
|
|
|
|
|
|
|
|
replyBuilder.Append("\n");
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-10-03 00:22:26 +02:00
|
|
|
|
highscorePlace++;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-12-28 22:38:49 +01:00
|
|
|
|
await ReplyAsync(replyBuilder.ToString());
|
2017-10-03 00:22:26 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-12 21:49:04 +02:00
|
|
|
|
}
|