about summary refs log tree commit diff
path: root/Utilities/LibMatrix.DevTestBot/Bot/Commands
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-04-23 22:15:00 +0200
committerRory& <root@rory.gay>2025-04-23 22:15:14 +0200
commit17288cf70c97ea48c310ab876ee44554c09e8fe0 (patch)
treea8aecca8602ee52b6487651473a4c2fa433622b2 /Utilities/LibMatrix.DevTestBot/Bot/Commands
parentAdd tombstone event content, URL-escape room id in synapse admin (diff)
downloadLibMatrix-unpacked-17288cf70c97ea48c310ab876ee44554c09e8fe0.tar.xz
Update devtestbot to use bot utils
Diffstat (limited to 'Utilities/LibMatrix.DevTestBot/Bot/Commands')
-rw-r--r--Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs72
-rw-r--r--Utilities/LibMatrix.DevTestBot/Bot/Commands/DbgAniRainbowTest.cs53
-rw-r--r--Utilities/LibMatrix.DevTestBot/Bot/Commands/HelpCommand.cs22
-rw-r--r--Utilities/LibMatrix.DevTestBot/Bot/Commands/PingCommand.cs6
4 files changed, 151 insertions, 2 deletions
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
@@ -0,0 +1,72 @@ +using ArcaneLibs.StringNormalisation; +using LibMatrix.EventTypes.Spec; +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<bool> CanInvoke(CommandContext ctx) => Task.FromResult(ctx.MessageEvent.Sender.EndsWith(":rory.gay") || ctx.MessageEvent.Sender.EndsWith(":conduit.rory.gay")); + + public async Task Invoke(CommandContext ctx) { + var cmd = ctx.Args.Aggregate("\"", (current, arg) => current + arg + " "); + + cmd = cmd.Trim(); + 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", + (Environment.OSVersion.Platform == PlatformID.Unix ? "-c " : "/c ") + cmd); + // .Replace("`", "\\`") + // .Split("\n").ToList(); + + var msg = ""; + var msgId = await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent { + FormattedBody = $"Waiting for command output...", + Body = msg.RemoveAnsi(), + Format = "m.notice" + }); + + var lastSendTask = Task.CompletedTask; + await foreach (var @out in output) { + Console.WriteLine($"{@out.Length:0000} {@out}"); + msg += @out + "\n"; + if (lastSendTask.IsCompleted) + lastSendTask = ctx.Room.SendMessageEventAsync(new RoomMessageEventContent { + FormattedBody = $"<pre class=\"language-csharp\">\n{msg}\n</pre>", + Body = msg.RemoveAnsi(), + Format = "org.matrix.custom.html" + }); + if (msg.Length > 31000) { + await lastSendTask; + msgId = await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent { + FormattedBody = $"Waiting for command output...", + Body = msg.RemoveAnsi(), + Format = "m.notice" + }); + msg = ""; + } + } + + // while (output.Count > 0) { + // Console.WriteLine("Adding: " + output[0]); + // msg += output[0] + "\n"; + // output.RemoveAt(0); + // if ((output.Count > 0 && (msg + output[0]).Length > 31500) || output.Count == 0) { + // await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventContent { + // FormattedBody = $"<pre class=\"language-csharp\">\n{msg}\n</pre>", + // // Body = Markdig.Markdown.ToHtml(msg), + // Body = msg.RemoveAnsi(), + // Format = "org.matrix.custom.html" + // }); + // msg = ""; + // } + // } + } +} \ No newline at end of file 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
@@ -0,0 +1,53 @@ +using System.Diagnostics; +using LibMatrix.EventTypes.Spec; +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<bool> CanInvoke(CommandContext ctx) => ctx.Room.RoomId == "!hLEefBaYvNfJwcTjmt:rory.gay"; + + public async Task Invoke(CommandContext ctx) { + //255 long string + // var rainbow = "🟥🟧🟨🟩🟦🟪"; + var rainbow = "M"; + var chars = rainbow; + for (var i = 0; i < 76; i++) chars += rainbow[i % rainbow.Length]; + + Task.Run(async () => { + var i = 0; + var msg = new MessageBuilder("m.notice").WithRainbowString(chars).Build(); + var msgEvent = await ctx.Room.SendMessageEventAsync(msg); + + while (true) { + msg = new MessageBuilder("m.notice").WithRainbowString(chars, offset: i * 5).Build(); + if (i % 50 == 0) { + msg.NewContent = null; + msg.RelatesTo = null; + msgEvent = await ctx.Room.SendMessageEventAsync(msg); + } + else { + msg = msg.SetReplaceRelation<RoomMessageEventContent>(msgEvent.EventId); + msg.Body = ""; + msg.FormattedBody = ""; + } + + var sw = Stopwatch.StartNew(); + await + ctx.Room.SendMessageEventAsync(msg); + await Task.Delay(sw.Elapsed); + i++; + } + }); + } +} \ No newline at end of file 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
@@ -0,0 +1,22 @@ +using System.Text; +using LibMatrix.EventTypes.Spec; +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) { + var sb = new StringBuilder(); + sb.AppendLine("Available commands:"); + var commands = services.GetServices<ICommand>().ToList(); + foreach (var command in commands) sb.AppendLine($"- {command.Name}: {command.Description}"); + + await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: sb.ToString())); + } +} \ No newline at end of file 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!"));