Adding battletag saving, settings in user repo, cors, help now reffers to website

This commit is contained in:
Runebaas 2017-10-04 01:35:25 +02:00
parent ef6fd15116
commit 0c9a2a5619
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
6 changed files with 149 additions and 22 deletions

View file

@ -0,0 +1,77 @@
using System;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.net.Lib;
using StackExchange.Redis;
namespace Geekbot.net.Commands
{
[Group("battletag")]
public class BattleTag : ModuleBase
{
private readonly IErrorHandler _errorHandler;
private readonly IDatabase _redis;
private readonly IUserRepository _userRepository;
public BattleTag(IErrorHandler errorHandler, IDatabase redis, IUserRepository userRepository)
{
_errorHandler = errorHandler;
_redis = redis;
_userRepository = userRepository;
}
[Command(RunMode = RunMode.Async)]
[Summary("Get your battletag")]
public async Task BattleTagCmd()
{
try
{
var tag = _userRepository.getUserSetting(Context.User.Id, "BattleTag");
if (!string.IsNullOrEmpty(tag))
{
await ReplyAsync($"Your BattleTag is {tag}");
}
else
{
await ReplyAsync("You haven't set your BattleTag, set it with `!battletag user#1234`");
}
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
[Command(RunMode = RunMode.Async)]
[Summary("Save your battletag")]
public async Task BattleTagCmd([Summary("Battletag")] string tag)
{
try
{
if (isValidTag(tag))
{
_userRepository.saveUserSetting(Context.User.Id, "BattleTag", tag);
await ReplyAsync("Saved!");
}
else
{
await ReplyAsync("That doesn't seem to be a valid battletag");
}
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
private bool isValidTag(string tag)
{
var splited = tag.Split("#");
if (splited.Length != 2) return false;
if (!int.TryParse(splited[1], out int discriminator)) return false;
if (splited[1].Length == 4 || splited[1].Length == 5) return true;
return false;
}
}
}

View file

@ -1,42 +1,55 @@
using System.Linq;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.net.Lib;
namespace Geekbot.net.Commands
{
public class Help : ModuleBase
{
private readonly CommandService _commands;
private readonly IErrorHandler _errorHandler;
public Help(CommandService commands)
public Help(CommandService commands, IErrorHandler errorHandler)
{
_commands = commands;
_errorHandler = errorHandler;
}
[Command("help", RunMode = RunMode.Async)]
[Summary("List all Commands")]
public async Task GetHelp()
{
var sb = new StringBuilder();
sb.AppendLine("```");
sb.AppendLine("**Geekbot Command list**");
sb.AppendLine("");
sb.AppendLine(tp("Name", 15) + tp("Parameters", 19) + "Description");
foreach (var cmd in _commands.Commands)
try
{
var param = string.Join(", !", cmd.Aliases);
if (!param.Contains("admin"))
if (cmd.Parameters.Any())
sb.AppendLine(tp(param, 15) +
tp(string.Join(" ", cmd.Parameters.Select(e => e.Summary)), 19) +
cmd.Summary);
else
sb.AppendLine(tp(param, 34) + cmd.Summary);
var sb = new StringBuilder();
// sb.AppendLine("```");
// sb.AppendLine("**Geekbot Command list**");
// sb.AppendLine("");
// sb.AppendLine(tp("Name", 15) + tp("Parameters", 19) + "Description");
// foreach (var cmd in _commands.Commands)
// {
// var param = string.Join(", !", cmd.Aliases);
// if (!param.Contains("admin"))
// if (cmd.Parameters.Any())
// sb.AppendLine(tp(param, 15) +
// tp(string.Join(" ", cmd.Parameters.Select(e => e.Summary)), 19) +
// cmd.Summary);
// else
// sb.AppendLine(tp(param, 34) + cmd.Summary);
// }
// sb.AppendLine("```");
sb.AppendLine("For a list of all commands, please visit the following page");
sb.AppendLine("https://geekbot.pizzaandcoffee.rocks/commands");
var dm = await Context.User.GetOrCreateDMChannelAsync();
await dm.SendMessageAsync(sb.ToString());
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
sb.AppendLine("```");
var dm = await Context.User.GetOrCreateDMChannelAsync();
await dm.SendMessageAsync(sb.ToString());
}
// Table Padding, short function name because of many usages

View file

@ -12,12 +12,10 @@ namespace Geekbot.net.Commands
public class Pokedex : ModuleBase
{
private readonly IErrorHandler _errorHandler;
private readonly ILogger _logger;
public Pokedex(IErrorHandler errorHandler, ILogger logger)
public Pokedex(IErrorHandler errorHandler)
{
_errorHandler = errorHandler;
_logger = logger;
}
[Command("pokedex", RunMode = RunMode.Async)]