Merge pull request #4 from pizzaandcoffee/rank_users

Highscores and minor cleanup
This commit is contained in:
Daan Boerlage 2017-07-31 00:25:07 +02:00 committed by GitHub
commit 43bad89573
2 changed files with 44 additions and 25 deletions

View file

@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib;
@ -59,6 +62,38 @@ namespace Geekbot.net.Modules
await ReplyAsync("", false, eb.Build());
}
[Alias("highscore")]
[Command("rank", RunMode = RunMode.Async), Summary("get user top 10")]
public async Task Rank()
{
await ReplyAsync("this will take a moment...");
var guildKey = Context.Guild.Id.ToString();
var guildMessages = (int)redis.Client.StringGet(guildKey + "-messages");
var allGuildUsers = await Context.Guild.GetUsersAsync();
var unsortedDict = new Dictionary<string, int>();
foreach(var user in allGuildUsers)
{
var key = Context.Guild.Id + "-" + user.Id;
var messages = (int)redis.Client.StringGet(key + "-messages");
if(messages > 0) {
unsortedDict.Add($"{user.Username}#{user.Discriminator}", messages);
}
}
var sortedDict = unsortedDict.OrderByDescending(x => x.Value);
var reply = new StringBuilder();
reply.AppendLine($"Total Messages on {Context.Guild.Name}: {guildMessages}");
var count = 1;
foreach(KeyValuePair<string, int> entry in sortedDict)
{
if(count < 11){
var percent = Math.Round((double)(100 * entry.Value) / guildMessages, 2);
reply.AppendLine($"#{count} - **{entry.Key}** - {percent}% of total - {entry.Value} messages");
count++;
} else {
break;
}
}
await ReplyAsync(reply.ToString());
}
}
}

View file

@ -103,44 +103,28 @@ namespace Geekbot.net
return true;
}
public async Task Reconnect(Exception exception)
{
Console.WriteLine("=========================================");
Console.WriteLine("Geekbot Disconnected from the Discord Gateway...");
Console.WriteLine(exception.Message);
Console.WriteLine("Attempting Reconnect...");
Console.WriteLine("=========================================");
await client.StopAsync();
System.Threading.Thread.Sleep(10000);
await Login();
}
public async Task FinishStartup()
{
}
public async Task HandleCommand(SocketMessage messageParam)
{
var message = messageParam as SocketUserMessage;
if (message == null) return;
if (message.Author.IsBot) return;
int argPos = 0;
if (message.ToString().ToLower().StartsWith("ping"))
var lowCaseMsg = message.ToString().ToLower();
if (lowCaseMsg.StartsWith("ping"))
{
await message.Channel.SendMessageAsync("pong");
return;
}
if (message.ToString().ToLower().StartsWith("hui"))
if (lowCaseMsg.StartsWith("hui"))
{
await message.Channel.SendMessageAsync("hui!!!");
return;
}
if (message.ToString().ToLower().Contains("teamspeak") || message.ToString().ToLower().Contains("skype"))
{
await message.Channel.SendMessageAsync("How dare you to use such a filthy word in here http://bit.ly/2poL2IZ");
return;
}
// if (message.ToString().ToLower().Contains("teamspeak") || message.ToString().ToLower().Contains("skype"))
// {
// await message.Channel.SendMessageAsync("How dare you to use such a filthy word in here http://bit.ly/2poL2IZ");
// return;
// }
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return;
var context = new CommandContext(client, message);
Task.Run(async () => await commands.ExecuteAsync(context, argPos, map));