Advertisement
yohanbohan

ws with commands

Jun 26th, 2025 (edited)
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.78 KB | None | 0 0
  1. local myURL = "promoted-goshawk-publicly.ngrok-free.app"
  2. local file = fs.open("apps/token", "r")
  3. local token = file.readAll()
  4. file.close()
  5.  
  6. local ws = assert(http.websocket("wss://" .. myURL .. "?token=" .. token))
  7.  
  8. function inspect()
  9.     local below, result = turtle.inspectDown()
  10.     if below then
  11.         ws.send(textutils.serialiseJSON(result))
  12.     end
  13.  
  14.     local up, result = turtle.inspectUp()
  15.     if up then
  16.         ws.send(textutils.serialiseJSON(result))
  17.     end
  18.  
  19.     local front, result = turtle.inspect()
  20.     if front then
  21.         ws.send(textutils.serialiseJSON(result))
  22.     end
  23. end
  24.  
  25. function forward()
  26.     turtle.forward()
  27.     inspect()
  28. end
  29.  
  30. function up()
  31.     turtle.up()
  32.     inspect()
  33. end
  34.  
  35. function back()
  36.     turtle.back()
  37.     inspect()
  38. end
  39.  
  40. function down()
  41.     turtle.down()
  42.     inspect()
  43. end
  44.  
  45. function right()
  46.     turtle.turnRight()
  47.     inspect()
  48. end
  49.  
  50. function left()
  51.     turtle.turnLeft()
  52.     inspect()
  53. end
  54.  
  55. function fuel()
  56.     local fuel = turtle.getFuelLevel()
  57.     if fuel == 0 then
  58.         turtle.refuel()
  59.     end
  60. end
  61.  
  62. function mine()
  63.     local broken, why = turtle.dig('right')
  64.     if broken == false then
  65.         ws.send(textutils.serialise(why))
  66.     end
  67. end
  68.  
  69. function mineUp()
  70.     local broken, why = turtle.digUp('right')
  71.     if broken == false then
  72.         ws.send(textutils.serialise(why))
  73.     end
  74. end
  75.  
  76. function mineDown()
  77.     local broken, why = turtle.digDown('right')
  78.     if broken == false then
  79.         ws.send(textutils.serialise(why))
  80.     end
  81. end
  82.  
  83. function place()
  84.     local placed, why = turtle.place('right')
  85.     if placed == false then
  86.         ws.send(textutils.serialise(why))
  87.     end
  88. end
  89.  
  90. function placeUp()
  91.     local placed, why = turtle.placeUp('right')
  92.     if placed == false then
  93.         ws.send(textutils.serialise(why))
  94.     end
  95. end
  96.  
  97. function placeDown()
  98.     local placed, why = turtle.placeDown('right')
  99.     if placed == false then
  100.         ws.send(textutils.serialise(why))
  101.     end
  102. end
  103.  
  104. while true do
  105.     fuel()
  106.     message = ws.receive()
  107.     print("Received: " .. message)
  108.     if message == 'back' then
  109.         back()
  110.     end
  111.     if message == 'forward' then
  112.         forward()
  113.     end
  114.     if message == 'up' then
  115.         up()
  116.     end
  117.     if message == 'down' then
  118.         down()
  119.     end
  120.     if message == 'left' then
  121.         left()
  122.     end
  123.     if message == 'right' then
  124.         right()
  125.     end
  126.     if message == 'mine' then
  127.         mine()
  128.     end
  129.     if message == 'mineUp' then
  130.         mineUp()
  131.     end
  132.     if message == 'mineDown' then
  133.         mineDown()
  134.     end
  135.     if message == 'place' then
  136.         place()
  137.     end
  138.     if message == 'placeUp' then
  139.         placeUp()
  140.     end
  141.     if message == 'placeDown' then
  142.         placeDown()
  143.     end
  144. end
  145.  
  146.  
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement