From 677fe757733ab4af327ba74d047195be7d578e60 Mon Sep 17 00:00:00 2001 From: Rory& Date: Sun, 17 Mar 2024 13:30:38 +0100 Subject: Bot related fixes, image size --- ArcaneLibs | 2 +- LibMatrix.EventTypes/Spec/RoomMessageEventContent.cs | 6 ++++++ LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs | 13 +++++++++++-- Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs | 2 +- .../LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs | 4 ++-- .../Services/CommandListenerHostedService.cs | 14 ++++++++------ 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/ArcaneLibs b/ArcaneLibs index e94a5b1..409890c 160000 --- a/ArcaneLibs +++ b/ArcaneLibs @@ -1 +1 @@ -Subproject commit e94a5b1a6117e9597eca647df64e12dc855b304c +Subproject commit 409890ccfedf3ad3dd0ffd9866a19b730f613ea4 diff --git a/LibMatrix.EventTypes/Spec/RoomMessageEventContent.cs b/LibMatrix.EventTypes/Spec/RoomMessageEventContent.cs index f87fa62..ae893f8 100644 --- a/LibMatrix.EventTypes/Spec/RoomMessageEventContent.cs +++ b/LibMatrix.EventTypes/Spec/RoomMessageEventContent.cs @@ -46,5 +46,11 @@ public class RoomMessageEventContent : TimelineEventContent { [JsonPropertyName("thumbnail_url")] public string? ThumbnailUrl { get; set; } + + [JsonPropertyName("w")] + public int? Width { get; set; } + + [JsonPropertyName("h")] + public int? Height { get; set; } } } \ No newline at end of file diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs index b4c1cc9..4f3bb41 100644 --- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs @@ -76,6 +76,15 @@ public class AuthenticatedHomeserverGeneric(string serverName, string accessToke return rooms; } + public virtual async Task UploadFile(string fileName, IEnumerable data, string contentType = "application/octet-stream") { + return await UploadFile(fileName, data.ToArray(), contentType); + } + + public virtual async Task UploadFile(string fileName, byte[] data, string contentType = "application/octet-stream") { + await using var ms = new MemoryStream(data); + return await UploadFile(fileName, ms, contentType); + } + public virtual async Task UploadFile(string fileName, Stream fileStream, string contentType = "application/octet-stream") { var req = new HttpRequestMessage(HttpMethod.Post, $"/_matrix/media/v3/upload?filename={fileName}"); req.Content = new StreamContent(fileStream); @@ -320,13 +329,13 @@ public class AuthenticatedHomeserverGeneric(string serverName, string accessToke filter.Room?.Timeline?.Senders, filter.Room?.Timeline?.NotSenders ]; - + foreach (var list in senderLists) if (list is { Count: > 0 } && list.Contains("@me")) { list.Remove("@me"); list.Add(UserId); } - + var resp = await ClientHttpClient.PostAsJsonAsync("/_matrix/client/v3/user/" + UserId + "/filter", filter); return await resp.Content.ReadFromJsonAsync() ?? throw new Exception("Failed to upload filter?"); } diff --git a/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs b/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs index 9959bf6..5e021a2 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/CommandContext.cs b/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs index 062e99f..c6abde2 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs @@ -14,8 +14,8 @@ public class CommandContext { .SkipWhile(x => x.StartsWith(">")) .Aggregate((x, y) => $"{x}\n{y}"); - public string CommandName => MessageContentWithoutReply.Split(' ')[0][1..]; - public string[] Args => MessageContentWithoutReply.Split(' ')[1..]; + public required string CommandName; + public required string[] Args; public required AuthenticatedHomeserverGeneric Homeserver { get; set; } public async Task Reply(RoomMessageEventContent content) => await Room.SendMessageEventAsync(content); diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs index 1f91268..fdf919b 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs @@ -112,19 +112,21 @@ public class CommandListenerHostedService : IHostedService { var message = evt.TypedContent as RoomMessageEventContent; var room = _hs.GetRoom(evt.RoomId!); - var ctx = new CommandContext { - Room = room, - MessageEvent = @evt, - Homeserver = _hs - }; 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 ctx = new CommandContext { + Room = room, + MessageEvent = @evt, + Homeserver = _hs, + Args = commandWithoutPrefix.Split(' ').Length == 1 ? [] : commandWithoutPrefix.Split(' ')[1..], + CommandName = commandWithoutPrefix.Split(' ')[0] + }; if (command == null) { await room.SendMessageEventAsync( - new RoomMessageEventContent("m.notice", $"Command \"{commandWithoutPrefix.Split(' ')[0]}\" not found!")); + new RoomMessageEventContent("m.notice", $"Command \"{ctx.CommandName}\" not found!")); return new() { Success = false, Result = CommandResult.CommandResultType.Failure_InvalidCommand, -- cgit 1.4.1