about summary refs log tree commit diff
path: root/MatrixContentFilter/Handlers/CommandResultHandler.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixContentFilter/Handlers/CommandResultHandler.cs')
-rw-r--r--MatrixContentFilter/Handlers/CommandResultHandler.cs40
1 files changed, 40 insertions, 0 deletions
diff --git a/MatrixContentFilter/Handlers/CommandResultHandler.cs b/MatrixContentFilter/Handlers/CommandResultHandler.cs
new file mode 100644
index 0000000..f05d2bb
--- /dev/null
+++ b/MatrixContentFilter/Handlers/CommandResultHandler.cs
@@ -0,0 +1,40 @@
+using ArcaneLibs;
+using LibMatrix.Helpers;
+using LibMatrix.Utilities.Bot.Interfaces;
+
+namespace MatrixContentFilter.Handlers;
+
+public static class CommandResultHandler {
+    private static string binDir = FileUtils.GetBinDir();
+
+    public static async Task HandleAsync(CommandResult res) {
+        {
+            if (res.Success) return;
+            var room = res.Context.Room;
+            var hs = res.Context.Homeserver;
+            var msb = new MessageBuilder();
+            if (res.Result == CommandResult.CommandResultType.Failure_Exception) {
+                var angryEmojiPath = Path.Combine(binDir, "Resources", "Stickers", "JennyAngryPink.webp");
+                var hash = await FileUtils.GetFileSha384Async(angryEmojiPath);
+                var angryEmoji = await hs.NamedCaches.FileCache.GetOrSetValueAsync(hash, async () => {
+                    await using var fs = File.OpenRead(angryEmojiPath);
+                    return await hs.UploadFile("JennyAngryPink.webp", fs, "image/webp");
+                });
+                msb.WithCustomEmoji(angryEmoji, "JennyAngryPink")
+                    .WithColoredBody("#EE4444", "An error occurred during the execution of this command")
+                    .WithCodeBlock(res.Exception!.ToString(), "csharp");
+            }
+            // else if(res.Result == CommandResult.CommandResultType.) {
+            // msb.AddMessage(new RoomMessageEventContent("m.notice", "An error occurred during the execution of this command"));
+            // }
+            // var msg = res.Result switch {
+            //     CommandResult.CommandResultType.Failure_Exception => MessageFormatter.FormatException("An error occurred during the execution of this command", res.Exception!)
+            //     CommandResult.CommandResultType.Failure_NoPermission => new RoomMessageEventContent("m.notice", "You do not have permission to run this command!"),
+            //     CommandResult.CommandResultType.Failure_InvalidCommand => new RoomMessageEventContent("m.notice", $"Command \"{res.Context.CommandName}\" not found!"),
+            //     _ => throw new ArgumentOutOfRangeException()
+            // };
+
+            await room.SendMessageEventAsync(msb.Build());
+        }
+    }
+}
\ No newline at end of file