From 6bd02248ccfbcb46960a6f39eaad23888d190eb5 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Fri, 15 Sep 2023 09:50:45 +0200 Subject: Some refactoring --- .../Bot/Commands/CmdCommand.cs | 60 ++++++++++++++++------ .../Bot/Commands/HelpCommand.cs | 2 +- .../Bot/Commands/PingCommand.cs | 2 +- 3 files changed, 46 insertions(+), 18 deletions(-) (limited to 'ExampleBots/LibMatrix.ExampleBot/Bot/Commands') diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs index efedbba..5b2828e 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs @@ -1,3 +1,4 @@ +using ArcaneLibs.Extensions; using LibMatrix.ExampleBot.Bot.Interfaces; using LibMatrix.StateEventTypes.Spec; @@ -8,7 +9,7 @@ public class CmdCommand : ICommand { public string Description => "Runs a command on the host system"; public Task CanInvoke(CommandContext ctx) { - return Task.FromResult(ctx.MessageEvent.Sender.EndsWith(":rory.gay")); + return Task.FromResult(ctx.MessageEvent.Sender.EndsWith(":rory.gay") || ctx.MessageEvent.Sender.EndsWith(":conduit.rory.gay")); } public async Task Invoke(CommandContext ctx) { @@ -17,28 +18,55 @@ public class CmdCommand : ICommand { cmd = cmd.Trim(); cmd += "\""; - await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData(body: $"Command being executed: `{cmd}`")); + await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventContent(body: $"Command being executed: `{cmd}`")); - var output = ArcaneLibs.Util.GetCommandOutputSync( - Environment.OSVersion.Platform == PlatformID.Unix ? "/bin/sh" : "cmd.exe", - (Environment.OSVersion.Platform == PlatformID.Unix ? "-c " : "/c ") + cmd) - .Replace("`", "\\`") - .Split("\n").ToList(); - foreach (var _out in output) Console.WriteLine($"{_out.Length:0000} {_out}"); + 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 = ""; - while (output.Count > 0) { - Console.WriteLine("Adding: " + output[0]); - msg += output[0] + "\n"; - output.RemoveAt(0); - if ((output.Count > 0 && (msg + output[0]).Length > 64000) || output.Count == 0) { - await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData { - FormattedBody = $"```ansi\n{msg}\n```", - // Body = Markdig.Markdown.ToHtml(msg), + EventIdResponse? msgId = await ctx.Room.SendMessageEventAsync("m.room.message", 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("m.room.message", 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 { + 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 = $"
\n{msg}\n
", + // // Body = Markdig.Markdown.ToHtml(msg), + // Body = msg.RemoveAnsi(), + // Format = "org.matrix.custom.html" + // }); + // msg = ""; + // } + // } } } diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/HelpCommand.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/HelpCommand.cs index 09c4e3f..c750130 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/HelpCommand.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/HelpCommand.cs @@ -17,6 +17,6 @@ public class HelpCommand(IServiceProvider services) : ICommand { sb.AppendLine($"- {command.Name}: {command.Description}"); } - await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData(body: sb.ToString())); + await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventContent(body: sb.ToString())); } } diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/PingCommand.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/PingCommand.cs index f70cd78..a261a59 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/PingCommand.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/PingCommand.cs @@ -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 RoomMessageEventData(body: "pong!")); + await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventContent(body: "pong!")); } } -- cgit 1.4.1