DorianH935

Untitled

Jul 13th, 2024 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. local cb = peripheral.find("chatBox")
  2. local srn = peripheral.wrap("top")
  3.  
  4. local logFile = fs.open("log.txt", "a")
  5.  
  6. local lines = {}
  7.  
  8. while true do
  9. local event, username, message, uuid, isHidden = os.pullEvent("chat")
  10.  
  11. -- Get the current Unix time
  12. local timestamp = os.epoch("utc")
  13.  
  14. -- Format the chat message with timestamp for logging
  15. local logMessage = os.date("%Y-%m-%d %H:%M:%S", timestamp) .. " - " .. username .. ": " .. message
  16. -- Format the chat message without timestamp for display
  17. local displayMessage = username .. ": " .. message
  18.  
  19. -- Log the chat to a file
  20. logFile.writeLine(logMessage)
  21. logFile.flush()
  22.  
  23. -- Update the lines table with the new message for display
  24. table.insert(lines, displayMessage)
  25. if #lines > 15 then
  26. table.remove(lines, 1)
  27. end
  28.  
  29. -- Clear the screen
  30. srn.clear()
  31.  
  32. -- Display the chat on the monitor
  33. srn.setCursorPos(1, 1)
  34. for i = 1, #lines do
  35. srn.write(lines[i])
  36. srn.setCursorPos(1, i + 1)
  37. end
  38. end
  39.  
  40. logFile.close()
  41.  
Advertisement
Add Comment
Please, Sign In to add comment