about summary refs log tree commit diff
path: root/Utilities
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-09-16 08:43:42 +0200
committerRory& <root@rory.gay>2024-09-16 08:43:42 +0200
commitb93c5e7e284c73c97192bf4ff7002c4b396ed2e9 (patch)
tree254ea85c256b7f8c582a6755e7918ba48585ae87 /Utilities
parentmeow (diff)
downloadLibMatrix-dev/moderationclient-changes.tar.xz
Diffstat (limited to 'Utilities')
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs4
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs48
2 files changed, 37 insertions, 15 deletions
diff --git a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs
index 215f28a..5a0b7ad 100644
--- a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs
+++ b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs
@@ -19,7 +19,7 @@ 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!");
             var hs = hsProvider.GetAuthenticatedWithToken(config.Homeserver, config.AccessToken).Result;
@@ -37,7 +37,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);
         }
diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
index 601e598..9a7585e 100644
--- a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
+++ b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
@@ -59,22 +59,44 @@ public class CommandListenerHostedService : IHostedService {
             FilterId = filter
         };
 
-        syncHelper.TimelineEventHandlers.Add(async @event => {
-            try {
-                var room = _hs.GetRoom(@event.RoomId);
-                // _logger.LogInformation(eventResponse.ToJson(indent: false));
-                if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message })
-                    if (message is { MessageType: "m.text" }) {
-                        var usedPrefix = await GetUsedPrefix(@event);
-                        if (usedPrefix is null) return;
-                        var res = await InvokeCommand(@event, usedPrefix);
-                        await (_commandResultHandler?.Invoke(res) ?? HandleResult(res));
+        syncHelper.SyncReceivedHandlers.Add(async sync => {
+            _logger.LogInformation("Sync received!");
+            foreach (var roomResp in sync.Rooms?.Join ?? []) {
+                if (roomResp.Value.Timeline?.Events is null or { Count: > 5 }) continue;
+                foreach (var @event in roomResp.Value.Timeline.Events) {
+                    @event.RoomId = roomResp.Key;
+                    try {
+                        // var room = _hs.GetRoom(@event.RoomId);
+                        // _logger.LogInformation(eventResponse.ToJson(indent: false));
+                        if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message })
+                            if (message is { MessageType: "m.text" }) {
+                                var usedPrefix = await GetUsedPrefix(@event);
+                                if (usedPrefix is null) return;
+                                var res = await InvokeCommand(@event, usedPrefix);
+                                await (_commandResultHandler?.Invoke(res) ?? HandleResult(res));
+                            }
                     }
-            }
-            catch (Exception e) {
-                _logger.LogError(e, "Error in command listener!");
+                    catch (Exception e) {
+                        _logger.LogError(e, "Error in command listener!");
+                        Console.WriteLine(@event.ToJson(ignoreNull: false, indent: true));
+                        var fakeResult = new CommandResult() {
+                            Result = CommandResult.CommandResultType.Failure_Exception,
+                            Exception = e,
+                            Success = false,
+                            Context = new() {
+                                Homeserver = _hs,
+                                CommandName = "[CommandListener.SyncHandler]",
+                                Room = _hs.GetRoom(roomResp.Key),
+                                Args = [],
+                                MessageEvent = @event
+                            }
+                        };
+                        await (_commandResultHandler?.Invoke(fakeResult) ?? HandleResult(fakeResult));
+                    }
+                }
             }
         });
+
         await syncHelper.RunSyncLoopAsync(cancellationToken: cancellationToken);
     }