about summary refs log tree commit diff
path: root/Utilities/LibMatrix.Utilities.Bot/Services
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-15 09:50:45 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-15 09:50:45 +0200
commit6bd02248ccfbcb46960a6f39eaad23888d190eb5 (patch)
tree110578f31b6f9f70a7a1edab32fb3a34d6ad4f1a /Utilities/LibMatrix.Utilities.Bot/Services
parentMedia moderator PoC works, abstract command handling to library (diff)
downloadLibMatrix-6bd02248ccfbcb46960a6f39eaad23888d190eb5.tar.xz
Some refactoring
Diffstat (limited to '')
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs60
1 files changed, 37 insertions, 23 deletions
diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
index d5e7dd6..df702f4 100644
--- a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
+++ b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
@@ -1,3 +1,4 @@
+using LibMatrix.Helpers;
 using LibMatrix.Homeservers;
 using LibMatrix.StateEventTypes.Spec;
 using MediaModeratorPoC.Bot.Interfaces;
@@ -37,34 +38,47 @@ public class CommandListenerHostedService : IHostedService {
     private async Task? Run(CancellationToken cancellationToken) {
         _logger.LogInformation("Starting command listener!");
         _hs.SyncHelper.TimelineEventHandlers.Add(async @event => {
-            var room = await _hs.GetRoom(@event.RoomId);
-            // _logger.LogInformation(eventResponse.ToJson(indent: false));
-            if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventData message }) {
-                if (message is { MessageType: "m.text" }) {
-                    var messageContentWithoutReply = message.Body.Split('\n', StringSplitOptions.RemoveEmptyEntries).SkipWhile(x=>x.StartsWith(">")).Aggregate((x, y) => $"{x}\n{y}");
-                    if (messageContentWithoutReply.StartsWith(_config.Prefix)) {
-                        var command = _commands.FirstOrDefault(x => x.Name == messageContentWithoutReply.Split(' ')[0][_config.Prefix.Length..]);
-                        if (command == null) {
-                            await room.SendMessageEventAsync("m.room.message",
-                                new RoomMessageEventData(messageType: "m.notice", body: "Command not found!"));
-                            return;
-                        }
+            try {
+                var room = await _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 messageContentWithoutReply =
+                            message.Body.Split('\n', StringSplitOptions.RemoveEmptyEntries).SkipWhile(x => x.StartsWith(">")).Aggregate((x, y) => $"{x}\n{y}");
+                        if (messageContentWithoutReply.StartsWith(_config.Prefix)) {
+                            var command = _commands.FirstOrDefault(x => x.Name == messageContentWithoutReply.Split(' ')[0][_config.Prefix.Length..]);
+                            if (command == null) {
+                                await room.SendMessageEventAsync("m.room.message",
+                                    new RoomMessageEventContent(messageType: "m.notice", body: "Command not found!"));
+                                return;
+                            }
 
-                        var ctx = new CommandContext {
-                            Room = room,
-                            MessageEvent = @event,
-                            Homeserver = _hs
-                        };
-                        if (await command.CanInvoke(ctx)) {
-                            await command.Invoke(ctx);
-                        }
-                        else {
-                            await room.SendMessageEventAsync("m.room.message",
-                                new RoomMessageEventData(messageType: "m.notice", body: "You do not have permission to run this command!"));
+                            var ctx = new CommandContext {
+                                Room = room,
+                                MessageEvent = @event,
+                                Homeserver = _hs
+                            };
+
+                            if (await command.CanInvoke(ctx)) {
+                                try {
+                                    await command.Invoke(ctx);
+                                }
+                                catch (Exception e) {
+                                    await room.SendMessageEventAsync("m.room.message",
+                                        MessageFormatter.FormatException("An error occurred during the execution of this command", e));
+                                }
+                            }
+                            else {
+                                await room.SendMessageEventAsync("m.room.message",
+                                    new RoomMessageEventContent(messageType: "m.notice", body: "You do not have permission to run this command!"));
+                            }
                         }
                     }
                 }
             }
+            catch (Exception e) {
+                _logger.LogError(e, "Error in command listener!");
+            }
         });
         await _hs.SyncHelper.RunSyncLoop(cancellationToken: cancellationToken);
     }