Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- DayBot Turtle Script v1.0
- Listens for chat commands to change the time to day using a Weak Automata peripheral.
- Inspired by the provided screenshot and jokebot structure.
- Commands:
- - @day: Executes the sequence to make it daytime.
- - @all: Explains what the @day command does.
- ]]
- --#region Configuration
- -- The name of the chat box peripheral. This bot needs a Neural Interface and Chat Module to work.
- local CHAT_BOX_PERIPHERAL_NAME = "chatBox"
- -- The name of the peripheral that changes the time.
- local AUTOMATA_PERIPHERAL_NAME = "weakAutomata"
- -- The name displayed in chat for the bot.
- local CHAT_BOT_NAME = "DayBot"
- local CHAT_BOT_BRACKETS = "[]"
- local CHAT_BOT_BRACKET_COLOR = "&e" -- Yellow color for the brackets
- --#endregion
- --#region Peripherals
- -- Find the chat peripheral at the start.
- local chatBox_ok, chatBox = pcall(peripheral.find, CHAT_BOX_PERIPHERAL_NAME)
- if not chatBox_ok or not chatBox then
- print("WARNING: Chat Box peripheral ('" .. CHAT_BOX_PERIPHERAL_NAME .. "') not found!")
- print("Bot will only print messages to its own console.")
- chatBox = nil -- Ensure chatBox is nil if not found
- end
- --#endregion
- --#region Minecraft JSON Text Component Colors (for formatted chat)
- local COLORS = {
- GREEN = "green", AQUA = "aqua", RED = "red",
- YELLOW = "yellow", GRAY = "gray"
- }
- --#endregion
- --#region Helper Functions
- -- A function to send formatted messages to the chatBox, with a fallback to the local console.
- local function announce(messageComponents)
- -- Fallback to printing on the local terminal if no chatBox is found
- if not chatBox then
- local plainText = ""
- for _, comp in ipairs(messageComponents) do plainText = plainText .. (comp.text or "") end
- print("[" .. CHAT_BOT_NAME .. "] " .. plainText)
- return
- end
- -- Attempt to send a rich, formatted message
- local jsonMessage = textutils.serialiseJSON(messageComponents)
- if not jsonMessage then
- chatBox.sendMessage("Error: Could not serialize message.", CHAT_BOT_NAME, CHAT_BOT_BRACKETS, CHAT_BOT_BRACKET_COLOR)
- return
- end
- -- Use pcall to safely call the peripheral method
- pcall(chatBox.sendFormattedMessage, jsonMessage, CHAT_BOT_NAME, CHAT_BOT_BRACKETS, CHAT_BOT_BRACKET_COLOR)
- end
- -- The core function to execute the day-making sequence based on the screenshot.
- local function makeDay(requestingUsername)
- announce({{text = "Request from " .. requestingUsername .. " received. Attempting to make it day...", color = COLORS.GRAY}})
- os.sleep(0.5)
- local automata = peripheral.find(AUTOMATA_PERIPHERAL_NAME)
- if not automata then
- announce({{text = "ERROR: ", color = COLORS.RED}, {text = "Peripheral '" .. AUTOMATA_PERIPHERAL_NAME .. "' not found.", color = COLORS.YELLOW}})
- return
- end
- local success, result
- -- Step 1: Select slot 1
- if not turtle.select(1) then
- announce({{text = "ERROR: ", color = COLORS.RED}, {text = "Failed to select slot 1.", color = COLORS.YELLOW}})
- return
- end
- -- Step 2: Use item in slot 1 on a block
- success, result = pcall(automata.useOnBlock)
- if not success or result ~= true then
- announce({{text = "ERROR: ", color = COLORS.RED}, {text = "Failed to use item from slot 1. Reason: " .. tostring(result), color = COLORS.YELLOW}})
- return
- end
- -- Step 3: Select slot 2
- if not turtle.select(2) then
- announce({{text = "ERROR: ", color = COLORS.RED}, {text = "Failed to select slot 2.", color = COLORS.YELLOW}})
- return
- end
- -- Step 4: Use item in slot 2 on a block
- success, result = pcall(automata.useOnBlock)
- if not success or result ~= true then
- return
- end
- announce({{text = "Success! The ritual is complete. It should now be day.", color = COLORS.GREEN}})
- end
- --#endregion
- --#region Main Loop
- local function run()
- term.clear()
- term.setCursorPos(1, 1)
- print(CHAT_BOT_NAME .. " script started.")
- print("Listening for chat commands: @day, @all")
- -- Announce online status in chat
- announce({{text = CHAT_BOT_NAME .. " is now online!", color = COLORS.GREEN, bold = true}})
- announce({{text = "Type '", color = COLORS.GRAY}, {text = "@all", color = COLORS.AQUA}, {text = "' for info.", color = COLORS.GRAY}})
- while true do
- local eventData = {os.pullEvent()}
- local event = eventData[1]
- if event == "chat" then
- local username, message = eventData[2], eventData[3]
- -- Make the command case-insensitive by converting to lowercase
- local command = string.lower(message)
- if command == "@all" then
- -- Handle the @all command to explain the bot's function
- announce({
- {text = "Hello, " .. username .. "! This is " .. CHAT_BOT_NAME .. ". Use '", color = COLORS.GREEN},
- {text = "@day", color = COLORS.AQUA, bold = true},
- {text = "' to change the time to day.", color = COLORS.GREEN}
- })
- elseif command == "@day" then
- -- Handle the @day command to run the sequence
- makeDay(username)
- end
- elseif event == "terminate" then
- -- Announce shutdown if the script is terminated (Ctrl+T)
- announce({{text = CHAT_BOT_NAME .. " shutting down...", color = COLORS.YELLOW}})
- print(CHAT_BOT_NAME .. " terminated.")
- return -- Exit the loop and end the script
- end
- end
- end
- -- Execute the main loop, with error handling to catch any crashes
- local ok, err = pcall(run)
- if not ok then
- print("A critical error occurred: " .. tostring(err))
- if chatBox then
- announce({{text = CHAT_BOT_NAME .. " has crashed! Error: " .. tostring(err), color = COLORS.RED, bold = true}})
- end
- end
- --#endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement