Advertisement
User9684

Untitled

Nov 21st, 2022 (edited)
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.73 KB | None | 0 0
  1. function string.starts(String, Start)
  2.     return string.sub(String, 1, string.len(Start)) == Start
  3. end
  4. function string.split(inputstr, sep)
  5.     if sep == nil then
  6.         sep = "%s"
  7.     end
  8.     local t = {}
  9.     for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
  10.         table.insert(t, str)
  11.     end
  12.     return t
  13. end
  14.  
  15. local chat = peripheral.wrap("left")
  16. local player = peripheral.wrap("right")
  17.  
  18. rednet.open("back")
  19.  
  20. local whitelist = {
  21.     ["User9684"] = true
  22. }
  23.  
  24. local whitelistedSenderIds = {
  25.     [2] = true
  26. }
  27.  
  28. local prefix = "%"
  29. local name = "robot"
  30.  
  31. local commands = {}
  32.  
  33. function commands.say(caller, args)
  34.     local includeName = (args[1] == "1")
  35.     table.remove(args, 1)
  36.  
  37.     local text = table.concat(args, " ")
  38.     if includeName then
  39.         text = caller .. ": " .. text
  40.     end
  41.     print(caller .. ": " .. table.concat(args, " "))
  42.     chat.sendMessage(text, name)
  43. end
  44.  
  45. function commands.rename(caller, args)
  46.     name = table.concat(args, " ")
  47. end
  48.  
  49. local function execCommand(caller, text, isRemote)
  50.     if whitelist[caller] or (isRemote and whitelistedSenderIds[caller]) then
  51.         for commandName, commandFunc in next, commands do
  52.             if string.starts(string.lower(text), prefix .. commandName) then
  53.                 local args = string.split(text, " ")
  54.                 table.remove(args, 1)
  55.                 commandFunc(caller, args)
  56.             end
  57.         end
  58.     end
  59. end
  60. while true do
  61.     local event, m, n, t = os.pullEvent()
  62.     local validEvent1 = (event == "rednet_message" and t == "computercmd")
  63.     local validEvent2 = (event == "chat")
  64.     if validEvent1 or validEvent2 then
  65.         print(event, m, n, t)
  66.         execCommand(m, n, (event == 'rednet_message'))
  67.     end
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement