diff options
author | Rory& <root@rory.gay> | 2024-03-15 18:10:58 +0100 |
---|---|---|
committer | Rory& <root@rory.gay> | 2024-03-15 18:11:18 +0100 |
commit | 096375344ef87fe53ca009b7a7eaa34c9c9f5407 (patch) | |
tree | 76d666cd6961ca04ae9e91e47c43d91eed27a87a /Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs | |
parent | Fix README (diff) | |
download | LibMatrix-096375344ef87fe53ca009b7a7eaa34c9c9f5407.tar.xz |
Bot changes, move named filters to subclass
Diffstat (limited to '')
-rw-r--r-- | Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs index eb67424..215f28a 100644 --- a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs +++ b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs @@ -1,5 +1,7 @@ using ArcaneLibs; +using LibMatrix.EventTypes.Spec.State; using LibMatrix.Homeservers; +using LibMatrix.Responses; using LibMatrix.Services; using LibMatrix.Utilities.Bot.Interfaces; using LibMatrix.Utilities.Bot.Services; @@ -8,16 +10,13 @@ using Microsoft.Extensions.DependencyInjection; namespace LibMatrix.Utilities.Bot; public static class BotCommandInstaller { - public static IServiceCollection AddBotCommands(this IServiceCollection services) { - foreach (var commandClass in new ClassCollector<ICommand>().ResolveFromAllAccessibleAssemblies()) { - Console.WriteLine($"Adding command {commandClass.Name}"); - services.AddScoped(typeof(ICommand), commandClass); - } - - return services; + public static BotInstaller AddMatrixBot(this IServiceCollection services) { + return new BotInstaller(services).AddMatrixBot(); } +} - public static IServiceCollection AddBot(this IServiceCollection services, bool withCommands = true, bool isAppservice = false) { +public class BotInstaller(IServiceCollection services) { + public BotInstaller AddMatrixBot() { services.AddSingleton<LibMatrixBotConfiguration>(); services.AddScoped<AuthenticatedHomeserverGeneric>(x => { @@ -28,13 +27,42 @@ public static class BotCommandInstaller { return hs; }); - if (withCommands) { - Console.WriteLine("Adding command handler..."); - services.AddBotCommands(); - services.AddHostedService<CommandListenerHostedService>(); - // services.AddSingleton<IHostedService, CommandListenerHostedService>(); + return this; + } + + public BotInstaller AddCommandHandler() { + Console.WriteLine("Adding command handler..."); + services.AddHostedService<CommandListenerHostedService>(); + return this; + } + + public BotInstaller DiscoverAllCommands() { + foreach (var commandClass in new ClassCollector<ICommand>().ResolveFromAllAccessibleAssemblies()) { + Console.WriteLine($"Adding command {commandClass.Name}"); + services.AddScoped(typeof(ICommand), commandClass); } - return services; + return this; + } + public BotInstaller AddCommands(IEnumerable<Type> commandClasses) { + foreach (var commandClass in commandClasses) { + if(!commandClass.IsAssignableTo(typeof(ICommand))) + throw new Exception($"Type {commandClass.Name} is not assignable to ICommand!"); + Console.WriteLine($"Adding command {commandClass.Name}"); + services.AddScoped(typeof(ICommand), commandClass); + } + + return this; + } + + public BotInstaller WithInviteHandler(Func<InviteHandlerHostedService.InviteEventArgs, Task> inviteHandler) { + services.AddSingleton(inviteHandler); + services.AddHostedService<InviteHandlerHostedService>(); + return this; + } + + public BotInstaller WithCommandResultHandler(Func<CommandResult, Task> commandResultHandler) { + services.AddSingleton(commandResultHandler); + return this; } } \ No newline at end of file |