about summary refs log tree commit diff
path: root/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-05 06:28:52 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-05 06:28:52 +0200
commitcf455ed8de20bbee011289223e7d8d5775dfd69e (patch)
treecbdfdbc207af64a105b4d21941a6f0e71ca65e9d /Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs
parentAdd start of Media Moderator PoC bot (diff)
downloadLibMatrix-cf455ed8de20bbee011289223e7d8d5775dfd69e.tar.xz
Media moderator PoC works, abstract command handling to library
Diffstat (limited to 'Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs')
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs44
1 files changed, 44 insertions, 0 deletions
diff --git a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs
new file mode 100644
index 0000000..42cdb6c
--- /dev/null
+++ b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs
@@ -0,0 +1,44 @@
+using ArcaneLibs;
+using ArcaneLibs.Extensions;
+using LibMatrix.Helpers;
+using LibMatrix.Homeservers;
+using LibMatrix.Services;
+using LibMatrix.StateEventTypes.Spec;
+using LibMatrix.Utilities.Bot.Services;
+using MediaModeratorPoC.Bot.Interfaces;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+
+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 IServiceCollection AddBot(this IServiceCollection services, bool withCommands = true) {
+        services.AddSingleton<LibMatrixBotConfiguration>();
+
+        services.AddScoped<AuthenticatedHomeserverGeneric>(x => {
+            var config = x.GetService<LibMatrixBotConfiguration>();
+            var hsProvider = x.GetService<HomeserverProviderService>();
+            var hs = hsProvider.GetAuthenticatedWithToken(config.Homeserver, config.AccessToken).Result;
+
+            return hs;
+        });
+
+        if (withCommands) {
+            Console.WriteLine("Adding command handler...");
+            services.AddBotCommands();
+            services.AddHostedService<CommandListenerHostedService>();
+            // services.AddSingleton<IHostedService, CommandListenerHostedService>();
+        }
+        return services;
+    }
+}