about summary refs log tree commit diff
path: root/Utilities/LibMatrix.Utilities.Bot
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-03-12 19:50:52 +0100
committerRory& <root@rory.gay>2025-03-12 19:51:23 +0100
commitcacabe2b1a15bb7492e23d477ec653513e84d260 (patch)
tree5b17d5f455ae6347de79eb73406bbf769b33d707 /Utilities/LibMatrix.Utilities.Bot
parentClean up stray console log from merge (diff)
downloadLibMatrix-cacabe2b1a15bb7492e23d477ec653513e84d260.tar.xz
Add more bot stuff
Diffstat (limited to 'Utilities/LibMatrix.Utilities.Bot')
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs26
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/Services/InviteListenerHostedService.cs4
2 files changed, 21 insertions, 9 deletions
diff --git a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs

index 8501d41..0d755a1 100644 --- a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs +++ b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs
@@ -21,20 +21,20 @@ public class BotInstaller(IServiceCollection services) { 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)) + + 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)) { + + 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; @@ -57,9 +57,10 @@ public class BotInstaller(IServiceCollection services) { return this; } + public BotInstaller AddCommands(IEnumerable<Type> commandClasses) { foreach (var commandClass in commandClasses) { - if(!commandClass.IsAssignableTo(typeof(ICommand))) + 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); @@ -67,13 +68,20 @@ public class BotInstaller(IServiceCollection services) { return this; } - + public BotInstaller WithInviteHandler(Func<InviteHandlerHostedService.InviteEventArgs, Task> inviteHandler) { services.AddSingleton(inviteHandler); services.AddHostedService<InviteHandlerHostedService>(); return this; } - + + public BotInstaller WithInviteHandler<T>() where T : class, InviteHandlerHostedService.IInviteHandler { + services.AddSingleton<T>(); + services.AddSingleton<Func<InviteHandlerHostedService.InviteEventArgs, Task>>(sp => sp.GetRequiredService<T>().HandleInviteAsync); + services.AddHostedService<InviteHandlerHostedService>(); + return this; + } + public BotInstaller WithCommandResultHandler(Func<CommandResult, Task> commandResultHandler) { services.AddSingleton(commandResultHandler); return this; diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/InviteListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/InviteListenerHostedService.cs
index 88a6a03..cac9ca4 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Services/InviteListenerHostedService.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Services/InviteListenerHostedService.cs
@@ -77,4 +77,8 @@ public class InviteHandlerHostedService : IHostedService { public StateEventResponse MemberEvent { get; set; } public AuthenticatedHomeserverGeneric Homeserver { get; set; } } + + public interface IInviteHandler { + public Task HandleInviteAsync(InviteEventArgs args); + } } \ No newline at end of file