From 17288cf70c97ea48c310ab876ee44554c09e8fe0 Mon Sep 17 00:00:00 2001 From: Rory& Date: Wed, 23 Apr 2025 22:15:00 +0200 Subject: Update devtestbot to use bot utils --- .../Bot/Commands/CmdCommand.cs | 4 +- .../Bot/Commands/DbgAniRainbowTest.cs | 7 ++- .../Bot/Commands/HelpCommand.cs | 4 +- .../Bot/Commands/PingCommand.cs | 6 ++- Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs | 49 +----------------- .../Bot/Interfaces/CommandContext.cs | 12 ----- .../Bot/Interfaces/ICommand.cs | 10 ---- Utilities/LibMatrix.DevTestBot/Bot/PingTestBot.cs | 58 +++++++++++----------- .../Bot/StartupTasks/ServerRoomSizeCalulator.cs | 4 +- 9 files changed, 47 insertions(+), 107 deletions(-) delete mode 100644 Utilities/LibMatrix.DevTestBot/Bot/Interfaces/CommandContext.cs delete mode 100644 Utilities/LibMatrix.DevTestBot/Bot/Interfaces/ICommand.cs (limited to 'Utilities/LibMatrix.DevTestBot/Bot') diff --git a/Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs b/Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs index 89a9033..874d195 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs @@ -1,11 +1,13 @@ using ArcaneLibs.StringNormalisation; using LibMatrix.EventTypes.Spec; -using LibMatrix.ExampleBot.Bot.Interfaces; +using LibMatrix.Utilities.Bot.Interfaces; namespace LibMatrix.ExampleBot.Bot.Commands; public class CmdCommand : ICommand { public string Name => "cmd"; + public string[]? Aliases => []; + public bool Unlisted => false; public string Description => "Runs a command on the host system"; public Task CanInvoke(CommandContext ctx) => Task.FromResult(ctx.MessageEvent.Sender.EndsWith(":rory.gay") || ctx.MessageEvent.Sender.EndsWith(":conduit.rory.gay")); diff --git a/Utilities/LibMatrix.DevTestBot/Bot/Commands/DbgAniRainbowTest.cs b/Utilities/LibMatrix.DevTestBot/Bot/Commands/DbgAniRainbowTest.cs index c91261f..0dde297 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/Commands/DbgAniRainbowTest.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/Commands/DbgAniRainbowTest.cs @@ -1,15 +1,20 @@ using System.Diagnostics; using LibMatrix.EventTypes.Spec; -using LibMatrix.ExampleBot.Bot.Interfaces; using LibMatrix.Helpers; using LibMatrix.Services; +using LibMatrix.Utilities.Bot.Interfaces; namespace ModerationBot.Commands; public class DbgAniRainbowTest(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver) : ICommand { public string Name { get; } = "ani-rainbow"; + + public string[]? Aliases => []; + public bool Unlisted => false; + public string Description { get; } = "[Debug] animated rainbow :)"; + public async Task CanInvoke(CommandContext ctx) => ctx.Room.RoomId == "!hLEefBaYvNfJwcTjmt:rory.gay"; public async Task Invoke(CommandContext ctx) { diff --git a/Utilities/LibMatrix.DevTestBot/Bot/Commands/HelpCommand.cs b/Utilities/LibMatrix.DevTestBot/Bot/Commands/HelpCommand.cs index 7ecbeb3..33bbc5a 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/Commands/HelpCommand.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/Commands/HelpCommand.cs @@ -1,12 +1,14 @@ using System.Text; using LibMatrix.EventTypes.Spec; -using LibMatrix.ExampleBot.Bot.Interfaces; +using LibMatrix.Utilities.Bot.Interfaces; using Microsoft.Extensions.DependencyInjection; namespace LibMatrix.ExampleBot.Bot.Commands; public class HelpCommand(IServiceProvider services) : ICommand { public string Name { get; } = "help"; + public string[]? Aliases => []; + public bool Unlisted => false; public string Description { get; } = "Displays this help message"; public async Task Invoke(CommandContext ctx) { diff --git a/Utilities/LibMatrix.DevTestBot/Bot/Commands/PingCommand.cs b/Utilities/LibMatrix.DevTestBot/Bot/Commands/PingCommand.cs index 745c75d..350d89e 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/Commands/PingCommand.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/Commands/PingCommand.cs @@ -1,10 +1,12 @@ using LibMatrix.EventTypes.Spec; -using LibMatrix.ExampleBot.Bot.Interfaces; +using LibMatrix.Utilities.Bot.Interfaces; namespace LibMatrix.ExampleBot.Bot.Commands; public class PingCommand : ICommand { - public string Name { get; } = "ping"; + public string Name { get; } = "do-ping"; + public string[]? Aliases => []; + public bool Unlisted => false; public string Description { get; } = "Pong!"; // public async Task Invoke(CommandContext ctx) => await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: "pong!")); diff --git a/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs b/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs index c650e2b..3bb0c25 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs @@ -2,7 +2,6 @@ using System.Diagnostics.CodeAnalysis; using ArcaneLibs.Extensions; using LibMatrix.EventTypes.Spec; using LibMatrix.EventTypes.Spec.State.RoomInfo; -using LibMatrix.ExampleBot.Bot.Interfaces; using LibMatrix.Helpers; using LibMatrix.Homeservers; using LibMatrix.Services; @@ -16,17 +15,13 @@ public class DevTestBot : IHostedService { private readonly HomeserverProviderService _homeserverProviderService; private readonly ILogger _logger; private readonly DevTestBotConfiguration _configuration; - private readonly IEnumerable _commands; public DevTestBot(HomeserverProviderService homeserverProviderService, ILogger logger, - DevTestBotConfiguration configuration, IServiceProvider services) { + DevTestBotConfiguration configuration) { logger.LogInformation("{} instantiated!", GetType().Name); _homeserverProviderService = homeserverProviderService; _logger = logger; _configuration = configuration; - _logger.LogInformation("Getting commands..."); - _commands = services.GetServices(); - _logger.LogInformation("Got {} commands!", _commands.Count()); } /// Triggered when the application host is ready to start the service. @@ -59,48 +54,6 @@ public class DevTestBot : IHostedService { // _logger.LogInformation($"Got room state for {room.RoomId}!"); // } - syncHelper.InviteReceivedHandlers.Add(async Task (args) => { - var inviteEvent = - args.Value.InviteState.Events.FirstOrDefault(x => - x.Type == "m.room.member" && x.StateKey == hs.UserId); - _logger.LogInformation( - $"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.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 hs.GetRoom(args.Key).LeaveAsync("I was unable to join the room: " + e); - } - }); - syncHelper.TimelineEventHandlers.Add(async @event => { - _logger.LogInformation( - "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(false, true)); - - 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( - new RoomMessageEventContent("m.text", "Command not found!")); - return; - } - - var ctx = new CommandContext { - Room = room, - MessageEvent = @event - }; - if (await command.CanInvoke(ctx)) - await command.Invoke(ctx); - else - await room.SendMessageEventAsync( - new RoomMessageEventContent("m.text", "You do not have permission to run this command!")); - } - }); await syncHelper.RunSyncLoopAsync(cancellationToken: cancellationToken); } diff --git a/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/CommandContext.cs b/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/CommandContext.cs deleted file mode 100644 index 221d1f4..0000000 --- a/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/CommandContext.cs +++ /dev/null @@ -1,12 +0,0 @@ -using LibMatrix.EventTypes.Spec; -using LibMatrix.RoomTypes; - -namespace LibMatrix.ExampleBot.Bot.Interfaces; - -public class CommandContext { - public required GenericRoom Room { get; init; } - public required StateEventResponse MessageEvent { get; init; } - public string CommandName => MessageContent.Body.Split(' ')[0][1..]; - public string[] Args => MessageContent.Body.Split(' ')[1..]; - private RoomMessageEventContent MessageContent => MessageEvent.TypedContent as RoomMessageEventContent ?? throw new Exception("Message content is not a RoomMessageEventContent"); -} \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/ICommand.cs b/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/ICommand.cs deleted file mode 100644 index a6dc8da..0000000 --- a/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/ICommand.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace LibMatrix.ExampleBot.Bot.Interfaces; - -public interface ICommand { - public string Name { get; } - public string Description { get; } - - public Task CanInvoke(CommandContext ctx) => Task.FromResult(true); - - public Task Invoke(CommandContext ctx); -} \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/Bot/PingTestBot.cs b/Utilities/LibMatrix.DevTestBot/Bot/PingTestBot.cs index 9c8ad67..e992e3c 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/PingTestBot.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/PingTestBot.cs @@ -1,8 +1,7 @@ using System.Diagnostics.CodeAnalysis; +using System.Windows.Input; using ArcaneLibs.Extensions; using LibMatrix.EventTypes.Spec; -using LibMatrix.EventTypes.Spec.State.RoomInfo; -using LibMatrix.ExampleBot.Bot.Interfaces; using LibMatrix.Filters; using LibMatrix.Helpers; using LibMatrix.Homeservers; @@ -86,33 +85,34 @@ public class PingTestBot : IHostedService { // await hs.GetRoom(args.Key).LeaveAsync("I was unable to join the room: " + e); // } // }); - syncHelper.TimelineEventHandlers.Add(async @event => { - _logger.LogInformation( - "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(false, true)); - - var room = hs.GetRoom(@event.RoomId); - // _logger.LogInformation(eventResponse.ToJson(indent: false)); - if (@event is not { Sender: "@emma:rory.gay" }) return; - 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( - new RoomMessageEventContent("m.text", "Command not found!")); - return; - } - - var ctx = new CommandContext { - Room = room, - MessageEvent = @event - }; - if (await command.CanInvoke(ctx)) - await command.Invoke(ctx); - else - await room.SendMessageEventAsync( - new RoomMessageEventContent("m.text", "You do not have permission to run this command!")); - } - }); + // Deprecated, using Bot Utils instead: + // syncHelper.TimelineEventHandlers.Add(async @event => { + // _logger.LogInformation( + // "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(false, true)); + // + // var room = hs.GetRoom(@event.RoomId); + // // _logger.LogInformation(eventResponse.ToJson(indent: false)); + // if (@event is not { Sender: "@emma:rory.gay" }) return; + // 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( + // new RoomMessageEventContent("m.text", "Command not found!")); + // return; + // } + // + // var ctx = new CommandContext { + // Room = room, + // MessageEvent = @event + // }; + // if (await command.CanInvoke(ctx)) + // await command.Invoke(ctx); + // else + // await room.SendMessageEventAsync( + // new RoomMessageEventContent("m.text", "You do not have permission to run this command!")); + // } + // }); await syncHelper.RunSyncLoopAsync(cancellationToken: cancellationToken); } diff --git a/Utilities/LibMatrix.DevTestBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs b/Utilities/LibMatrix.DevTestBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs index 253eb37..fdc7717 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs @@ -1,5 +1,4 @@ using System.Diagnostics.CodeAnalysis; -using LibMatrix.ExampleBot.Bot.Interfaces; using LibMatrix.Homeservers; using LibMatrix.Services; using Microsoft.Extensions.Hosting; @@ -11,10 +10,9 @@ public class ServerRoomSizeCalulator : IHostedService { private readonly HomeserverProviderService _homeserverProviderService; private readonly ILogger _logger; private readonly DevTestBotConfiguration _configuration; - private readonly IEnumerable _commands; public ServerRoomSizeCalulator(HomeserverProviderService homeserverProviderService, ILogger logger, - DevTestBotConfiguration configuration, IServiceProvider services) { + DevTestBotConfiguration configuration) { logger.LogInformation("Server room size calculator hosted service instantiated!"); _homeserverProviderService = homeserverProviderService; _logger = logger; -- cgit 1.5.1