LK005

Chat listner

Jun 26th, 2022 (edited)
729
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local tArgs = { ... }
  2. local name = tArgs[1]
  3. local outputSide = tArgs[2]
  4. local inverted = tArgs[3]
  5. local CB
  6.  
  7. function splitString(str)
  8.   local fields = {}
  9.   str:gsub("([^ ]+)", function(c) table.insert(fields, c) end)
  10.   return fields
  11. end
  12.  
  13. function init()
  14.   CB = peripheral.find("chatBox")
  15.   if CB == nil then error("chatBox not found") end
  16. end
  17.  
  18. function mainLoop()
  19.   while true do
  20.     event, username, message = os.pullEvent("chat")
  21.  
  22.     if username == "_LK005_" or username == "MoospeSan" then
  23.       splitMessage = splitString(message)
  24.       local identifier = splitMessage[1]
  25.       local command = splitMessage[2]
  26.       local switchName = splitMessage[3]
  27.       local state = splitMessage[4]
  28.  
  29.       if identifier == "-" then
  30.         if switchName == name then
  31.           print("Command: "..command)
  32.           if command == "switch" then
  33.             print("Setting "..switchName.." to "..state)
  34.             if state == "on" then
  35.               if inverted == "y" then
  36.                 redstone.setOutput(outputSide, false)
  37.               else
  38.                 redstone.setOutput(outputSide, true)
  39.               end
  40.             elseif state == "off" then
  41.               if inverted == "y" then
  42.                 redstone.setOutput(outputSide, true)
  43.               else
  44.                 redstone.setOutput(outputSide, false)
  45.               end
  46.             end
  47.           end
  48.         end
  49.       end
  50.     end
  51.   end
  52. end
  53.  
  54. init()
  55. mainLoop()
RAW Paste Data Copied