Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Chat Server with MiniChat support
- local modem = peripheral.find("modem")
- if not modem then
- print("Ender Modem not found!")
- return
- end
- local defaultChannel = 1
- modem.open(defaultChannel)
- -- Table to store chat logs and users for each MiniChat channel
- local channels = {}
- -- Function to add a user to a channel
- function addUserToChannel(channel, username)
- if not channels[channel] then
- -- Create a new channel with the user as owner
- channels[channel] = {
- owner = username,
- users = {},
- messages = {}
- }
- end
- -- Add the user to the channel if they are not already in
- if not channels[channel].users[username] then
- channels[channel].users[username] = {
- role = "normal" -- normal, muted, banned, etc.
- }
- end
- end
- -- Function to handle new messages
- function handleChatMessage(channel, username, message)
- -- Make sure the user is in the channel and not banned
- if channels[channel] and channels[channel].users[username] then
- -- Store the message in the channel history
- local formattedMessage = "[" .. username .. "]: " .. message
- table.insert(channels[channel].messages, formattedMessage)
- -- Broadcast the message to all users in the channel
- for user, _ in pairs(channels[channel].users) do
- modem.transmit(defaultChannel, defaultChannel, {type="message", channel=channel, data=formattedMessage})
- end
- end
- end
- -- Function to handle incoming requests
- function handleRequest(event, side, freq, replyFreq, message)
- if message.type == "join" then
- addUserToChannel(message.channel, message.username)
- modem.transmit(defaultChannel, replyFreq, {type="status", data="Joined channel: " .. message.channel})
- elseif message.type == "message" then
- handleChatMessage(message.channel, message.username, message.message)
- end
- end
- -- Main loop to handle incoming messages
- print("Server is running...")
- while true do
- local event, side, freq, replyFreq, message, dist = os.pullEvent("modem_message")
- if freq == defaultChannel and type(message) == "table" then
- handleRequest(event, side, freq, replyFreq, message)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement