Read the bot commands on startup and provide them to the /v1/commands endpoint
This commit is contained in:
parent
4f4e16d674
commit
9beef55979
5 changed files with 46 additions and 29 deletions
|
@ -2,6 +2,7 @@
|
|||
using CommandLine;
|
||||
using Geekbot.Bot;
|
||||
using Geekbot.Core;
|
||||
using Geekbot.Core.BotCommandLookup;
|
||||
using Geekbot.Core.Converters;
|
||||
using Geekbot.Core.Database;
|
||||
using Geekbot.Core.DiceParser;
|
||||
|
@ -79,7 +80,8 @@ if (!runParameters.DisableGateway)
|
|||
//
|
||||
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()
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using Discord.Commands;
|
||||
using Geekbot.Core.BotCommandLookup;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
|
@ -8,33 +8,29 @@ namespace Geekbot.Web.Controllers.Commands;
|
|||
[EnableCors("AllowSpecificOrigin")]
|
||||
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")]
|
||||
public IActionResult GetCommands()
|
||||
{
|
||||
var commandList = (from cmd in _commands.Commands
|
||||
let cmdParamsObj = cmd.Parameters.Select(cmdParam => new ResponseCommandParam
|
||||
{
|
||||
Summary = cmdParam.Summary,
|
||||
Default = cmdParam.DefaultValue?.ToString(),
|
||||
Type = cmdParam.Type?.ToString()
|
||||
})
|
||||
.ToList()
|
||||
let param = string.Join(", !", cmd.Aliases)
|
||||
select new ResponseCommand
|
||||
var commandList = _commandInfos.Select(cmd => new ResponseCommand()
|
||||
{
|
||||
Name = cmd.Name,
|
||||
Summary = cmd.Summary,
|
||||
IsAdminCommand = cmd.Name.StartsWith("admin") || cmd.Name.StartsWith("owner"),
|
||||
Aliases = new List<string>() { cmd.Name },
|
||||
Params = cmd.Parameters.Select(dict => new ResponseCommandParam()
|
||||
{
|
||||
Name = cmd.Name,
|
||||
Summary = cmd.Summary,
|
||||
IsAdminCommand = param.Contains("admin") || param.Contains("owner"),
|
||||
Aliases = cmd.Aliases.ToList(),
|
||||
Params = cmdParamsObj
|
||||
}).ToList();
|
||||
return Ok(commandList.FindAll(e => !e.Aliases[0].StartsWith("owner")));
|
||||
Summary = dict.Value.Summary,
|
||||
Default = dict.Value.DefaultValue,
|
||||
Type = dict.Value.Type
|
||||
}).ToList()
|
||||
});
|
||||
return Ok(commandList);
|
||||
}
|
||||
}
|
|
@ -1,10 +1,21 @@
|
|||
namespace Geekbot.Web.Controllers.Commands;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Geekbot.Web.Controllers.Commands;
|
||||
|
||||
public record ResponseCommand
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("summary")]
|
||||
public string Summary { get; set; }
|
||||
|
||||
[JsonPropertyName("isAdminCommand")]
|
||||
public bool IsAdminCommand { get; set; }
|
||||
|
||||
[JsonPropertyName("aliases")]
|
||||
public List<string> Aliases { get; set; }
|
||||
|
||||
[JsonPropertyName("params")]
|
||||
public List<ResponseCommandParam> Params { get; set; }
|
||||
}
|
|
@ -1,8 +1,15 @@
|
|||
namespace Geekbot.Web.Controllers.Commands;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Geekbot.Web.Controllers.Commands;
|
||||
|
||||
public record ResponseCommandParam
|
||||
{
|
||||
[JsonPropertyName("summary")]
|
||||
public string Summary { get; set; }
|
||||
|
||||
[JsonPropertyName("default")]
|
||||
public string Default { get; set; }
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public string Type { get; set; }
|
||||
}
|
|
@ -1,12 +1,13 @@
|
|||
using System.Net;
|
||||
using System.Reflection;
|
||||
using Discord.Commands;
|
||||
using Geekbot.Core;
|
||||
using Geekbot.Core.BotCommandLookup;
|
||||
using Geekbot.Core.Database;
|
||||
using Geekbot.Core.GlobalSettings;
|
||||
using Geekbot.Core.GuildSettingsManager;
|
||||
using Geekbot.Core.Highscores;
|
||||
using Geekbot.Core.Logger;
|
||||
using Geekbot.Core.UserRepository;
|
||||
using Geekbot.Interactions;
|
||||
using Geekbot.Web.Logging;
|
||||
|
||||
|
@ -19,8 +20,7 @@ public static class WebApiStartup
|
|||
{
|
||||
}
|
||||
|
||||
public static void StartWebApi(IServiceProvider commandProvider, IGeekbotLogger logger, RunParameters runParameters, CommandService commandService,
|
||||
DatabaseContext databaseContext, IGlobalSettings globalSettings, IHighscoreManager highscoreManager, IGuildSettingsManager guildSettingsManager)
|
||||
public static void StartWebApi(IServiceProvider commandProvider, IGeekbotLogger logger, RunParameters runParameters, DatabaseContext databaseContext, IGlobalSettings globalSettings, List<CommandInfo> commandInfos)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(new WebApplicationOptions() { ApplicationName = typeof(WebApiStartup).GetTypeInfo().Assembly.FullName });
|
||||
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.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(globalSettings);
|
||||
builder.Services.AddSingleton(highscoreManager);
|
||||
builder.Services.AddSingleton(logger);
|
||||
builder.Services.AddSingleton<IInteractionCommandManager>(interactionCommandManager);
|
||||
if (!runParameters.DisableGateway) builder.Services.AddSingleton(commandService);
|
||||
builder.Services.AddSingleton(commandInfos);
|
||||
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.SetMinimumLevel(LogLevel.Debug);
|
||||
|
|
Loading…
Reference in a new issue