Advertisement
Myros27

dayBot

Jun 10th, 2025 (edited)
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.94 KB | None | 0 0
  1. --[[
  2.     DayBot Turtle Script v1.0
  3.     Listens for chat commands to change the time to day using a Weak Automata peripheral.
  4.    
  5.     Inspired by the provided screenshot and jokebot structure.
  6.  
  7.     Commands:
  8.     - @day: Executes the sequence to make it daytime.
  9.     - @all: Explains what the @day command does.
  10. ]]
  11.  
  12. --#region Configuration
  13. -- The name of the chat box peripheral. This bot needs a Neural Interface and Chat Module to work.
  14. local CHAT_BOX_PERIPHERAL_NAME = "chatBox"
  15. -- The name of the peripheral that changes the time.
  16. local AUTOMATA_PERIPHERAL_NAME = "weakAutomata"
  17.  
  18. -- The name displayed in chat for the bot.
  19. local CHAT_BOT_NAME = "DayBot"
  20. local CHAT_BOT_BRACKETS = "[]"
  21. local CHAT_BOT_BRACKET_COLOR = "&e" -- Yellow color for the brackets
  22. --#endregion
  23.  
  24. --#region Peripherals
  25. -- Find the chat peripheral at the start.
  26. local chatBox_ok, chatBox = pcall(peripheral.find, CHAT_BOX_PERIPHERAL_NAME)
  27. if not chatBox_ok or not chatBox then
  28.     print("WARNING: Chat Box peripheral ('" .. CHAT_BOX_PERIPHERAL_NAME .. "') not found!")
  29.     print("Bot will only print messages to its own console.")
  30.     chatBox = nil -- Ensure chatBox is nil if not found
  31. end
  32. --#endregion
  33.  
  34. --#region Minecraft JSON Text Component Colors (for formatted chat)
  35. local COLORS = {
  36.     GREEN = "green", AQUA = "aqua", RED = "red",
  37.     YELLOW = "yellow", GRAY = "gray"
  38. }
  39. --#endregion
  40.  
  41. --#region Helper Functions
  42. -- A function to send formatted messages to the chatBox, with a fallback to the local console.
  43. local function announce(messageComponents)
  44.     -- Fallback to printing on the local terminal if no chatBox is found
  45.     if not chatBox then
  46.         local plainText = ""
  47.         for _, comp in ipairs(messageComponents) do plainText = plainText .. (comp.text or "") end
  48.         print("[" .. CHAT_BOT_NAME .. "] " .. plainText)
  49.         return
  50.     end
  51.  
  52.     -- Attempt to send a rich, formatted message
  53.     local jsonMessage = textutils.serialiseJSON(messageComponents)
  54.     if not jsonMessage then
  55.         chatBox.sendMessage("Error: Could not serialize message.", CHAT_BOT_NAME, CHAT_BOT_BRACKETS, CHAT_BOT_BRACKET_COLOR)
  56.         return
  57.     end
  58.  
  59.     -- Use pcall to safely call the peripheral method
  60.     pcall(chatBox.sendFormattedMessage, jsonMessage, CHAT_BOT_NAME, CHAT_BOT_BRACKETS, CHAT_BOT_BRACKET_COLOR)
  61. end
  62.  
  63. -- The core function to execute the day-making sequence based on the screenshot.
  64. local function makeDay(requestingUsername)
  65.     announce({{text = "Request from " .. requestingUsername .. " received. Attempting to make it day...", color = COLORS.GRAY}})
  66.     os.sleep(0.5)
  67.  
  68.     local automata = peripheral.find(AUTOMATA_PERIPHERAL_NAME)
  69.     if not automata then
  70.         announce({{text = "ERROR: ", color = COLORS.RED}, {text = "Peripheral '" .. AUTOMATA_PERIPHERAL_NAME .. "' not found.", color = COLORS.YELLOW}})
  71.         return
  72.     end
  73.  
  74.     local success, result
  75.  
  76.     -- Step 1: Select slot 1
  77.     if not turtle.select(1) then
  78.         announce({{text = "ERROR: ", color = COLORS.RED}, {text = "Failed to select slot 1.", color = COLORS.YELLOW}})
  79.         return
  80.     end
  81.    
  82.     -- Step 2: Use item in slot 1 on a block
  83.     success, result = pcall(automata.useOnBlock)
  84.     if not success or result ~= true then
  85.         announce({{text = "ERROR: ", color = COLORS.RED}, {text = "Failed to use item from slot 1. Reason: " .. tostring(result), color = COLORS.YELLOW}})
  86.         return
  87.     end
  88.  
  89.     -- Step 3: Select slot 2
  90.     if not turtle.select(2) then
  91.         announce({{text = "ERROR: ", color = COLORS.RED}, {text = "Failed to select slot 2.", color = COLORS.YELLOW}})
  92.         return
  93.     end
  94.    
  95.     -- Step 4: Use item in slot 2 on a block
  96.     success, result = pcall(automata.useOnBlock)
  97.     if not success or result ~= true then
  98.         return
  99.     end
  100.  
  101.     announce({{text = "Success! The ritual is complete. It should now be day.", color = COLORS.GREEN}})
  102. end
  103. --#endregion
  104.  
  105. --#region Main Loop
  106. local function run()
  107.     term.clear()
  108.     term.setCursorPos(1, 1)
  109.     print(CHAT_BOT_NAME .. " script started.")
  110.     print("Listening for chat commands: @day, @all")
  111.  
  112.     -- Announce online status in chat
  113.     announce({{text = CHAT_BOT_NAME .. " is now online!", color = COLORS.GREEN, bold = true}})
  114.     announce({{text = "Type '", color = COLORS.GRAY}, {text = "@all", color = COLORS.AQUA}, {text = "' for info.", color = COLORS.GRAY}})
  115.  
  116.     while true do
  117.         local eventData = {os.pullEvent()}
  118.         local event = eventData[1]
  119.        
  120.         if event == "chat" then
  121.             local username, message = eventData[2], eventData[3]
  122.             -- Make the command case-insensitive by converting to lowercase
  123.             local command = string.lower(message)
  124.  
  125.             if command == "@all" then
  126.                 -- Handle the @all command to explain the bot's function
  127.                 announce({
  128.                     {text = "Hello, " .. username .. "! This is " .. CHAT_BOT_NAME .. ". Use '", color = COLORS.GREEN},
  129.                     {text = "@day", color = COLORS.AQUA, bold = true},
  130.                     {text = "' to change the time to day.", color = COLORS.GREEN}
  131.                 })
  132.  
  133.             elseif command == "@day" then
  134.                 -- Handle the @day command to run the sequence
  135.                 makeDay(username)
  136.             end
  137.        
  138.         elseif event == "terminate" then
  139.             -- Announce shutdown if the script is terminated (Ctrl+T)
  140.             announce({{text = CHAT_BOT_NAME .. " shutting down...", color = COLORS.YELLOW}})
  141.             print(CHAT_BOT_NAME .. " terminated.")
  142.             return -- Exit the loop and end the script
  143.         end
  144.     end
  145. end
  146.  
  147. -- Execute the main loop, with error handling to catch any crashes
  148. local ok, err = pcall(run)
  149. if not ok then
  150.     print("A critical error occurred: " .. tostring(err))
  151.     if chatBox then
  152.         announce({{text = CHAT_BOT_NAME .. " has crashed! Error: " .. tostring(err), color = COLORS.RED, bold = true}})
  153.     end
  154. end
  155. --#endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement