Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to list all connected modems
- local function listModems()
- local modems = {}
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- table.insert(modems, side)
- end
- end
- return modems
- end
- -- Function to setup the modem
- local function setupModem()
- local modems = listModems()
- if #modems == 0 then
- print("No modems found! Please connect a modem and restart.")
- return nil
- end
- print("Connected modems:")
- for i, side in ipairs(modems) do
- print(i .. ": " .. side)
- end
- -- Open the first modem found
- rednet.open(modems[1])
- print("Using modem on " .. modems[1])
- return modems[1]
- end
- -- Function to display a message in the chat
- local function displayMessage(senderId, message)
- term.setTextColor(colors.yellow) -- Set text color
- print("[" .. senderId .. "]: " .. message)
- term.setTextColor(colors.white) -- Reset text color
- end
- -- Function to handle incoming messages
- local function handleIncomingMessages()
- while true do
- local senderId, message, protocol = rednet.receive()
- displayMessage(senderId, message)
- end
- end
- -- Function to send a message to all connected Rednet users
- local function sendMessage()
- while true do
- print("Type your message (or 'exit' to quit): ")
- local userInput = read() -- Get the user's input
- if userInput == "exit" then
- print("Exiting chat...")
- break
- end
- -- Broadcast the message to all users
- rednet.broadcast(userInput)
- displayMessage("You", userInput)
- end
- end
- -- Main program
- term.clear() -- Clear the terminal
- term.setCursorPos(1, 1) -- Set cursor position
- print("Welcome to the Enhanced Rednet Chat Room!")
- local modemSide = setupModem()
- if modemSide == nil then return end -- Exit if no modem was found
- print("Type 'exit' to leave the chat.")
- -- Start monitoring
- parallel.waitForAny(handleIncomingMessages, sendMessage)
Add Comment
Please, Sign In to add comment