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
{
private readonly CommandService commands;
private readonly CommandService _commands;
public Help(CommandService commands)
{
this.commands = commands;
_commands = commands;
}
[Command("help", RunMode = RunMode.Async)]
@ -23,7 +23,7 @@ namespace Geekbot.net.Commands
sb.AppendLine("**Geekbot Command list**");
sb.AppendLine("");
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);
if (!param.Contains("admin"))

View file

@ -3,6 +3,7 @@ using System.Diagnostics;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Geekbot.net.Lib;
using StackExchange.Redis;
@ -12,11 +13,13 @@ namespace Geekbot.net.Commands
{
private readonly IDatabase _redis;
private readonly IErrorHandler _errorHandler;
private readonly DiscordSocketClient _client;
public Info(IDatabase redis, IErrorHandler errorHandler)
public Info(IDatabase redis, IErrorHandler errorHandler, DiscordSocketClient client)
{
_redis = redis;
_errorHandler = errorHandler;
_client = client;
}
[Command("info", RunMode = RunMode.Async)]
@ -27,16 +30,17 @@ namespace Geekbot.net.Commands
{
var eb = new EmbedBuilder();
eb.WithTitle("Geekbot V3.3");
var botOwner = Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner"))).Result;
eb.WithAuthor(new EmbedAuthorBuilder()
.WithIconUrl(_client.CurrentUser.GetAvatarUrl())
.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));
eb.AddInlineField("Bot Name", Context.Client.CurrentUser.Username)
.AddInlineField("Servers", Context.Client.GetGuildsAsync().Result.Count)
.AddInlineField("Uptime", $"{uptime.Days}D {uptime.Hours}H {uptime.Minutes}M {uptime.Seconds}S")
.AddInlineField("Bot Owner", $"{botOwner.Username}#{botOwner.Discriminator}")
.AddInlineField("Website", "https://geekbot.pizzaandcoffee.rocks/");
eb.AddInlineField("Bot Name", _client.CurrentUser.Username);
eb.AddInlineField("Servers", Context.Client.GetGuildsAsync().Result.Count);
eb.AddInlineField("Uptime", $"{uptime.Days}D {uptime.Hours}H {uptime.Minutes}M {uptime.Seconds}S");
eb.AddInlineField("Bot Owner", $"{botOwner.Username}#{botOwner.Discriminator}");
eb.AddInlineField("Website", "https://geekbot.pizzaandcoffee.rocks/");
await ReplyAsync("", false, eb.Build());
}
@ -45,5 +49,20 @@ namespace Geekbot.net.Commands
_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

@ -11,74 +11,28 @@ using StackExchange.Redis;
namespace Geekbot.net.Commands
{
public class UserInfo : ModuleBase
public class Rank : ModuleBase
{
private readonly IDatabase redis;
private readonly IErrorHandler errorHandler;
private readonly ILogger logger;
private readonly IUserRepository userRepository;
private readonly IDatabase _redis;
private readonly IErrorHandler _errorHandler;
private readonly ILogger _logger;
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;
this.errorHandler = errorHandler;
this.logger = logger;
this.userRepository = userRepository;
}
[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);
}
_redis = redis;
_errorHandler = errorHandler;
_logger = logger;
_userRepository = userRepository;
}
[Command("rank", RunMode = RunMode.Async)]
[Summary("get user top 10")]
public async Task Rank()
public async Task RankCmd()
{
try
{
var messageList = redis.HashGetAll($"{Context.Guild.Id}:Messages");
var messageList = _redis.HashGetAll($"{Context.Guild.Id}:Messages");
var sortedList = messageList.OrderByDescending(e => e.Value).ToList();
var guildMessages = (int) sortedList.First().Value;
sortedList.RemoveAt(0);
@ -91,7 +45,7 @@ namespace Geekbot.net.Commands
if (listLimiter > 10) break;
try
{
var guildUser = userRepository.Get((ulong)user.Name);
var guildUser = _userRepository.Get((ulong)user.Name);
if (guildUser.Username != null)
{
highscoreUsers.Add(new RankUserPolyfill()
@ -112,7 +66,7 @@ namespace Geekbot.net.Commands
}
catch (Exception e)
{
logger.Warning(e, $"Could not retrieve user {user.Name}");
_logger.Warning(e, $"Could not retrieve user {user.Name}");
}
}
@ -140,7 +94,7 @@ namespace Geekbot.net.Commands
}
catch (Exception e)
{
errorHandler.HandleCommandException(e, Context);
_errorHandler.HandleCommandException(e, Context);
}
}
@ -153,7 +107,7 @@ namespace Geekbot.net.Commands
}
catch (Exception e)
{
logger.Warning(e, $"Can't provide emoji number {number}");
_logger.Warning(e, $"Can't provide emoji number {number}");
return ":zero:";
}
}

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.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
@ -11,7 +10,6 @@ using Discord.Commands;
using Discord.WebSocket;
using Geekbot.net.Lib;
using Geekbot.net.Lib.Media;
using Geekbot.net.WebApi;
using Microsoft.Extensions.DependencyInjection;
using Nancy.Hosting.Self;
using Serilog;
@ -83,7 +81,7 @@ namespace Geekbot.net
if (migrateDbConfirm.Key == ConsoleKey.Y)
{
logger.Warning("[Geekbot] Starting Migration");
await MigrateDatabaseToHash();
await DbMigration.MigrateDatabaseToHash(redis, logger);
logger.Warning("[Geekbot] Finished Migration");
}
else
@ -167,7 +165,9 @@ namespace Geekbot.net
if (!args.Contains("--disable-api"))
{
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");
@ -225,47 +225,5 @@ namespace Geekbot.net
}
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 Geekbot.net.Lib;
namespace Geekbot.net.WebApi
{
public class Status : NancyModule
public class StatusController : NancyModule
{
public Status()
public StatusController()
{
Get("/", args =>
{
var responseBody = new ApiStatusDto()
{
GeekbotVersion = "3.4",
ApiVersion = "0.1",
GeekbotVersion = Constants.BotVersion.ToString(),
ApiVersion = Constants.ApiVersion.ToString(),
Status = "Online"
};
return Response.AsJson(responseBody);