From f5447484512d726f4403f0d7725777d0a95601fb Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Tue, 19 Sep 2023 00:16:36 +0200 Subject: Add more stuff, add unit tests --- .../Bot/Commands/CmdCommand.cs | 10 +++--- .../Bot/Commands/HelpCommand.cs | 4 +-- .../Bot/Commands/PingCommand.cs | 4 +-- .../Bot/Interfaces/CommandContext.cs | 2 +- ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs | 17 +++++----- .../Bot/StartupTasks/ServerRoomSizeCalulator.cs | 3 +- .../Bot/Commands/BanMediaCommand.cs | 34 ++++++++++---------- ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs | 37 +++++++++++----------- .../Bot/Commands/CreateSystemCommand.cs | 10 +++--- .../PluralContactBotPoC/Bot/PluralContactBot.cs | 23 +++++++------- ExampleBots/PluralContactBotPoC/Program.cs | 1 - 11 files changed, 73 insertions(+), 72 deletions(-) (limited to 'ExampleBots') diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs index 5b2828e..f3b4dde 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs @@ -1,6 +1,6 @@ using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec; using LibMatrix.ExampleBot.Bot.Interfaces; -using LibMatrix.StateEventTypes.Spec; namespace LibMatrix.ExampleBot.Bot.Commands; @@ -18,7 +18,7 @@ public class CmdCommand : ICommand { cmd = cmd.Trim(); cmd += "\""; - await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventContent(body: $"Command being executed: `{cmd}`")); + await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: $"Command being executed: `{cmd}`")); var output = ArcaneLibs.Util.GetCommandOutputAsync( Environment.OSVersion.Platform == PlatformID.Unix ? "/bin/sh" : "cmd.exe", @@ -27,7 +27,7 @@ public class CmdCommand : ICommand { // .Split("\n").ToList(); var msg = ""; - EventIdResponse? msgId = await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventContent { + EventIdResponse? msgId = await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent { FormattedBody = $"Waiting for command output...", Body = msg.RemoveAnsi(), Format = "m.notice" @@ -38,14 +38,14 @@ public class CmdCommand : ICommand { Console.WriteLine($"{@out.Length:0000} {@out}"); msg += @out + "\n"; if (lastSendTask.IsCompleted) - lastSendTask = ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventContent { + lastSendTask = ctx.Room.SendMessageEventAsync(new RoomMessageEventContent { FormattedBody = $"
\n{msg}\n
", Body = msg.RemoveAnsi(), Format = "org.matrix.custom.html" }); if (msg.Length > 31000) { await lastSendTask; - msgId = await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventContent { + msgId = await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent { FormattedBody = $"Waiting for command output...", Body = msg.RemoveAnsi(), Format = "m.notice" diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/HelpCommand.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/HelpCommand.cs index c750130..23c4fe2 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/HelpCommand.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/HelpCommand.cs @@ -1,6 +1,6 @@ using System.Text; +using LibMatrix.EventTypes.Spec; using LibMatrix.ExampleBot.Bot.Interfaces; -using LibMatrix.StateEventTypes.Spec; using Microsoft.Extensions.DependencyInjection; namespace LibMatrix.ExampleBot.Bot.Commands; @@ -17,6 +17,6 @@ public class HelpCommand(IServiceProvider services) : ICommand { sb.AppendLine($"- {command.Name}: {command.Description}"); } - await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventContent(body: sb.ToString())); + await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: sb.ToString())); } } diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/PingCommand.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/PingCommand.cs index a261a59..ba242fe 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/PingCommand.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/PingCommand.cs @@ -1,5 +1,5 @@ +using LibMatrix.EventTypes.Spec; using LibMatrix.ExampleBot.Bot.Interfaces; -using LibMatrix.StateEventTypes.Spec; namespace LibMatrix.ExampleBot.Bot.Commands; @@ -8,6 +8,6 @@ public class PingCommand : ICommand { public string Description { get; } = "Pong!"; public async Task Invoke(CommandContext ctx) { - await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventContent(body: "pong!")); + await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: "pong!")); } } diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/Interfaces/CommandContext.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/Interfaces/CommandContext.cs index 3715cb6..9b6ef7a 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/Interfaces/CommandContext.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/Interfaces/CommandContext.cs @@ -1,6 +1,6 @@ +using LibMatrix.EventTypes.Spec; using LibMatrix.Responses; using LibMatrix.RoomTypes; -using LibMatrix.StateEventTypes.Spec; namespace LibMatrix.ExampleBot.Bot.Interfaces; diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs index 0b4e2ba..f04ec3a 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs @@ -1,10 +1,11 @@ using System.Diagnostics.CodeAnalysis; using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec; +using LibMatrix.EventTypes.Spec.State; using LibMatrix.ExampleBot.Bot.Interfaces; using LibMatrix.Extensions; using LibMatrix.Homeservers; using LibMatrix.Services; -using LibMatrix.StateEventTypes.Spec; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -43,7 +44,7 @@ public class MRUBot : IHostedService { throw; } - await (await hs.GetRoom("!DoHEdFablOLjddKWIp:rory.gay")).JoinAsync(); + await (hs.GetRoom("!DoHEdFablOLjddKWIp:rory.gay")).JoinAsync(); // foreach (var room in await hs.GetJoinedRooms()) { // if(room.RoomId is "!OGEhHVWSdvArJzumhm:matrix.org") continue; @@ -61,12 +62,12 @@ public class MRUBot : IHostedService { $"Got invite to {args.Key} by {inviteEvent.Sender} with reason: {(inviteEvent.TypedContent as RoomMemberEventContent).Reason}"); if (inviteEvent.Sender.EndsWith(":rory.gay") || inviteEvent.Sender == "@mxidupwitch:the-apothecary.club") { try { - var senderProfile = await hs.GetProfile(inviteEvent.Sender); - await (await hs.GetRoom(args.Key)).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!"); + var senderProfile = await hs.GetProfileAsync(inviteEvent.Sender); + await (hs.GetRoom(args.Key)).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!"); } catch (Exception e) { _logger.LogError("{}", e.ToString()); - await (await hs.GetRoom(args.Key)).LeaveAsync(reason: "I was unable to join the room: " + e); + await (hs.GetRoom(args.Key)).LeaveAsync(reason: "I was unable to join the room: " + e); } } }); @@ -74,13 +75,13 @@ public class MRUBot : IHostedService { _logger.LogInformation( "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(indent: false, ignoreNull: true)); - var room = await hs.GetRoom(@event.RoomId); + var room = hs.GetRoom(@event.RoomId); // _logger.LogInformation(eventResponse.ToJson(indent: false)); if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message }) { if (message is { MessageType: "m.text" } && message.Body.StartsWith(_configuration.Prefix)) { var command = _commands.FirstOrDefault(x => x.Name == message.Body.Split(' ')[0][_configuration.Prefix.Length..]); if (command == null) { - await room.SendMessageEventAsync("m.room.message", + await room.SendMessageEventAsync( new RoomMessageEventContent(messageType: "m.text", body: "Command not found!")); return; } @@ -93,7 +94,7 @@ public class MRUBot : IHostedService { await command.Invoke(ctx); } else { - await room.SendMessageEventAsync("m.room.message", + await room.SendMessageEventAsync( new RoomMessageEventContent(messageType: "m.text", body: "You do not have permission to run this command!")); } } diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs index 4785192..890db85 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs @@ -3,7 +3,6 @@ using ArcaneLibs.Extensions; using LibMatrix.ExampleBot.Bot.Interfaces; using LibMatrix.Homeservers; using LibMatrix.Services; -using LibMatrix.StateEventTypes.Spec; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -39,7 +38,7 @@ public class ServerRoomSizeCalulator : IHostedService { throw; } - await (await hs.GetRoom("!DoHEdFablOLjddKWIp:rory.gay")).JoinAsync(); + await (hs.GetRoom("!DoHEdFablOLjddKWIp:rory.gay")).JoinAsync(); Dictionary totalRoomSize = new(); foreach (var room in await hs.GetJoinedRooms()) { diff --git a/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs b/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs index 4642007..d633f89 100644 --- a/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs +++ b/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs @@ -1,11 +1,11 @@ using System.Security.Cryptography; using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec; using LibMatrix.Helpers; using LibMatrix.Responses; using LibMatrix.Services; -using LibMatrix.StateEventTypes.Spec; +using LibMatrix.Utilities.Bot.Interfaces; using MediaModeratorPoC.Bot.AccountData; -using MediaModeratorPoC.Bot.Interfaces; using MediaModeratorPoC.Bot.StateEventTypes; namespace MediaModeratorPoC.Bot.Commands; @@ -17,11 +17,11 @@ public class BanMediaCommand(IServiceProvider services, HomeserverProviderServic public async Task CanInvoke(CommandContext ctx) { //check if user is admin in control room var botData = await ctx.Homeserver.GetAccountData("gay.rory.media_moderator_poc_data"); - var controlRoom = await ctx.Homeserver.GetRoom(botData.ControlRoom); + var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom); var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasPermission(ctx.MessageEvent.Sender, "m.room.ban"); if (!isAdmin) { // await ctx.Reply("You do not have permission to use this command!"); - await (await ctx.Homeserver.GetRoom(botData.LogRoom!)).SendMessageEventAsync("m.room.message", + await ctx.Homeserver.GetRoom(botData.LogRoom!).SendMessageEventAsync( new RoomMessageEventContent(body: $"User {ctx.MessageEvent.Sender} tried to use command {Name} but does not have permission!", messageType: "m.text")); } @@ -30,14 +30,14 @@ public class BanMediaCommand(IServiceProvider services, HomeserverProviderServic public async Task Invoke(CommandContext ctx) { var botData = await ctx.Homeserver.GetAccountData("gay.rory.media_moderator_poc_data"); - var policyRoom = await ctx.Homeserver.GetRoom(botData.PolicyRoom ?? botData.ControlRoom); - var logRoom = await ctx.Homeserver.GetRoom(botData.LogRoom ?? botData.ControlRoom); + var policyRoom = ctx.Homeserver.GetRoom(botData.PolicyRoom ?? botData.ControlRoom); + var logRoom = ctx.Homeserver.GetRoom(botData.LogRoom ?? botData.ControlRoom); //check if reply var messageContent = ctx.MessageEvent.TypedContent as RoomMessageEventContent; if (messageContent?.RelatesTo is { InReplyTo: not null }) { try { - await logRoom.SendMessageEventAsync("m.room.message", + await logRoom.SendMessageEventAsync( new RoomMessageEventContent( body: $"User {MessageFormatter.HtmlFormatMention(ctx.MessageEvent.Sender)} is trying to ban media {messageContent!.RelatesTo!.InReplyTo!.EventId}", messageType: "m.text")); @@ -47,14 +47,14 @@ public class BanMediaCommand(IServiceProvider services, HomeserverProviderServic //check if recommendation is in list if (ctx.Args.Length < 2) { - await ctx.Room.SendMessageEventAsync("m.room.message", MessageFormatter.FormatError("You must specify a recommendation type and reason!")); + await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatError("You must specify a recommendation type and reason!")); return; } var recommendation = ctx.Args[0]; if (recommendation is not ("ban" or "kick" or "mute" or "redact" or "spoiler" or "warn" or "warn_admins")) { - await ctx.Room.SendMessageEventAsync("m.room.message", MessageFormatter.FormatError($"Invalid recommendation type {recommendation}, must be `warn_admins`, `warn`, `spoiler`, `redact`, `mute`, `kick` or `ban`!")); + await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatError($"Invalid recommendation type {recommendation}, must be `warn_admins`, `warn`, `spoiler`, `redact`, `mute`, `kick` or `ban`!")); return; } @@ -69,7 +69,7 @@ public class BanMediaCommand(IServiceProvider services, HomeserverProviderServic fileHash = await hashAlgo.ComputeHashAsync(await ctx.Homeserver._httpClient.GetStreamAsync(resolvedUri)); } catch (Exception ex) { - await logRoom.SendMessageEventAsync("m.room.message", + await logRoom.SendMessageEventAsync( MessageFormatter.FormatException($"Error calculating file hash for {mxcUri} via {mxcUri.Split('/')[2]}, retrying via {ctx.Homeserver.HomeServerDomain}...", ex)); try { @@ -77,8 +77,8 @@ public class BanMediaCommand(IServiceProvider services, HomeserverProviderServic fileHash = await hashAlgo.ComputeHashAsync(await ctx.Homeserver._httpClient.GetStreamAsync(resolvedUri)); } catch (Exception ex2) { - await ctx.Room.SendMessageEventAsync("m.room.message", MessageFormatter.FormatException("Error calculating file hash", ex2)); - await logRoom.SendMessageEventAsync("m.room.message", + await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatException("Error calculating file hash", ex2)); + await logRoom.SendMessageEventAsync( MessageFormatter.FormatException($"Error calculating file hash via {ctx.Homeserver.HomeServerDomain}!", ex2)); } } @@ -91,18 +91,18 @@ public class BanMediaCommand(IServiceProvider services, HomeserverProviderServic Recommendation = recommendation, }); - await ctx.Room.SendMessageEventAsync("m.room.message", MessageFormatter.FormatSuccessJson("Media policy created", policy)); - await logRoom.SendMessageEventAsync("m.room.message", MessageFormatter.FormatSuccessJson("Media policy created", policy)); + await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatSuccessJson("Media policy created", policy)); + await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccessJson("Media policy created", policy)); } catch (Exception e) { - await logRoom.SendMessageEventAsync("m.room.message", MessageFormatter.FormatException("Error creating policy", e)); - await ctx.Room.SendMessageEventAsync("m.room.message", MessageFormatter.FormatException("Error creating policy", e)); + await logRoom.SendMessageEventAsync(MessageFormatter.FormatException("Error creating policy", e)); + await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatException("Error creating policy", e)); await using var stream = new MemoryStream(e.ToString().AsBytes().ToArray()); await logRoom.SendFileAsync("m.file", "error.log.cs", stream); } } else { - await ctx.Room.SendMessageEventAsync("m.room.message", MessageFormatter.FormatError("This command must be used in reply to a message!")); + await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatError("This command must be used in reply to a message!")); } } } diff --git a/ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs b/ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs index 7104114..e6ba269 100644 --- a/ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs +++ b/ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs @@ -6,14 +6,15 @@ using System.Text.Encodings.Web; using System.Text.RegularExpressions; using ArcaneLibs.Extensions; using LibMatrix; +using LibMatrix.EventTypes.Spec; +using LibMatrix.EventTypes.Spec.State; using LibMatrix.Helpers; using LibMatrix.Homeservers; using LibMatrix.Responses; using LibMatrix.RoomTypes; using LibMatrix.Services; -using LibMatrix.StateEventTypes.Spec; +using LibMatrix.Utilities.Bot.Interfaces; using MediaModeratorPoC.Bot.AccountData; -using MediaModeratorPoC.Bot.Interfaces; using MediaModeratorPoC.Bot.StateEventTypes; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -86,9 +87,9 @@ public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger await hs.SetAccountData("gay.rory.media_moderator_poc_data", botData); } - _policyRoom = await hs.GetRoom(botData.PolicyRoom ?? botData.ControlRoom); - _logRoom = await hs.GetRoom(botData.LogRoom ?? botData.ControlRoom); - _controlRoom = await hs.GetRoom(botData.ControlRoom); + _policyRoom = hs.GetRoom(botData.PolicyRoom ?? botData.ControlRoom); + _logRoom = hs.GetRoom(botData.LogRoom ?? botData.ControlRoom); + _controlRoom = hs.GetRoom(botData.ControlRoom); List admins = new(); @@ -113,18 +114,18 @@ public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger $"Got invite to {args.Key} by {inviteEvent.Sender} with reason: {(inviteEvent.TypedContent as RoomMemberEventContent).Reason}"); if (inviteEvent.Sender.EndsWith(":rory.gay") || inviteEvent.Sender.EndsWith(":conduit.rory.gay")) { try { - var senderProfile = await hs.GetProfile(inviteEvent.Sender); - await (await hs.GetRoom(args.Key)).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!"); + var senderProfile = await hs.GetProfileAsync(inviteEvent.Sender); + await (hs.GetRoom(args.Key)).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!"); } catch (Exception e) { logger.LogError("{}", e.ToString()); - await (await hs.GetRoom(args.Key)).LeaveAsync(reason: "I was unable to join the room: " + e); + await (hs.GetRoom(args.Key)).LeaveAsync(reason: "I was unable to join the room: " + e); } } }); hs.SyncHelper.TimelineEventHandlers.Add(async @event => { - var room = await hs.GetRoom(@event.RoomId); + var room = hs.GetRoom(@event.RoomId); try { logger.LogInformation( "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(indent: true, ignoreNull: true)); @@ -136,7 +137,7 @@ public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger if (matchedPolicy is null) return; var matchedpolicyData = matchedPolicy.TypedContent as MediaPolicyEventContent; var recommendation = matchedpolicyData.Recommendation; - await _logRoom.SendMessageEventAsync("m.room.message", + await _logRoom.SendMessageEventAsync( new RoomMessageEventContent( body: $"User {MessageFormatter.HtmlFormatMention(@event.Sender)} posted an image in {MessageFormatter.HtmlFormatMention(room.RoomId)} that matched rule {matchedPolicy.StateKey}, applying action {matchedpolicyData.Recommendation}, as described in rule: {matchedPolicy.RawContent!.ToJson(ignoreNull: true)}", @@ -147,7 +148,7 @@ public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger }); switch (recommendation) { case "warn_admins": { - await _controlRoom.SendMessageEventAsync("m.room.message", + await _controlRoom.SendMessageEventAsync( new RoomMessageEventContent( body: $"{string.Join(' ', admins)}\nUser {MessageFormatter.HtmlFormatMention(@event.Sender)} posted a banned image {message.Url}", messageType: "m.text") { @@ -158,7 +159,7 @@ public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger break; } case "warn": { - await room.SendMessageEventAsync("m.room.message", + await room.SendMessageEventAsync( new RoomMessageEventContent( body: $"Please be careful when posting this image: {matchedpolicyData.Reason}", messageType: "m.text") { @@ -184,7 +185,7 @@ public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger // // // - await room.SendMessageEventAsync("m.room.message", + await room.SendMessageEventAsync( new RoomMessageEventContent( body: $"Please be careful when posting this image: {matchedpolicyData.Reason}, I have spoilered it for you:", @@ -194,7 +195,7 @@ public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger $"Please be careful when posting this image: {matchedpolicyData.Reason}, I have spoilered it for you:" }); var imageUrl = message.Url; - await room.SendMessageEventAsync("m.room.message", + await room.SendMessageEventAsync( new RoomMessageEventContent(body: $"CN: {imageUrl}", messageType: "m.text") { Format = "org.matrix.custom.html", @@ -244,9 +245,9 @@ public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger } catch (Exception e) { logger.LogError("{}", e.ToString()); - await _controlRoom.SendMessageEventAsync("m.room.message", + await _controlRoom.SendMessageEventAsync( MessageFormatter.FormatException($"Unable to ban user in {MessageFormatter.HtmlFormatMention(room.RoomId)}", e)); - await _logRoom.SendMessageEventAsync("m.room.message", + await _logRoom.SendMessageEventAsync( MessageFormatter.FormatException($"Unable to ban user in {MessageFormatter.HtmlFormatMention(room.RoomId)}", e)); await using var stream = new MemoryStream(e.ToString().AsBytes().ToArray()); await _controlRoom.SendFileAsync("m.file", "error.log.cs", stream); @@ -274,7 +275,7 @@ public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger fileHash = await hashAlgo.ComputeHashAsync(await hs._httpClient.GetStreamAsync(resolvedUri)); } catch (Exception ex) { - await _logRoom.SendMessageEventAsync("m.room.message", + await _logRoom.SendMessageEventAsync( MessageFormatter.FormatException($"Error calculating file hash for {mxcUri} via {mxcUri.Split('/')[2]} ({resolvedUri}), retrying via {hs.HomeServerDomain}...", ex)); try { @@ -282,7 +283,7 @@ public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger fileHash = await hashAlgo.ComputeHashAsync(await hs._httpClient.GetStreamAsync(resolvedUri)); } catch (Exception ex2) { - await _logRoom.SendMessageEventAsync("m.room.message", + await _logRoom.SendMessageEventAsync( MessageFormatter.FormatException($"Error calculating file hash via {hs.HomeServerDomain} ({resolvedUri})!", ex2)); } } diff --git a/ExampleBots/PluralContactBotPoC/Bot/Commands/CreateSystemCommand.cs b/ExampleBots/PluralContactBotPoC/Bot/Commands/CreateSystemCommand.cs index 5da4f5e..55624a8 100644 --- a/ExampleBots/PluralContactBotPoC/Bot/Commands/CreateSystemCommand.cs +++ b/ExampleBots/PluralContactBotPoC/Bot/Commands/CreateSystemCommand.cs @@ -1,8 +1,8 @@ using LibMatrix; +using LibMatrix.EventTypes.Spec.State; using LibMatrix.Helpers; using LibMatrix.Services; -using LibMatrix.StateEventTypes.Spec; -using MediaModeratorPoC.Bot.Interfaces; +using LibMatrix.Utilities.Bot.Interfaces; using PluralContactBotPoC.Bot.AccountData; using PluralContactBotPoC.Bot.StateEventTypes; @@ -18,7 +18,7 @@ public class CreateSystemCommand(IServiceProvider services, HomeserverProviderSe public async Task Invoke(CommandContext ctx) { if (ctx.Args.Length != 1) { - await ctx.Reply("m.notice", MessageFormatter.FormatError("Only one argument is allowed: system name!")); + await ctx.Reply(MessageFormatter.FormatError("Only one argument is allowed: system name!")); return; } @@ -26,7 +26,7 @@ public class CreateSystemCommand(IServiceProvider services, HomeserverProviderSe try { try { await ctx.Homeserver.GetAccountData("gay.rory.plural_contact_bot.system_data"); - await ctx.Reply("m.notice", MessageFormatter.FormatError($"System {sysName} already exists!")); + await ctx.Reply(MessageFormatter.FormatError($"System {sysName} already exists!")); } catch (MatrixException e) { if (e is { ErrorCode: "M_NOT_FOUND" }) { @@ -51,7 +51,7 @@ public class CreateSystemCommand(IServiceProvider services, HomeserverProviderSe } } catch (Exception e) { - await ctx.Reply("m.notice", MessageFormatter.FormatException("Something went wrong!", e)); + await ctx.Reply(MessageFormatter.FormatException("Something went wrong!", e)); } } } diff --git a/ExampleBots/PluralContactBotPoC/Bot/PluralContactBot.cs b/ExampleBots/PluralContactBotPoC/Bot/PluralContactBot.cs index 2136b42..c3cebe2 100644 --- a/ExampleBots/PluralContactBotPoC/Bot/PluralContactBot.cs +++ b/ExampleBots/PluralContactBotPoC/Bot/PluralContactBot.cs @@ -1,12 +1,13 @@ using System.Text; using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec; +using LibMatrix.EventTypes.Spec.State; using LibMatrix.Helpers; using LibMatrix.Homeservers; using LibMatrix.RoomTypes; using LibMatrix.Services; -using LibMatrix.StateEventTypes.Spec; using LibMatrix.Utilities.Bot; -using MediaModeratorPoC.Bot.Interfaces; +using LibMatrix.Utilities.Bot.Interfaces; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using PluralContactBotPoC.Bot.AccountData; @@ -35,7 +36,7 @@ public class PluralContactBot(AuthenticatedHomeserverGeneric hs, ILogger { var inviteEvent = @@ -46,9 +47,9 @@ public class PluralContactBot(AuthenticatedHomeserverGeneric hs, ILogger($"gay.rory.plural_contact_bot.system_data#{inviteEvent.StateKey}"); if (accountData.Members.Contains(inviteEvent.Sender)) { - await (await hs.GetRoom(args.Key)).JoinAsync(reason: "I was invited by a system member!"); + await (hs.GetRoom(args.Key)).JoinAsync(reason: "I was invited by a system member!"); - await _logRoom.SendMessageEventAsync("m.room.message", + await _logRoom.SendMessageEventAsync( MessageFormatter.FormatSuccess( $"I was invited by a system member ({MessageFormatter.HtmlFormatMention(inviteEvent.Sender)}) to {MessageFormatter.HtmlFormatMention(args.Key)}")); @@ -56,25 +57,25 @@ public class PluralContactBot(AuthenticatedHomeserverGeneric hs, ILogger { - var room = await hs.GetRoom(@event.RoomId); + var room = hs.GetRoom(@event.RoomId); try { logger.LogInformation( "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(indent: true, ignoreNull: true)); @@ -83,7 +84,7 @@ public class PluralContactBot(AuthenticatedHomeserverGeneric hs, ILogger