Read the bot commands on startup and provide them to the /v1/commands endpoint

This commit is contained in:
Daan Boerlage 2021-11-09 00:53:09 +01:00
parent 4f4e16d674
commit 9beef55979
Signed by: daan
GPG key ID: FCE070E1E4956606
5 changed files with 46 additions and 29 deletions

View file

@ -2,6 +2,7 @@
using CommandLine; using CommandLine;
using Geekbot.Bot; using Geekbot.Bot;
using Geekbot.Core; using Geekbot.Core;
using Geekbot.Core.BotCommandLookup;
using Geekbot.Core.Converters; using Geekbot.Core.Converters;
using Geekbot.Core.Database; using Geekbot.Core.Database;
using Geekbot.Core.DiceParser; using Geekbot.Core.DiceParser;
@ -79,7 +80,8 @@ if (!runParameters.DisableGateway)
// //
if (!runParameters.DisableApi) if (!runParameters.DisableApi)
{ {
WebApiStartup.StartWebApi(serviceProvider.BuildServiceProvider(), logger, runParameters, databaseInitializer.Initialize(), globalSettings); var botCommands = new CommandLookup(typeof(BotStartup).Assembly).GetCommands();
WebApiStartup.StartWebApi(serviceProvider.BuildServiceProvider(), logger, runParameters, databaseInitializer.Initialize(), globalSettings, botCommands);
} }
ServiceCollection RegisterServices() ServiceCollection RegisterServices()

View file

@ -1,4 +1,4 @@
using Discord.Commands; using Geekbot.Core.BotCommandLookup;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -8,33 +8,29 @@ namespace Geekbot.Web.Controllers.Commands;
[EnableCors("AllowSpecificOrigin")] [EnableCors("AllowSpecificOrigin")]
public class CommandController : ControllerBase public class CommandController : ControllerBase
{ {
private readonly CommandService _commands; private readonly List<CommandInfo> _commandInfos;
public CommandController(CommandService commands) public CommandController(List<CommandInfo> commandInfos)
{ {
_commands = commands; _commandInfos = commandInfos;
} }
[HttpGet("/v1/commands")] [HttpGet("/v1/commands")]
public IActionResult GetCommands() public IActionResult GetCommands()
{ {
var commandList = (from cmd in _commands.Commands var commandList = _commandInfos.Select(cmd => new ResponseCommand()
let cmdParamsObj = cmd.Parameters.Select(cmdParam => new ResponseCommandParam {
{ Name = cmd.Name,
Summary = cmdParam.Summary, Summary = cmd.Summary,
Default = cmdParam.DefaultValue?.ToString(), IsAdminCommand = cmd.Name.StartsWith("admin") || cmd.Name.StartsWith("owner"),
Type = cmdParam.Type?.ToString() Aliases = new List<string>() { cmd.Name },
}) Params = cmd.Parameters.Select(dict => new ResponseCommandParam()
.ToList()
let param = string.Join(", !", cmd.Aliases)
select new ResponseCommand
{ {
Name = cmd.Name, Summary = dict.Value.Summary,
Summary = cmd.Summary, Default = dict.Value.DefaultValue,
IsAdminCommand = param.Contains("admin") || param.Contains("owner"), Type = dict.Value.Type
Aliases = cmd.Aliases.ToList(), }).ToList()
Params = cmdParamsObj });
}).ToList(); return Ok(commandList);
return Ok(commandList.FindAll(e => !e.Aliases[0].StartsWith("owner")));
} }
} }

View file

@ -1,10 +1,21 @@
namespace Geekbot.Web.Controllers.Commands; using System.Text.Json.Serialization;
namespace Geekbot.Web.Controllers.Commands;
public record ResponseCommand public record ResponseCommand
{ {
[JsonPropertyName("name")]
public string Name { get; set; } public string Name { get; set; }
[JsonPropertyName("summary")]
public string Summary { get; set; } public string Summary { get; set; }
[JsonPropertyName("isAdminCommand")]
public bool IsAdminCommand { get; set; } public bool IsAdminCommand { get; set; }
[JsonPropertyName("aliases")]
public List<string> Aliases { get; set; } public List<string> Aliases { get; set; }
[JsonPropertyName("params")]
public List<ResponseCommandParam> Params { get; set; } public List<ResponseCommandParam> Params { get; set; }
} }

View file

@ -1,8 +1,15 @@
namespace Geekbot.Web.Controllers.Commands; using System.Text.Json.Serialization;
namespace Geekbot.Web.Controllers.Commands;
public record ResponseCommandParam public record ResponseCommandParam
{ {
[JsonPropertyName("summary")]
public string Summary { get; set; } public string Summary { get; set; }
[JsonPropertyName("default")]
public string Default { get; set; } public string Default { get; set; }
[JsonPropertyName("type")]
public string Type { get; set; } public string Type { get; set; }
} }

View file

@ -1,12 +1,13 @@
using System.Net; using System.Net;
using System.Reflection; using System.Reflection;
using Discord.Commands;
using Geekbot.Core; using Geekbot.Core;
using Geekbot.Core.BotCommandLookup;
using Geekbot.Core.Database; using Geekbot.Core.Database;
using Geekbot.Core.GlobalSettings; using Geekbot.Core.GlobalSettings;
using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.GuildSettingsManager;
using Geekbot.Core.Highscores; using Geekbot.Core.Highscores;
using Geekbot.Core.Logger; using Geekbot.Core.Logger;
using Geekbot.Core.UserRepository;
using Geekbot.Interactions; using Geekbot.Interactions;
using Geekbot.Web.Logging; using Geekbot.Web.Logging;
@ -19,8 +20,7 @@ public static class WebApiStartup
{ {
} }
public static void StartWebApi(IServiceProvider commandProvider, IGeekbotLogger logger, RunParameters runParameters, CommandService commandService, public static void StartWebApi(IServiceProvider commandProvider, IGeekbotLogger logger, RunParameters runParameters, DatabaseContext databaseContext, IGlobalSettings globalSettings, List<CommandInfo> commandInfos)
DatabaseContext databaseContext, IGlobalSettings globalSettings, IHighscoreManager highscoreManager, IGuildSettingsManager guildSettingsManager)
{ {
var builder = WebApplication.CreateBuilder(new WebApplicationOptions() { ApplicationName = typeof(WebApiStartup).GetTypeInfo().Assembly.FullName }); var builder = WebApplication.CreateBuilder(new WebApplicationOptions() { ApplicationName = typeof(WebApiStartup).GetTypeInfo().Assembly.FullName });
builder.WebHost.UseKestrel(options => options.Listen(IPAddress.Any, int.Parse(runParameters.ApiPort))); builder.WebHost.UseKestrel(options => options.Listen(IPAddress.Any, int.Parse(runParameters.ApiPort)));
@ -28,14 +28,15 @@ public static class WebApiStartup
builder.Services.AddControllers(); builder.Services.AddControllers();
builder.Services.AddCors(options => options.AddPolicy("AllowSpecificOrigin", cors => cors.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod())); builder.Services.AddCors(options => options.AddPolicy("AllowSpecificOrigin", cors => cors.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()));
var interactionCommandManager = new InteractionCommandManager(commandProvider, guildSettingsManager); var interactionCommandManager = new InteractionCommandManager(commandProvider, commandProvider.GetService<IGuildSettingsManager>());
var highscoreManager = new HighscoreManager(commandProvider.GetService<DatabaseContext>(), commandProvider.GetService<IUserRepository>());
builder.Services.AddSingleton(databaseContext); builder.Services.AddSingleton(databaseContext);
builder.Services.AddSingleton(globalSettings); builder.Services.AddSingleton(globalSettings);
builder.Services.AddSingleton(highscoreManager); builder.Services.AddSingleton(highscoreManager);
builder.Services.AddSingleton(logger); builder.Services.AddSingleton(logger);
builder.Services.AddSingleton<IInteractionCommandManager>(interactionCommandManager); builder.Services.AddSingleton<IInteractionCommandManager>(interactionCommandManager);
if (!runParameters.DisableGateway) builder.Services.AddSingleton(commandService); builder.Services.AddSingleton(commandInfos);
builder.Logging.ClearProviders(); builder.Logging.ClearProviders();
builder.Logging.SetMinimumLevel(LogLevel.Debug); builder.Logging.SetMinimumLevel(LogLevel.Debug);