Splitting and renaming userinfo.cs, adding commands endpoint to api, minor refactor in other places

This commit is contained in:
Runebaas 2017-10-03 00:22:26 +02:00
parent 92015d8880
commit 09dbb6b14d
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
9 changed files with 364 additions and 230 deletions

View file

@ -7,11 +7,11 @@ namespace Geekbot.net.Commands
{ {
public class Help : ModuleBase public class Help : ModuleBase
{ {
private readonly CommandService commands; private readonly CommandService _commands;
public Help(CommandService commands) public Help(CommandService commands)
{ {
this.commands = commands; _commands = commands;
} }
[Command("help", RunMode = RunMode.Async)] [Command("help", RunMode = RunMode.Async)]
@ -23,7 +23,7 @@ namespace Geekbot.net.Commands
sb.AppendLine("**Geekbot Command list**"); sb.AppendLine("**Geekbot Command list**");
sb.AppendLine(""); sb.AppendLine("");
sb.AppendLine(tp("Name", 15) + tp("Parameters", 19) + "Description"); sb.AppendLine(tp("Name", 15) + tp("Parameters", 19) + "Description");
foreach (var cmd in commands.Commands) foreach (var cmd in _commands.Commands)
{ {
var param = string.Join(", !", cmd.Aliases); var param = string.Join(", !", cmd.Aliases);
if (!param.Contains("admin")) if (!param.Contains("admin"))

View file

@ -3,6 +3,7 @@ using System.Diagnostics;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket;
using Geekbot.net.Lib; using Geekbot.net.Lib;
using StackExchange.Redis; using StackExchange.Redis;
@ -12,11 +13,13 @@ namespace Geekbot.net.Commands
{ {
private readonly IDatabase _redis; private readonly IDatabase _redis;
private readonly IErrorHandler _errorHandler; private readonly IErrorHandler _errorHandler;
private readonly DiscordSocketClient _client;
public Info(IDatabase redis, IErrorHandler errorHandler) public Info(IDatabase redis, IErrorHandler errorHandler, DiscordSocketClient client)
{ {
_redis = redis; _redis = redis;
_errorHandler = errorHandler; _errorHandler = errorHandler;
_client = client;
} }
[Command("info", RunMode = RunMode.Async)] [Command("info", RunMode = RunMode.Async)]
@ -26,17 +29,18 @@ namespace Geekbot.net.Commands
try try
{ {
var eb = new EmbedBuilder(); var eb = new EmbedBuilder();
eb.WithTitle("Geekbot V3.3"); eb.WithAuthor(new EmbedAuthorBuilder()
.WithIconUrl(_client.CurrentUser.GetAvatarUrl())
var botOwner = Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner"))).Result; .WithName($"{Constants.Name} V{Constants.BotVersion}"));
var botOwner = await Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner")));
var uptime = (DateTime.Now.Subtract(Process.GetCurrentProcess().StartTime)); var uptime = (DateTime.Now.Subtract(Process.GetCurrentProcess().StartTime));
eb.AddInlineField("Bot Name", Context.Client.CurrentUser.Username) eb.AddInlineField("Bot Name", _client.CurrentUser.Username);
.AddInlineField("Servers", Context.Client.GetGuildsAsync().Result.Count) eb.AddInlineField("Servers", Context.Client.GetGuildsAsync().Result.Count);
.AddInlineField("Uptime", $"{uptime.Days}D {uptime.Hours}H {uptime.Minutes}M {uptime.Seconds}S") eb.AddInlineField("Uptime", $"{uptime.Days}D {uptime.Hours}H {uptime.Minutes}M {uptime.Seconds}S");
.AddInlineField("Bot Owner", $"{botOwner.Username}#{botOwner.Discriminator}") eb.AddInlineField("Bot Owner", $"{botOwner.Username}#{botOwner.Discriminator}");
.AddInlineField("Website", "https://geekbot.pizzaandcoffee.rocks/"); eb.AddInlineField("Website", "https://geekbot.pizzaandcoffee.rocks/");
await ReplyAsync("", false, eb.Build()); await ReplyAsync("", false, eb.Build());
} }
@ -45,5 +49,20 @@ namespace Geekbot.net.Commands
_errorHandler.HandleCommandException(e, Context); _errorHandler.HandleCommandException(e, Context);
} }
} }
[Command("uptime", RunMode = RunMode.Async)]
[Summary("Get the Bot Uptime")]
public async Task BotUptime()
{
try
{
var uptime = (DateTime.Now.Subtract(Process.GetCurrentProcess().StartTime));
await ReplyAsync($"{uptime.Days}D {uptime.Hours}H {uptime.Minutes}M {uptime.Seconds}S");
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
} }
} }

View file

@ -1,168 +1,122 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib; using Geekbot.net.Lib;
using Serilog; using Serilog;
using StackExchange.Redis; using StackExchange.Redis;
namespace Geekbot.net.Commands namespace Geekbot.net.Commands
{ {
public class UserInfo : ModuleBase public class Rank : ModuleBase
{ {
private readonly IDatabase redis; private readonly IDatabase _redis;
private readonly IErrorHandler errorHandler; private readonly IErrorHandler _errorHandler;
private readonly ILogger logger; private readonly ILogger _logger;
private readonly IUserRepository userRepository; private readonly IUserRepository _userRepository;
public UserInfo(IDatabase redis, IErrorHandler errorHandler, ILogger logger, IUserRepository userRepository) public Rank(IDatabase redis, IErrorHandler errorHandler, ILogger logger, IUserRepository userRepository)
{ {
this.redis = redis; _redis = redis;
this.errorHandler = errorHandler; _errorHandler = errorHandler;
this.logger = logger; _logger = logger;
this.userRepository = userRepository; _userRepository = userRepository;
} }
[Command("stats", RunMode = RunMode.Async)] [Command("rank", RunMode = RunMode.Async)]
[Summary("Get information about this user")] [Summary("get user top 10")]
public async Task User([Summary("@someone")] IUser user = null) public async Task RankCmd()
{ {
try try
{ {
var userInfo = user ?? Context.Message.Author; var messageList = _redis.HashGetAll($"{Context.Guild.Id}:Messages");
var userGuildInfo = (IGuildUser) userInfo; var sortedList = messageList.OrderByDescending(e => e.Value).ToList();
var createdAt = userInfo.CreatedAt; var guildMessages = (int) sortedList.First().Value;
var joinedAt = userGuildInfo.JoinedAt.Value; sortedList.RemoveAt(0);
var age = Math.Floor((DateTime.Now - createdAt).TotalDays);
var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays); var highscoreUsers = new Dictionary<RankUserPolyfill, int>();
var listLimiter = 1;
var messages = (int) redis.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString()); var failedToRetrieveUser = false;
var guildMessages = (int) redis.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); foreach (var user in sortedList)
var level = LevelCalc.GetLevelAtExperience(messages); {
if (listLimiter > 10) break;
var percent = Math.Round((double) (100 * messages) / guildMessages, 2); try
{
var eb = new EmbedBuilder(); var guildUser = _userRepository.Get((ulong)user.Name);
eb.WithAuthor(new EmbedAuthorBuilder() if (guildUser.Username != null)
.WithIconUrl(userInfo.GetAvatarUrl()) {
.WithName(userInfo.Username)); highscoreUsers.Add(new RankUserPolyfill()
eb.WithColor(new Color(221, 255, 119)); {
Username = guildUser.Username,
var karma = redis.HashGet($"{Context.Guild.Id}:Karma", userInfo.Id); Discriminator = guildUser.Discriminator
var correctRolls = redis.HashGet($"{Context.Guild.Id}:Rolls", userInfo.Id.ToString()); }, (int) user.Value);
}
eb.AddInlineField("Discordian Since", $"{createdAt.Day}.{createdAt.Month}.{createdAt.Year} ({age} days)") else
.AddInlineField("Joined Server", $"{joinedAt.Day}.{joinedAt.Month}.{joinedAt.Year} ({joinedDayAgo} days)") {
.AddInlineField("Karma", karma.ToString() ?? "0") highscoreUsers.Add(new RankUserPolyfill()
.AddInlineField("Level", level) {
.AddInlineField("Messages Sent", messages) Id = user.Name
.AddInlineField("Server Total", $"{percent}%"); }, (int) user.Value);
failedToRetrieveUser = true;
if (!correctRolls.IsNullOrEmpty) }
eb.AddInlineField("Guessed Rolls", correctRolls); listLimiter++;
}
await ReplyAsync("", false, eb.Build()); catch (Exception e)
} {
catch (Exception e) _logger.Warning(e, $"Could not retrieve user {user.Name}");
{ }
errorHandler.HandleCommandException(e, Context);
} }
}
var highScore = new StringBuilder();
[Command("rank", RunMode = RunMode.Async)] if (failedToRetrieveUser) highScore.AppendLine(":warning: I couldn't get all userdata, sorry! (bugfix coming soon:tm:)\n");
[Summary("get user top 10")] highScore.AppendLine($":bar_chart: **Highscore for {Context.Guild.Name}**");
public async Task Rank() var highscorePlace = 1;
{ foreach (var user in highscoreUsers)
try {
{ var percent = Math.Round((double) (100 * user.Value) / guildMessages, 2);
var messageList = redis.HashGetAll($"{Context.Guild.Id}:Messages"); if (user.Key.Username != null)
var sortedList = messageList.OrderByDescending(e => e.Value).ToList(); {
var guildMessages = (int) sortedList.First().Value; highScore.AppendLine(
sortedList.RemoveAt(0); $"{NumerToEmoji(highscorePlace)} **{user.Key.Username}#{user.Key.Discriminator}** - {percent}% of total - {user.Value} messages");
}
var highscoreUsers = new Dictionary<RankUserPolyfill, int>(); else
var listLimiter = 1; {
var failedToRetrieveUser = false; highScore.AppendLine(
foreach (var user in sortedList) $"{NumerToEmoji(highscorePlace)} **{user.Key.Id}** - {percent}% of total - {user.Value} messages");
{ }
if (listLimiter > 10) break; highscorePlace++;
try }
{ await ReplyAsync(highScore.ToString());
var guildUser = userRepository.Get((ulong)user.Name); }
if (guildUser.Username != null) catch (Exception e)
{ {
highscoreUsers.Add(new RankUserPolyfill() _errorHandler.HandleCommandException(e, Context);
{ }
Username = guildUser.Username, }
Discriminator = guildUser.Discriminator
}, (int) user.Value); private string NumerToEmoji(int number)
} {
else var emojis = new string[] {":one:", ":two:", ":three:", ":four:", ":five:", ":six:", ":seven:", ":eight:", ":nine:", ":keycap_ten:"};
{ try
highscoreUsers.Add(new RankUserPolyfill() {
{ return emojis[number - 1];
Id = user.Name }
}, (int) user.Value); catch (Exception e)
failedToRetrieveUser = true; {
} _logger.Warning(e, $"Can't provide emoji number {number}");
listLimiter++; return ":zero:";
} }
catch (Exception e) }
{ }
logger.Warning(e, $"Could not retrieve user {user.Name}");
} class RankUserPolyfill
{
} public string Username { get; set; }
public string Discriminator { get; set; }
var highScore = new StringBuilder(); public string Id { get; set; }
if (failedToRetrieveUser) highScore.AppendLine(":warning: I couldn't get all userdata, sorry! (bugfix coming soon:tm:)\n"); }
highScore.AppendLine($":bar_chart: **Highscore for {Context.Guild.Name}**");
var highscorePlace = 1;
foreach (var user in highscoreUsers)
{
var percent = Math.Round((double) (100 * user.Value) / guildMessages, 2);
if (user.Key.Username != null)
{
highScore.AppendLine(
$"{NumerToEmoji(highscorePlace)} **{user.Key.Username}#{user.Key.Discriminator}** - {percent}% of total - {user.Value} messages");
}
else
{
highScore.AppendLine(
$"{NumerToEmoji(highscorePlace)} **{user.Key.Id}** - {percent}% of total - {user.Value} messages");
}
highscorePlace++;
}
await ReplyAsync(highScore.ToString());
}
catch (Exception e)
{
errorHandler.HandleCommandException(e, Context);
}
}
private string NumerToEmoji(int number)
{
var emojis = new string[] {":one:", ":two:", ":three:", ":four:", ":five:", ":six:", ":seven:", ":eight:", ":nine:", ":keycap_ten:"};
try
{
return emojis[number - 1];
}
catch (Exception e)
{
logger.Warning(e, $"Can't provide emoji number {number}");
return ":zero:";
}
}
}
class RankUserPolyfill
{
public string Username { get; set; }
public string Discriminator { get; set; }
public string Id { get; set; }
}
} }

View file

@ -0,0 +1,67 @@
using System;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib;
using StackExchange.Redis;
namespace Geekbot.net.Commands
{
public class Stats : ModuleBase
{
private readonly IDatabase _redis;
private readonly IErrorHandler _errorHandler;
public Stats(IDatabase redis, IErrorHandler errorHandler)
{
_redis = redis;
_errorHandler = errorHandler;
}
[Command("stats", RunMode = RunMode.Async)]
[Summary("Get information about this user")]
public async Task User([Summary("@someone")] IUser user = null)
{
try
{
var userInfo = user ?? Context.Message.Author;
var userGuildInfo = (IGuildUser) userInfo;
var createdAt = userInfo.CreatedAt;
var joinedAt = userGuildInfo.JoinedAt.Value;
var age = Math.Floor((DateTime.Now - createdAt).TotalDays);
var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays);
var messages = (int) _redis.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString());
var guildMessages = (int) _redis.HashGet($"{Context.Guild.Id}:Messages", 0.ToString());
var level = LevelCalc.GetLevelAtExperience(messages);
var percent = Math.Round((double) (100 * messages) / guildMessages, 2);
var eb = new EmbedBuilder();
eb.WithAuthor(new EmbedAuthorBuilder()
.WithIconUrl(userInfo.GetAvatarUrl())
.WithName(userInfo.Username));
eb.WithColor(new Color(221, 255, 119));
var karma = _redis.HashGet($"{Context.Guild.Id}:Karma", userInfo.Id);
var correctRolls = _redis.HashGet($"{Context.Guild.Id}:Rolls", userInfo.Id.ToString());
eb.AddInlineField("Discordian Since", $"{createdAt.Day}.{createdAt.Month}.{createdAt.Year} ({age} days)")
.AddInlineField("Joined Server", $"{joinedAt.Day}.{joinedAt.Month}.{joinedAt.Year} ({joinedDayAgo} days)")
.AddInlineField("Karma", karma.ToString() ?? "0")
.AddInlineField("Level", level)
.AddInlineField("Messages Sent", messages)
.AddInlineField("Server Total", $"{percent}%");
if (!correctRolls.IsNullOrEmpty)
eb.AddInlineField("Guessed Rolls", correctRolls);
await ReplyAsync("", false, eb.Build());
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
}
}

View file

@ -0,0 +1,9 @@
namespace Geekbot.net.Lib
{
public class Constants
{
public const string Name = "Geekbot";
public const double BotVersion = 3.4;
public const double ApiVersion = 1;
}
}

View file

@ -0,0 +1,51 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Serilog;
using StackExchange.Redis;
namespace Geekbot.net.Lib
{
public class DbMigration
{
public static Task MigrateDatabaseToHash(IDatabase redis, ILogger logger)
{
foreach (var key in redis.Multiplexer.GetServer("127.0.0.1", 6379).Keys(6))
{
var keyParts = key.ToString().Split("-");
if (keyParts.Length == 2 || keyParts.Length == 3)
{
logger.Verbose($"Migrating key {key}");
var stuff = new List<string>();
stuff.Add("messages");
stuff.Add("karma");
stuff.Add("welcomeMsg");
stuff.Add("correctRolls");
if(stuff.Contains(keyParts[keyParts.Length - 1]))
{
var val = redis.StringGet(key);
ulong.TryParse(keyParts[0], out ulong guildId);
ulong.TryParse(keyParts[1], out ulong userId);
switch (keyParts[keyParts.Length - 1])
{
case "messages":
redis.HashSet($"{guildId}:Messages", new HashEntry[] { new HashEntry(userId.ToString(), val) });
break;
case "karma":
redis.HashSet($"{guildId}:Karma", new HashEntry[] { new HashEntry(userId.ToString(), val) });
break;
case "correctRolls":
redis.HashSet($"{guildId}:Rolls", new HashEntry[] { new HashEntry(userId.ToString(), val) });
break;
case "welcomeMsg":
redis.HashSet($"{guildId}:Settings", new HashEntry[] { new HashEntry("WelcomeMsg", val) });
break;
}
}
redis.KeyDelete(key);
}
}
return Task.CompletedTask;
}
}
}

View file

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@ -11,7 +10,6 @@ using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
using Geekbot.net.Lib; using Geekbot.net.Lib;
using Geekbot.net.Lib.Media; using Geekbot.net.Lib.Media;
using Geekbot.net.WebApi;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Nancy.Hosting.Self; using Nancy.Hosting.Self;
using Serilog; using Serilog;
@ -83,7 +81,7 @@ namespace Geekbot.net
if (migrateDbConfirm.Key == ConsoleKey.Y) if (migrateDbConfirm.Key == ConsoleKey.Y)
{ {
logger.Warning("[Geekbot] Starting Migration"); logger.Warning("[Geekbot] Starting Migration");
await MigrateDatabaseToHash(); await DbMigration.MigrateDatabaseToHash(redis, logger);
logger.Warning("[Geekbot] Finished Migration"); logger.Warning("[Geekbot] Finished Migration");
} }
else else
@ -167,7 +165,9 @@ namespace Geekbot.net
if (!args.Contains("--disable-api")) if (!args.Contains("--disable-api"))
{ {
logger.Information("[API] Starting Webserver"); logger.Information("[API] Starting Webserver");
new NancyHost(new Uri("http://localhost:4567")).Start(); var webApiUrl = new Uri("http://localhost:12995");
new NancyHost(webApiUrl).Start();
logger.Information($"[API] Webserver now running on {webApiUrl}");
} }
logger.Information("[Geekbot] Done and ready for use\n"); logger.Information("[Geekbot] Done and ready for use\n");
@ -225,47 +225,5 @@ namespace Geekbot.net
} }
return Task.CompletedTask; return Task.CompletedTask;
} }
// db migration script
private Task MigrateDatabaseToHash()
{
foreach (var key in redis.Multiplexer.GetServer("127.0.0.1", 6379).Keys(6))
{
var keyParts = key.ToString().Split("-");
if (keyParts.Length == 2 || keyParts.Length == 3)
{
logger.Verbose($"Migrating key {key}");
var stuff = new List<string>();
stuff.Add("messages");
stuff.Add("karma");
stuff.Add("welcomeMsg");
stuff.Add("correctRolls");
if(stuff.Contains(keyParts[keyParts.Length - 1]))
{
var val = redis.StringGet(key);
ulong.TryParse(keyParts[0], out ulong guildId);
ulong.TryParse(keyParts[1], out ulong userId);
switch (keyParts[keyParts.Length - 1])
{
case "messages":
redis.HashSet($"{guildId}:Messages", new HashEntry[] { new HashEntry(userId.ToString(), val) });
break;
case "karma":
redis.HashSet($"{guildId}:Karma", new HashEntry[] { new HashEntry(userId.ToString(), val) });
break;
case "correctRolls":
redis.HashSet($"{guildId}:Rolls", new HashEntry[] { new HashEntry(userId.ToString(), val) });
break;
case "welcomeMsg":
redis.HashSet($"{guildId}:Settings", new HashEntry[] { new HashEntry("WelcomeMsg", val) });
break;
}
}
redis.KeyDelete(key);
}
}
return Task.CompletedTask;
}
} }
} }

View file

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Nancy;
using Geekbot.net.Lib;
namespace Geekbot.net.WebApi
{
public class HelpController : NancyModule
{
public HelpController()
{
Get("/v1/commands", args =>
{
var commands = getCommands().Result;
var commandList = new List<CommandDto>();
foreach (var cmd in commands.Commands)
{
var cmdParamsObj = new List<CommandParamDto>();
foreach (var cmdParam in cmd.Parameters)
{
var singleParamObj = new CommandParamDto()
{
Summary = cmdParam.Summary,
Default = cmdParam?.DefaultValue?.ToString() ?? null,
Type = cmdParam?.Type?.ToString()
};
cmdParamsObj.Add(singleParamObj);
}
var param = string.Join(", !", cmd.Aliases);
var cmdObj = new CommandDto()
{
Name = cmd.Name,
Summary = cmd.Summary,
IsAdminCommand = (param.Contains("admin")),
Aliases = cmd.Aliases.ToArray(),
Params = cmdParamsObj
};
commandList.Add(cmdObj);
}
return Response.AsJson(commandList);
});
}
private async Task<CommandService> getCommands()
{
var commands = new CommandService();
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
return commands;
}
}
public class CommandDto
{
public string Name { get; set; }
public string Summary { get; set; }
public bool IsAdminCommand { get; set; }
public Array Aliases { get; set; }
public List<CommandParamDto> Params { get; set; }
}
public class CommandParamDto
{
public string Summary { get; set; }
public string Default { get; set; }
public string Type { get; set; }
}
}

View file

@ -1,17 +1,18 @@
using Nancy; using Nancy;
using Geekbot.net.Lib;
namespace Geekbot.net.WebApi namespace Geekbot.net.WebApi
{ {
public class Status : NancyModule public class StatusController : NancyModule
{ {
public Status() public StatusController()
{ {
Get("/", args => Get("/", args =>
{ {
var responseBody = new ApiStatusDto() var responseBody = new ApiStatusDto()
{ {
GeekbotVersion = "3.4", GeekbotVersion = Constants.BotVersion.ToString(),
ApiVersion = "0.1", ApiVersion = Constants.ApiVersion.ToString(),
Status = "Online" Status = "Online"
}; };
return Response.AsJson(responseBody); return Response.AsJson(responseBody);