From bfca811955e5a7d15a9e60cbee89a6109b30ba19 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sun, 30 Jul 2017 23:35:28 +0200 Subject: [PATCH 1/2] Highscores and cleanup --- Geekbot.net/Modules/UserInfo.cs | 33 ++++++++++++++++++++++++++++++++- Geekbot.net/Program.cs | 32 ++++++++------------------------ 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/Geekbot.net/Modules/UserInfo.cs b/Geekbot.net/Modules/UserInfo.cs index ce1644f..c134893 100644 --- a/Geekbot.net/Modules/UserInfo.cs +++ b/Geekbot.net/Modules/UserInfo.cs @@ -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,34 @@ 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 allGuildUsers = await Context.Guild.GetUsersAsync(); + var unsortedDict = new Dictionary(); + 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, messages); + } + } + var sortedDict = unsortedDict.OrderByDescending(x => x.Value); + var reply = new StringBuilder(); + var count = 1; + foreach(KeyValuePair entry in sortedDict) + { + if(count < 11){ + reply.Append($"#{count} - **{entry.Key}** - {entry.Value}\r\n"); + count++; + } else { + break; + } + } + await ReplyAsync(reply.ToString()); + } } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index cc33b50..cf0487c 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -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)); From 9f9850920e5633491942a6af48d6847eb1e54cd5 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Mon, 31 Jul 2017 00:20:37 +0200 Subject: [PATCH 2/2] Bug Fix and more info in rank command --- Geekbot.net/Modules/UserInfo.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Modules/UserInfo.cs b/Geekbot.net/Modules/UserInfo.cs index c134893..2dbb827 100644 --- a/Geekbot.net/Modules/UserInfo.cs +++ b/Geekbot.net/Modules/UserInfo.cs @@ -67,6 +67,8 @@ namespace Geekbot.net.Modules 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(); foreach(var user in allGuildUsers) @@ -74,16 +76,18 @@ namespace Geekbot.net.Modules var key = Context.Guild.Id + "-" + user.Id; var messages = (int)redis.Client.StringGet(key + "-messages"); if(messages > 0) { - unsortedDict.Add(user.Username, messages); + 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 entry in sortedDict) { if(count < 11){ - reply.Append($"#{count} - **{entry.Key}** - {entry.Value}\r\n"); + var percent = Math.Round((double)(100 * entry.Value) / guildMessages, 2); + reply.AppendLine($"#{count} - **{entry.Key}** - {percent}% of total - {entry.Value} messages"); count++; } else { break;