1 files changed, 17 insertions, 4 deletions
diff --git a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs
index 215f28a..8501d41 100644
--- a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs
+++ b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs
@@ -1,8 +1,7 @@
using ArcaneLibs;
-using LibMatrix.EventTypes.Spec.State;
using LibMatrix.Homeservers;
-using LibMatrix.Responses;
using LibMatrix.Services;
+using LibMatrix.Utilities.Bot.AppServices;
using LibMatrix.Utilities.Bot.Interfaces;
using LibMatrix.Utilities.Bot.Services;
using Microsoft.Extensions.DependencyInjection;
@@ -19,9 +18,23 @@ public class BotInstaller(IServiceCollection services) {
public BotInstaller AddMatrixBot() {
services.AddSingleton<LibMatrixBotConfiguration>();
- services.AddScoped<AuthenticatedHomeserverGeneric>(x => {
+ services.AddSingleton<AuthenticatedHomeserverGeneric>(x => {
var config = x.GetService<LibMatrixBotConfiguration>() ?? throw new Exception("No configuration found!");
var hsProvider = x.GetService<HomeserverProviderService>() ?? throw new Exception("No homeserver provider found!");
+
+ if (x.GetService<AppServiceConfiguration>() is AppServiceConfiguration appsvcConfig)
+ config.AccessToken = appsvcConfig.AppserviceToken;
+ else if (Environment.GetEnvironmentVariable("LIBMATRIX_ACCESS_TOKEN_PATH") is string path)
+ config.AccessTokenPath = path;
+
+ if(string.IsNullOrWhiteSpace(config.AccessToken) && string.IsNullOrWhiteSpace(config.AccessTokenPath))
+ throw new Exception("Unable to add bot service without an access token or access token path!");
+
+ if(!string.IsNullOrWhiteSpace(config.AccessTokenPath)) {
+ var token = File.ReadAllText(config.AccessTokenPath);
+ config.AccessToken = token;
+ }
+
var hs = hsProvider.GetAuthenticatedWithToken(config.Homeserver, config.AccessToken).Result;
return hs;
@@ -37,7 +50,7 @@ public class BotInstaller(IServiceCollection services) {
}
public BotInstaller DiscoverAllCommands() {
- foreach (var commandClass in new ClassCollector<ICommand>().ResolveFromAllAccessibleAssemblies()) {
+ foreach (var commandClass in ClassCollector<ICommand>.ResolveFromAllAccessibleAssemblies()) {
Console.WriteLine($"Adding command {commandClass.Name}");
services.AddScoped(typeof(ICommand), commandClass);
}
|