Adding battletag saving, settings in user repo, cors, help now reffers to website
This commit is contained in:
parent
ef6fd15116
commit
0c9a2a5619
6 changed files with 149 additions and 22 deletions
77
Geekbot.net/Commands/BattleTag.cs
Normal file
77
Geekbot.net/Commands/BattleTag.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,43 +1,56 @@
|
|||
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);
|
||||
}
|
||||
sb.AppendLine("```");
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// Table Padding, short function name because of many usages
|
||||
private string tp(string text, int shouldHave)
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
<PackageReference Include="Nancy" Version="2.0.0-clinteastwood" />
|
||||
<PackageReference Include="Nancy.Hosting.Self" Version="2.0.0-clinteastwood" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||
<PackageReference Include="Overwatch.Net" Version="3.0.0-alpha" />
|
||||
<PackageReference Include="PokeApi.NET" Version="1.1.0" />
|
||||
<PackageReference Include="Serilog" Version="2.6.0-dev-00894" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1-dev-00757" />
|
||||
|
|
|
@ -71,6 +71,20 @@ namespace Geekbot.net.Lib
|
|||
}
|
||||
return dto;
|
||||
}
|
||||
|
||||
public string getUserSetting(ulong userId, string setting)
|
||||
{
|
||||
return _redis.HashGet($"Users:{userId}", setting);
|
||||
}
|
||||
|
||||
public bool saveUserSetting(ulong userId, string setting, string value)
|
||||
{
|
||||
_redis.HashSet($"Users:{userId}", new HashEntry[]
|
||||
{
|
||||
new HashEntry(setting, value)
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class UserRepositoryUser
|
||||
|
@ -86,5 +100,7 @@ namespace Geekbot.net.Lib
|
|||
{
|
||||
Task<bool> Update(SocketUser user);
|
||||
UserRepositoryUser Get(ulong userId);
|
||||
string getUserSetting(ulong userId, string setting);
|
||||
bool saveUserSetting(ulong userId, string setting, string value);
|
||||
}
|
||||
}
|
22
Geekbot.net/WebApi/WebConfig.cs
Normal file
22
Geekbot.net/WebApi/WebConfig.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using Nancy;
|
||||
using Nancy.Bootstrapper;
|
||||
using Nancy.TinyIoc;
|
||||
|
||||
namespace Geekbot.net.WebApi
|
||||
{
|
||||
public class WebConfig : DefaultNancyBootstrapper
|
||||
{
|
||||
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
|
||||
{
|
||||
|
||||
//CORS Enable
|
||||
pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) =>
|
||||
{
|
||||
ctx.Response.WithHeader("Access-Control-Allow-Origin", "*")
|
||||
.WithHeader("Access-Control-Allow-Methods", "POST,GET")
|
||||
.WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type");
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue