Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Chat Assistant Node
- print("1lann's Chat Assistant Node")
- print("Please don't touch me :(")
- local chat = peripheral.wrap("right")
- local modem = peripheral.wrap("top")
- local operatingChannel = 27158
- local successChannel = 27159
- local lastMessage = os.clock() - 0.5
- local standardMessages = {}
- local successMessages = {}
- if not chat then
- print("Warning: Could not find chatbox!")
- end
- if modem then
- modem.closeAll()
- modem.open(operatingChannel)
- modem.open(successChannel)
- print("Modem ready")
- else
- error("Could not find modem!")
- end
- local queuedMessages = {}
- local function receiveLoop()
- while true do
- local event, side, channel, reply, message = os.pullEvent()
- if event == "modem_message" and channel == operatingChannel then
- if type(message) == "table" and message[1] == "chat-assistant-network" then
- if #tostring(textutils.serialize(message)) < 200 then
- if type(message[2]) == "string" and type(message[3]) == "string" and type(message[4]) == "string" then
- if (not standardMessages[message[2]]) or (standardMessages[message[2]] < os.clock()) then
- standardMessages[message[2]] = os.clock() + 10
- if chat and (os.clock() - lastMessage) >= 0.5 then
- lastMessage = os.clock()
- local result = nil
- local err, msg = pcall(function() result = chat.tell(message[3], message[4]) end)
- if not err then
- modem.transmit(operatingChannel, operatingChannel, message)
- table.insert(queuedMessages, {message[2], message[3], message[4]})
- elseif not result then
- modem.transmit(operatingChannel, operatingChannel, message)
- elseif result then
- modem.transmit(successChannel, successChannel, message[2])
- end
- elseif chat then
- modem.transmit(operatingChannel, operatingChannel, message)
- table.insert(queuedMessages, {message[2], message[3], message[4]})
- else
- modem.transmit(operatingChannel, operatingChannel, message)
- end
- end
- end
- end
- end
- elseif event == "modem_message" and channel == successChannel then
- if type(message) == "string" and #message < 50 then
- if (not successMessages[message]) or (successMessages[message] < os.clock()) then
- successMessages[message] = os.clock() + 10
- modem.transmit(successChannel, successChannel, message)
- end
- end
- end
- end
- end
- local function chatLoop()
- while true do
- sleep(0.3)
- if #queuedMessages > 0 and ((os.clock() - lastMessage) > 0.5) then
- local result = nil
- while not pcall(function() result = chat.tell(queuedMessages[1][2], queuedMessages[1][3]) end) do
- sleep(0.2)
- end
- if result then
- modem.transmit(successChannel, successChannel, queuedMessages[1][1])
- end
- table.remove(queuedMessages, 1)
- end
- end
- end
- parallel.waitForAny(receiveLoop, chatLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement