about summary refs log tree commit diff
path: root/Jenny/Handlers/CommandResultHandler.cs
blob: 767944a7751b7c219d8cf574814827ed982bb14d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using ArcaneLibs;
using LibMatrix.Helpers;
using LibMatrix.Utilities.Bot.Interfaces;

namespace Jenny.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());
        }
    }
}