Add DI support for interaction commands

This commit is contained in:
Daan Boerlage 2021-10-30 14:46:23 +02:00
parent 24749d9009
commit a1893c7414
Signed by: daan
GPG key ID: FCE070E1E4956606
3 changed files with 12 additions and 10 deletions

View file

@ -229,7 +229,7 @@ namespace Geekbot.Bot
} }
var highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository); var highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository);
WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, highscoreManager); WebApiStartup.StartWebApi(_servicesProvider, _logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, highscoreManager);
} }
private void RegisterSentry() private void RegisterSentry()

View file

@ -1,25 +1,26 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http.Headers;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Geekbot.Core.GlobalSettings;
using Geekbot.Core.Interactions.ApplicationCommand; using Geekbot.Core.Interactions.ApplicationCommand;
using Geekbot.Core.Interactions.Request; using Geekbot.Core.Interactions.Request;
using Geekbot.Core.Interactions.Response; using Geekbot.Core.Interactions.Response;
using Geekbot.Core.Logger; using Microsoft.Extensions.DependencyInjection;
namespace Geekbot.Core.Interactions namespace Geekbot.Core.Interactions
{ {
public class InteractionCommandManager : IInteractionCommandManager public class InteractionCommandManager : IInteractionCommandManager
{ {
private readonly IServiceProvider _provider;
private readonly Dictionary<string, Type> _commands = new(); private readonly Dictionary<string, Type> _commands = new();
public Dictionary<string, Command> CommandsInfo { get; init; } public Dictionary<string, Command> CommandsInfo { get; init; }
public InteractionCommandManager() public InteractionCommandManager(IServiceProvider provider)
{ {
_provider = provider;
var interactions = Assembly.GetCallingAssembly() var interactions = Assembly.GetCallingAssembly()
.GetTypes() .GetTypes()
.Where(type => type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(InteractionBase))) .Where(type => type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(InteractionBase)))
@ -29,7 +30,7 @@ namespace Geekbot.Core.Interactions
foreach (var interactionType in interactions) foreach (var interactionType in interactions)
{ {
var instance = (InteractionBase)Activator.CreateInstance(interactionType); var instance = (InteractionBase)ActivatorUtilities.CreateInstance(provider, interactionType) ;
var commandInfo = instance.GetCommandInfo(); var commandInfo = instance.GetCommandInfo();
_commands.Add(commandInfo.Name, interactionType); _commands.Add(commandInfo.Name, interactionType);
CommandsInfo.Add(commandInfo.Name, commandInfo); CommandsInfo.Add(commandInfo.Name, commandInfo);
@ -39,7 +40,7 @@ namespace Geekbot.Core.Interactions
public async Task<InteractionResponse> RunCommand(Interaction interaction) public async Task<InteractionResponse> RunCommand(Interaction interaction)
{ {
var type = _commands[interaction.Data.Name]; var type = _commands[interaction.Data.Name];
var command = (InteractionBase)Activator.CreateInstance(type); var command = ActivatorUtilities.CreateInstance(_provider, type) as InteractionBase;
InteractionResponse response; InteractionResponse response;
command.BeforeExecute(); command.BeforeExecute();

View file

@ -1,4 +1,5 @@
using System.Net; using System;
using System.Net;
using System.Reflection; using System.Reflection;
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
@ -22,7 +23,7 @@ namespace Geekbot.Web
// Using the "Microsoft.NET.Sdk.Web" SDK requires a static main function... // Using the "Microsoft.NET.Sdk.Web" SDK requires a static main function...
public static void Main() {} public static void Main() {}
public static void StartWebApi(IGeekbotLogger logger, RunParameters runParameters, CommandService commandService, public static void StartWebApi(IServiceProvider commandProvider, IGeekbotLogger logger, RunParameters runParameters, CommandService commandService,
DatabaseContext databaseContext, DiscordSocketClient client, IGlobalSettings globalSettings, IHighscoreManager highscoreManager) DatabaseContext databaseContext, DiscordSocketClient client, IGlobalSettings globalSettings, IHighscoreManager highscoreManager)
{ {
WebHost.CreateDefaultBuilder() WebHost.CreateDefaultBuilder()
@ -43,7 +44,7 @@ namespace Geekbot.Web
}); });
services.AddSentry(); services.AddSentry();
var interactionCommandManager = new InteractionCommandManager(); var interactionCommandManager = new InteractionCommandManager(commandProvider);
services.AddSingleton(databaseContext); services.AddSingleton(databaseContext);
services.AddSingleton(globalSettings); services.AddSingleton(globalSettings);