From b992d20da79b9de020d629bf9574abefff9c4b12 Mon Sep 17 00:00:00 2001 From: Rory& Date: Wed, 20 Mar 2024 12:00:54 +0100 Subject: New messagebuilder stuff, table-based help command --- LibMatrix/Helpers/MessageBuilder.cs | 37 ++++++++-- LibMatrix/Helpers/SyncHelper.cs | 2 - .../Homeservers/AuthenticatedHomeserverGeneric.cs | 2 - LibMatrix/Homeservers/FederationClient.cs | 5 -- LibMatrix/Homeservers/RemoteHomeServer.cs | 1 - LibMatrix/Responses/CreateRoomRequest.cs | 1 - LibMatrix/RoomTypes/SpaceRoom.cs | 1 - LibMatrix/StateEvent.cs | 2 - .../Commands/AliassesCommand.cs | 43 ++++++++++++ .../Commands/HelpCommand.cs | 61 +++++++++++------ .../Commands/PingCommand.cs | 2 +- .../LibMatrix.Utilities.Bot/Interfaces/ICommand.cs | 8 ++- .../Services/CommandListenerHostedService.cs | 79 ++++++++++++---------- 13 files changed, 165 insertions(+), 79 deletions(-) create mode 100644 Utilities/LibMatrix.Utilities.Bot/Commands/AliassesCommand.cs diff --git a/LibMatrix/Helpers/MessageBuilder.cs b/LibMatrix/Helpers/MessageBuilder.cs index 0753aca..d897078 100644 --- a/LibMatrix/Helpers/MessageBuilder.cs +++ b/LibMatrix/Helpers/MessageBuilder.cs @@ -1,4 +1,3 @@ -using ArcaneLibs; using LibMatrix.EventTypes.Spec; namespace LibMatrix.Helpers; @@ -50,7 +49,7 @@ public class MessageBuilder(string msgType = "m.text", string format = "org.matr Content.FormattedBody += ""; return this; } - + public MessageBuilder WithCustomEmoji(string mxcUri, string name) { Content.Body += $"{{{name}}}"; Content.FormattedBody += $"\"{name}\""; @@ -72,23 +71,51 @@ public class MessageBuilder(string msgType = "m.text", string format = "org.matr // } return this; } - + public MessageBuilder WithCodeBlock(string code, string language = "plaintext") { Content.Body += code; Content.FormattedBody += $"
{code}
"; return this; } - + public MessageBuilder WithCollapsibleSection(string title, string body) { Content.Body += body; Content.FormattedBody += $"
{title}{body}
"; return this; } - + public MessageBuilder WithCollapsibleSection(string title, Action bodyBuilder) { Content.FormattedBody += $"
{title}"; bodyBuilder(this); Content.FormattedBody += "
"; return this; } + + public MessageBuilder WithTable(Action tableBuilder) { + var tb = new TableBuilder(this); + this.WithHtmlTag("table", msb => tableBuilder(tb)); + return this; + } + + public class TableBuilder(MessageBuilder msb) { + public TableBuilder WithTitle(string title, int colspan) { + msb.Content.Body += title + "\n"; + msb.Content.FormattedBody += $"{title}"; + return this; + } + + public TableBuilder WithRow(Action rowBuilder) { + var rb = new RowBuilder(msb); + msb.WithHtmlTag("tr", msb => rowBuilder(rb)).WithBody("\n"); + return this; + } + + public class RowBuilder(MessageBuilder msb) { + public RowBuilder WithCell(string content, Dictionary? attributes = null) { + msb.Content.Body += content + "\n"; + msb.Content.FormattedBody += $"{content}\t"; + return this; + } + } + } } \ No newline at end of file diff --git a/LibMatrix/Helpers/SyncHelper.cs b/LibMatrix/Helpers/SyncHelper.cs index e696b70..1833bd0 100644 --- a/LibMatrix/Helpers/SyncHelper.cs +++ b/LibMatrix/Helpers/SyncHelper.cs @@ -1,7 +1,5 @@ using System.Diagnostics; using System.Net.Http.Json; -using System.Text.Json; -using System.Text.Json.Serialization; using ArcaneLibs.Extensions; using LibMatrix.Filters; using LibMatrix.Homeservers; diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs index 4f3bb41..727c0ea 100644 --- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs @@ -1,7 +1,5 @@ -using System.Diagnostics.CodeAnalysis; using System.Net.Http.Headers; using System.Net.Http.Json; -using System.Runtime.CompilerServices; using System.Text.Json; using System.Text.Json.Nodes; using System.Text.Json.Serialization; diff --git a/LibMatrix/Homeservers/FederationClient.cs b/LibMatrix/Homeservers/FederationClient.cs index 288b6b5..3926b29 100644 --- a/LibMatrix/Homeservers/FederationClient.cs +++ b/LibMatrix/Homeservers/FederationClient.cs @@ -1,10 +1,5 @@ -using System.Net.Http.Json; -using System.Text.Json; using System.Text.Json.Serialization; -using System.Web; -using ArcaneLibs.Extensions; using LibMatrix.Extensions; -using LibMatrix.Responses; using LibMatrix.Services; namespace LibMatrix.Homeservers; diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs index 422a8a9..8cd7ad7 100644 --- a/LibMatrix/Homeservers/RemoteHomeServer.cs +++ b/LibMatrix/Homeservers/RemoteHomeServer.cs @@ -1,5 +1,4 @@ using System.Net.Http.Json; -using System.Runtime.CompilerServices; using System.Text.Json; using System.Text.Json.Serialization; using System.Web; diff --git a/LibMatrix/Responses/CreateRoomRequest.cs b/LibMatrix/Responses/CreateRoomRequest.cs index ee4317e..6f47183 100644 --- a/LibMatrix/Responses/CreateRoomRequest.cs +++ b/LibMatrix/Responses/CreateRoomRequest.cs @@ -5,7 +5,6 @@ using System.Text.RegularExpressions; using LibMatrix.EventTypes; using LibMatrix.EventTypes.Spec.State; using LibMatrix.Homeservers; -using LibMatrix.Interfaces; namespace LibMatrix.Responses; diff --git a/LibMatrix/RoomTypes/SpaceRoom.cs b/LibMatrix/RoomTypes/SpaceRoom.cs index 49e751d..b40ccc6 100644 --- a/LibMatrix/RoomTypes/SpaceRoom.cs +++ b/LibMatrix/RoomTypes/SpaceRoom.cs @@ -1,6 +1,5 @@ using ArcaneLibs.Extensions; using LibMatrix.Homeservers; -using Microsoft.Extensions.Logging; namespace LibMatrix.RoomTypes; diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs index 541fb78..d9d0f4e 100644 --- a/LibMatrix/StateEvent.cs +++ b/LibMatrix/StateEvent.cs @@ -1,6 +1,4 @@ using System.Collections.Frozen; -using System.Collections.Immutable; -using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Text.Json; 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().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().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() + .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 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 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 { ["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; } diff --git a/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs b/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs index 4626a23..941d69e 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs @@ -1,5 +1,6 @@ using System.Collections.Frozen; using System.Collections.Immutable; +using Microsoft.Extensions.DependencyInjection; namespace LibMatrix.Utilities.Bot.Interfaces; @@ -14,7 +15,8 @@ public interface ICommand { public Task Invoke(CommandContext ctx); } +public interface ICommand : ICommand where T : ICommandGroup { } -public interface ICommandGroup : ICommand { - public IImmutableList SubCommands { get; } -} \ No newline at end of file +public interface ICommandGroup : ICommand { } + +public interface ICommandGroup : ICommandGroup where T : ICommandGroup { } \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs index fdf919b..6f22c03 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs @@ -112,11 +112,7 @@ public class CommandListenerHostedService : IHostedService { var message = evt.TypedContent as RoomMessageEventContent; var room = _hs.GetRoom(evt.RoomId!); - - var commandWithoutPrefix = message.BodyWithoutReplyFallback[usedPrefix.Length..]; - var command = _commands.OrderByDescending(x => x.Name.Length).FirstOrDefault(x => commandWithoutPrefix.StartsWith(x.Name)); - if (commandWithoutPrefix.Length != command.Name.Length && commandWithoutPrefix[command.Name.Length] != ' ') command = null; - + var commandWithoutPrefix = message.BodyWithoutReplyFallback[usedPrefix.Length..].Trim(); var ctx = new CommandContext { Room = room, MessageEvent = @evt, @@ -124,45 +120,56 @@ public class CommandListenerHostedService : IHostedService { Args = commandWithoutPrefix.Split(' ').Length == 1 ? [] : commandWithoutPrefix.Split(' ')[1..], CommandName = commandWithoutPrefix.Split(' ')[0] }; - if (command == null) { - await room.SendMessageEventAsync( - new RoomMessageEventContent("m.notice", $"Command \"{ctx.CommandName}\" not found!")); - return new() { - Success = false, - Result = CommandResult.CommandResultType.Failure_InvalidCommand, - Context = ctx - }; - } + try { + var command = _commands.SingleOrDefault(x => x.Name == commandWithoutPrefix.Split(' ')[0] || x.Aliases?.Contains(commandWithoutPrefix.Split(' ')[0]) == true); + if (command == null) { + await room.SendMessageEventAsync( + new RoomMessageEventContent("m.notice", $"Command \"{ctx.CommandName}\" not found!")); + return new() { + Success = false, + Result = CommandResult.CommandResultType.Failure_InvalidCommand, + Context = ctx + }; + } - if (await command.CanInvoke(ctx)) - try { - await command.Invoke(ctx); - } - catch (Exception e) { + if (await command.CanInvoke(ctx)) + try { + await command.Invoke(ctx); + } + catch (Exception e) { + return new CommandResult() { + Context = ctx, + Result = CommandResult.CommandResultType.Failure_Exception, + Success = false, + Exception = e + }; + // await room.SendMessageEventAsync( + // MessageFormatter.FormatException("An error occurred during the execution of this command", e)); + } + else return new CommandResult() { Context = ctx, - Result = CommandResult.CommandResultType.Failure_Exception, - Success = false, - Exception = e + Result = CommandResult.CommandResultType.Failure_NoPermission, + Success = false }; - // await room.SendMessageEventAsync( - // MessageFormatter.FormatException("An error occurred during the execution of this command", e)); - } - else + // await room.SendMessageEventAsync( + // new RoomMessageEventContent("m.notice", "You do not have permission to run this command!")); + return new CommandResult() { Context = ctx, - Result = CommandResult.CommandResultType.Failure_NoPermission, - Success = false + Success = true, + Result = CommandResult.CommandResultType.Success }; - // await room.SendMessageEventAsync( - // new RoomMessageEventContent("m.notice", "You do not have permission to run this command!")); - - return new CommandResult() { - Context = ctx, - Success = true, - Result = CommandResult.CommandResultType.Success - }; + } + catch (Exception e) { + return new CommandResult() { + Context = ctx, + Result = CommandResult.CommandResultType.Failure_Exception, + Success = false, + Exception = e + }; + } } private async Task HandleResult(CommandResult res) { -- cgit 1.4.1