From 82c8313cb99a61c0127b8428f4594e27055f03bd Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Tue, 18 Jan 2022 14:44:31 +0100 Subject: [PATCH] Added peripheral list --- MinecraftDiscordBot/ClientScript.lua | 16 ++++++++++++---- MinecraftDiscordBot/Program.cs | 4 ++++ .../Services/RootCommandService.cs | 7 ++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/MinecraftDiscordBot/ClientScript.lua b/MinecraftDiscordBot/ClientScript.lua index 13333fe..fa04108 100644 --- a/MinecraftDiscordBot/ClientScript.lua +++ b/MinecraftDiscordBot/ClientScript.lua @@ -57,6 +57,16 @@ local function runRsCommand(params) return textutils.serializeJSON(retvals) end +local function getPeripheralInfo(side) + return {type = peripheral.getType(side), methods = peripheral.getMethods(side), side = side} +end + +local function getPeripheralList() + local pers = {} + for i,side in pairs(peripheral.getNames()) do pers[side] = getPeripheralInfo(side) end + return pers +end + -- error: any error during execution -- return string result local function getResponse(parsed) @@ -76,6 +86,8 @@ local function getResponse(parsed) return textutils.serializeJSON(item) elseif parsed.method == "command" then return runRsCommand(parsed.params) + elseif parsed.method == "peripherals" then + return textutils.serializeJSON(getPeripheralList()) elseif parsed.method == "getonline" then return textutils.serializeJSON(getPeripheral("playerDetector").getOnlinePlayers()) elseif parsed.method == "whereis" then @@ -160,10 +172,6 @@ local function chatEventListener(socket) end end -local function getPeripheralInfo(side) - return {type = peripheral.getType(side), methods = peripheral.getMethods(side), side = side} -end - local function peripheralDetachEventListener(socket) while true do event, side = os.pullEvent("peripheral_detach") diff --git a/MinecraftDiscordBot/Program.cs b/MinecraftDiscordBot/Program.cs index 5286b5d..11930a7 100644 --- a/MinecraftDiscordBot/Program.cs +++ b/MinecraftDiscordBot/Program.cs @@ -60,6 +60,8 @@ public class Program : IDisposable, ICommandHandler, IUserRoleMana _computer = new(this); _computer.ChatMessageReceived += MinecraftMessageReceived; _computer.SocketChanged += ComputerConnectedChanged; + _computer.PeripheralAttached += PeripheralAttached; + _computer.PeripheralDetached += PeripheralDetached; _administrators = config.Administrators.ToHashSet(); ClientScript = GetClientScript(config); _client.Log += LogAsync; @@ -72,6 +74,8 @@ public class Program : IDisposable, ICommandHandler, IUserRoleMana _whitelistedChannels = config.Channels.ToHashSet(); } + private void PeripheralAttached(object? sender, PeripheralAttachEvent e) => LogInfo("Computer", $"Peripheral {e.Peripheral.Type} was attached on side {e.Side}."); + private void PeripheralDetached(object? sender, PeripheralDetachEvent e) => LogInfo("Computer", $"Peripheral on side {e.Side} was detached."); private void ComputerConnectedChanged(object? sender, IWebSocketConnection? e) => _ = Task.Run(() => Broadcast(i => i.SendMessageAsync(e is not null ? "The Minecraft client is now available!" diff --git a/MinecraftDiscordBot/Services/RootCommandService.cs b/MinecraftDiscordBot/Services/RootCommandService.cs index 0e6dd99..80e055d 100644 --- a/MinecraftDiscordBot/Services/RootCommandService.cs +++ b/MinecraftDiscordBot/Services/RootCommandService.cs @@ -3,6 +3,7 @@ using Fleck; using MinecraftDiscordBot.Commands; using MinecraftDiscordBot.Models; using Newtonsoft.Json; +using System.Linq; namespace MinecraftDiscordBot.Services; @@ -18,7 +19,7 @@ public class RootCommandService : CommandRouter, ITaskWaitSource { Chat = new ChatBoxService(this); } - public static async Task Method(ITaskWaitSource taskSource, string methodName, Func parser, CancellationToken ct, Dictionary? parameters) { + public static async Task Method(ITaskWaitSource taskSource, string methodName, Func parser, CancellationToken ct, Dictionary? parameters = null) { var waiter = taskSource.GetWaiter(parser, ct); await taskSource.Send(new RequestMessage(waiter.ID, methodName, parameters)); return await waiter.Task; @@ -97,9 +98,13 @@ public class RootCommandService : CommandRouter, ITaskWaitSource { return waiter; } + public Task> GetPeripherals(CancellationToken ct) => Method(this, "peripherals", Deserialize>(), ct); [CommandHandler("rs", HelpText = "Provides some commands for interacting with the Refined Storage system.")] public Task RefinedStorageHandler(SocketUserMessage message, string[] parameters, CancellationToken ct) => RefinedStorage.HandleCommand(message, parameters, ct); + [CommandHandler("peripherals", HelpText = "Gets a list of peripherals that are attached.")] + public async Task HandleGetPeripherals(SocketUserMessage message, string[] parameters, CancellationToken ct) + => ResponseType.AsString(string.Join("\n", (await GetPeripherals(ct)).Values.Select(i => $"On side {i.Side}: {i.Type}"))); [CommandHandler("pd", HelpText = "Provides some commands for interacting with the Player Detector.")] public Task PlayerDetectorHandler(SocketUserMessage message, string[] parameters, CancellationToken ct) => Players.HandleCommand(message, parameters, ct);