_DudeWhat_

Discord to MC

Jan 7th, 2022 (edited)
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.79 KB | None | 0 0
  1. local chat = peripheral.find("chatBox")
  2. if not chat then error("No chatbox attached!") end
  3.  
  4. print("Connecting to the socket server...")
  5. local socket, reason = http.websocket("ws://ws.cnml.de:8080")
  6. if not socket then error("Socket server could not be reached: "..reason) end
  7. print("Connection successful!")
  8.  
  9. local function handleMessage(message)
  10.     local parsed, reason = textutils.unserializeJSON(message)
  11.     if not parsed then print("Deserialization failed: "..reason..", in messsage "..message) return end
  12.    
  13.     if parsed.type == "text" then
  14.         chat.sendMessage(parsed.message, parsed.author)
  15.         return
  16.     end
  17.    
  18.     print("No handler for messages of type", parsed.type)
  19. end
  20.  
  21. while true do
  22.     message, binary = socket.receive(1)
  23.     if not not message and not binary then
  24.         handleMessage(message)
  25.     end
  26. end
Add Comment
Please, Sign In to add comment