From 6db2a6bf7a203c05d478d3b6b5a7636fe622ffb3 Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 24 Apr 2025 01:36:14 +0200 Subject: Fix command names with spaces, allow specifying formatted body in MessageBuilder#WithBody --- LibMatrix/Helpers/MessageBuilder.cs | 6 +++--- .../Services/CommandListenerHostedService.cs | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/LibMatrix/Helpers/MessageBuilder.cs b/LibMatrix/Helpers/MessageBuilder.cs index d3bd6a5..5e2b1b7 100644 --- a/LibMatrix/Helpers/MessageBuilder.cs +++ b/LibMatrix/Helpers/MessageBuilder.cs @@ -10,9 +10,9 @@ public class MessageBuilder(string msgType = "m.text", string format = "org.matr public RoomMessageEventContent Build() => Content; - public MessageBuilder WithBody(string body) { + public MessageBuilder WithBody(string body, string? formattedBody = null) { Content.Body += body; - Content.FormattedBody += body; + Content.FormattedBody += formattedBody ?? body; return this; } @@ -112,7 +112,7 @@ public class MessageBuilder(string msgType = "m.text", string format = "org.matr public class TableBuilder(MessageBuilder msb) { public TableBuilder WithTitle(string title, int colspan) { msb.Content.Body += title + "\n"; - msb.Content.FormattedBody += $"{title}"; + msb.Content.FormattedBody += $"{title}
"; return this; } diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs index f9b46d2..750e6a3 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs @@ -137,15 +137,19 @@ public class CommandListenerHostedService : IHostedService { var room = _hs.GetRoom(evt.RoomId!); var commandWithoutPrefix = message.BodyWithoutReplyFallback[usedPrefix.Length..].Trim(); + var usedCommand = _commands + .SelectMany(x => [x.Name, ..x.Aliases ?? []]) + .OrderByDescending(x => x.Length) + .FirstOrDefault(commandWithoutPrefix.StartsWith); var ctx = new CommandContext { Room = room, MessageEvent = @evt, Homeserver = _hs, Args = commandWithoutPrefix.Split(' ').Length == 1 ? [] : commandWithoutPrefix.Split(' ')[1..], - CommandName = commandWithoutPrefix.Split(' ')[0] + CommandName = usedCommand ?? commandWithoutPrefix.Split(' ')[0] }; try { - var command = _commands.SingleOrDefault(x => x.Name == commandWithoutPrefix.Split(' ')[0] || x.Aliases?.Contains(commandWithoutPrefix.Split(' ')[0]) == true); + var command = _commands.SingleOrDefault(x => x.Name == ctx.CommandName || x.Aliases?.Contains(ctx.CommandName) == true); if (command == null) { await room.SendMessageEventAsync( new RoomMessageEventContent("m.notice", $"Command \"{ctx.CommandName}\" not found!")); -- cgit 1.5.1