about summary refs log tree commit diff
path: root/Utilities/LibMatrix.Utilities.Bot/Commands/AliassesCommand.cs
blob: aaa61edc4f954ffc221e13f5fd9b749d42e83a66 (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
41
42
using System.Text;
using LibMatrix.Helpers;
using LibMatrix.LegacyEvents.EventTypes.Spec;
using LibMatrix.Utilities.Bot.Interfaces;
using Microsoft.Extensions.DependencyInjection;

namespace LibMatrix.Utilities.Bot.Commands;

public class AliassesCommand(IServiceProvider services) : ICommand {
    public string Name { get; } = "aliasses";
    public string[]? Aliases { get; }
    public string Description { get; } = "Displays aliasses for a command";
    public bool Unlisted { get; } = true;
    //TODO: implement command

    public async Task Invoke(CommandContext ctx) {
        var sb = new StringBuilder();
        sb.AppendLine("Available commands:");
        var commands = services.GetServices<ICommand>().Where(x => !x.Unlisted).ToList();
        foreach (var command in commands) sb.AppendLine($"- {command.Name}: {command.Description}");

        await ctx.Room.SendMessageEventAsync(new RoomMessageLegacyEventContent("m.notice", sb.ToString()));
        
        var msb = new MessageBuilder("m.notice");
        msb.WithHtmlTag("table", tb => {
            tb.WithHtmlTag("thead", th => th.WithBody("Available commands"));
            tb.WithHtmlTag("tr", tr => {
                tr.WithHtmlTag("th", th => th.WithBody("Command"));
                tr.WithHtmlTag("th", th => th.WithBody("Aliasses"));
                tr.WithHtmlTag("th", th => th.WithBody("Description"));
            });
            foreach (var command in commands) {
                tb.WithHtmlTag("tr", tr => {
                    tr.WithHtmlTag("td", td => td.WithBody(command.Name));
                    tr.WithHtmlTag("td", td => td.WithBody(string.Join(", ", command.Aliases)));
                    tr.WithHtmlTag("td", td => td.WithBody(command.Description));
                });
            }
        });
        await ctx.Room.SendMessageEventAsync(msb.Build());
    }
}