about summary refs log tree commit diff
path: root/Utilities/LibMatrix.Utilities.Bot/Commands
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-03-20 12:00:54 +0100
committerRory& <root@rory.gay>2024-03-20 12:00:54 +0100
commitb992d20da79b9de020d629bf9574abefff9c4b12 (patch)
treea5205343b6edbc8f627215efdd147d86a4a737c7 /Utilities/LibMatrix.Utilities.Bot/Commands
parentBot related fixes, image size (diff)
downloadLibMatrix-b992d20da79b9de020d629bf9574abefff9c4b12.tar.xz
New messagebuilder stuff, table-based help command
Diffstat (limited to '')
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/Commands/AliassesCommand.cs43
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs61
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs2
3 files changed, 85 insertions, 21 deletions
diff --git a/Utilities/LibMatrix.Utilities.Bot/Commands/AliassesCommand.cs b/Utilities/LibMatrix.Utilities.Bot/Commands/AliassesCommand.cs
new file mode 100644
index 0000000..5c9c480
--- /dev/null
+++ b/Utilities/LibMatrix.Utilities.Bot/Commands/AliassesCommand.cs
@@ -0,0 +1,43 @@
+using System.Collections.Frozen;
+using System.Text;
+using LibMatrix.EventTypes.Spec;
+using LibMatrix.Helpers;
+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 RoomMessageEventContent("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());
+    }
+}
\ No newline at end of file
diff --git a/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs b/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs
index 979fab6..0abc76b 100644
--- a/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs
+++ b/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs
@@ -1,6 +1,7 @@
 using System.Collections.Frozen;
 using System.Text;
 using LibMatrix.EventTypes.Spec;
+using LibMatrix.Helpers;
 using LibMatrix.Utilities.Bot.Interfaces;
 using Microsoft.Extensions.DependencyInjection;
 
@@ -13,27 +14,47 @@ public class HelpCommand(IServiceProvider services) : ICommand {
     public bool Unlisted { get; }
 
     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 RoomMessageEventContent("m.notice", sb.ToString()));
+        var commands = services.GetServices<ICommand>()
+            .Where(x => !x.Unlisted
+                        && !x.GetType().GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommand<>))
+            ).ToList();
+
+        var msb = GenerateCommandList(commands);
+        
+        await ctx.Room.SendMessageEventAsync(msb.Build());
     }
-}
-
-public class HelpCommandWithSubCommands<T>(T command) where T : ICommandGroup {
-    public string Name { get; } = "help";
-    public string[]? Aliases { get; } = new[] { "?" };
-    public string Description { get; } = "Displays this help message";
-
-    public async Task Invoke(CommandContext ctx) {
-        var sb = new StringBuilder();
-        sb.AppendLine("Available subcommands:");
-        var commands = command.SubCommands;
-
-        foreach (var command in commands) sb.AppendLine($"- {command.Name}: {command.Description}");
 
-        await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent("m.notice", sb.ToString()));
+    public static MessageBuilder GenerateCommandList(List<ICommand> commands, MessageBuilder? msb = null) {
+        msb ??= new MessageBuilder("m.notice");
+        msb.WithTable(tb => {
+            tb.WithTitle("Available commands", 2);
+            tb.WithRow(rb => {
+                rb.WithCell("Command");
+                rb.WithCell("Description");
+            });
+
+            foreach (var command in commands) {
+                tb.WithRow(rb => {
+                    rb.WithCell(command.Name);
+                    rb.WithCell(command.Description);
+                });
+            }
+        });
+        // msb.WithHtmlTag("table", tb => {
+            // tb.WithHtmlTag("thead",
+                // th => { th.WithHtmlTag("tr", tr => { tr.WithHtmlTag("th", th => th.WithBody("Available commands"), new Dictionary<string, string> { ["colspan"] = "2" }); }); });
+            // tb.WithHtmlTag("tr", tr => {
+                // tr.WithHtmlTag("th", th => th.WithBody("Command"));
+                // 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(command.Description));
+                // });
+            // }
+        // });
+        
+        return msb;
     }
 }
\ No newline at end of file
diff --git a/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs b/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs
index 5e021a2..76e48f5 100644
--- a/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs
+++ b/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs
@@ -5,7 +5,7 @@ namespace LibMatrix.Utilities.Bot.Commands;
 
 public class PingCommand : ICommand {
     public string Name { get; } = "ping";
-    public string[]? Aliases { get; } = [ "?" ];
+    public string[]? Aliases { get; } = [ ];
     public string Description { get; } = "Pong!";
     public bool Unlisted { get; }