Splitting and renaming userinfo.cs, adding commands endpoint to api, minor refactor in other places
This commit is contained in:
parent
92015d8880
commit
09dbb6b14d
9 changed files with 364 additions and 230 deletions
|
@ -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"))
|
||||
|
|
|
@ -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)]
|
||||
|
@ -26,17 +29,18 @@ namespace Geekbot.net.Commands
|
|||
try
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,168 +1,122 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using Serilog;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Geekbot.net.Commands
|
||||
{
|
||||
public class UserInfo : ModuleBase
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
[Command("rank", RunMode = RunMode.Async)]
|
||||
[Summary("get user top 10")]
|
||||
public async Task Rank()
|
||||
{
|
||||
try
|
||||
{
|
||||
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);
|
||||
|
||||
var highscoreUsers = new Dictionary<RankUserPolyfill, int>();
|
||||
var listLimiter = 1;
|
||||
var failedToRetrieveUser = false;
|
||||
foreach (var user in sortedList)
|
||||
{
|
||||
if (listLimiter > 10) break;
|
||||
try
|
||||
{
|
||||
var guildUser = userRepository.Get((ulong)user.Name);
|
||||
if (guildUser.Username != null)
|
||||
{
|
||||
highscoreUsers.Add(new RankUserPolyfill()
|
||||
{
|
||||
Username = guildUser.Username,
|
||||
Discriminator = guildUser.Discriminator
|
||||
}, (int) user.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
highscoreUsers.Add(new RankUserPolyfill()
|
||||
{
|
||||
Id = user.Name
|
||||
}, (int) user.Value);
|
||||
failedToRetrieveUser = true;
|
||||
}
|
||||
listLimiter++;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.Warning(e, $"Could not retrieve user {user.Name}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var highScore = new StringBuilder();
|
||||
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; }
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using Serilog;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Geekbot.net.Commands
|
||||
{
|
||||
public class Rank : ModuleBase
|
||||
{
|
||||
private readonly IDatabase _redis;
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IUserRepository _userRepository;
|
||||
|
||||
public Rank(IDatabase redis, IErrorHandler errorHandler, ILogger logger, IUserRepository userRepository)
|
||||
{
|
||||
_redis = redis;
|
||||
_errorHandler = errorHandler;
|
||||
_logger = logger;
|
||||
_userRepository = userRepository;
|
||||
}
|
||||
|
||||
[Command("rank", RunMode = RunMode.Async)]
|
||||
[Summary("get user top 10")]
|
||||
public async Task RankCmd()
|
||||
{
|
||||
try
|
||||
{
|
||||
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);
|
||||
|
||||
var highscoreUsers = new Dictionary<RankUserPolyfill, int>();
|
||||
var listLimiter = 1;
|
||||
var failedToRetrieveUser = false;
|
||||
foreach (var user in sortedList)
|
||||
{
|
||||
if (listLimiter > 10) break;
|
||||
try
|
||||
{
|
||||
var guildUser = _userRepository.Get((ulong)user.Name);
|
||||
if (guildUser.Username != null)
|
||||
{
|
||||
highscoreUsers.Add(new RankUserPolyfill()
|
||||
{
|
||||
Username = guildUser.Username,
|
||||
Discriminator = guildUser.Discriminator
|
||||
}, (int) user.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
highscoreUsers.Add(new RankUserPolyfill()
|
||||
{
|
||||
Id = user.Name
|
||||
}, (int) user.Value);
|
||||
failedToRetrieveUser = true;
|
||||
}
|
||||
listLimiter++;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Warning(e, $"Could not retrieve user {user.Name}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var highScore = new StringBuilder();
|
||||
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; }
|
||||
}
|
||||
}
|
67
Geekbot.net/Commands/Stats.cs
Normal file
67
Geekbot.net/Commands/Stats.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
9
Geekbot.net/Lib/Constants.cs
Normal file
9
Geekbot.net/Lib/Constants.cs
Normal 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;
|
||||
}
|
||||
}
|
51
Geekbot.net/Lib/DbMigration.cs
Normal file
51
Geekbot.net/Lib/DbMigration.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
75
Geekbot.net/WebApi/HelpController.cs
Normal file
75
Geekbot.net/WebApi/HelpController.cs
Normal 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; }
|
||||
}
|
||||
}
|
|
@ -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);
|
Loading…
Reference in a new issue