Advertisement
TheRockettek

ChatBot

Feb 9th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. local chatBox = peripheral.find("chatbox_admin") or peripheral.find("chatbox")
  2.  
  3. function chatBoxSay(message)
  4. if not message then return false end
  5. message = string.gsub(message,"&s","\194\167")
  6. chatBox.say(message)
  7. return true
  8. end
  9.  
  10. function chatBoxTell(user,message)
  11. if not message or user then return false end
  12. message = string.gsub(message,"&s","\194\167")
  13. chatBox.tell(user,message)
  14. return true
  15. end
  16.  
  17. function chatBoxSeeLabel()
  18. return chatBox.getLabel()
  19. end
  20.  
  21. function chatBoxSetLabel(label)
  22. if not label then return false end
  23. chatBox.setLabel(label)
  24. return true
  25. end
  26.  
  27. function log(file,text)
  28. if not logHandle then logHandle = fs.open("chatLog","a") end
  29. logHandle.writeLine(text)
  30. logHandle.flush()
  31. end
  32.  
  33. function getArgs()
  34. if #cmdArgs == 0 then return {} end
  35. local arguments = {}
  36. for i=2,#cmdArgs,1 do
  37. table.insert(arguments,cmdArgs[i])
  38. end
  39. return arguments
  40. end
  41.  
  42. function addHelp(command,description)
  43. if not description then discription = nil end
  44. if not command then return end
  45. helpList[command] = description
  46. return true
  47. end
  48.  
  49. function removeHelp(command)
  50. if not helpList[command] then return false end
  51. table.remove(helpList,command)
  52. return true
  53. end
  54.  
  55. config = {}
  56. config.label = "Rocky"
  57. config.scriptRoot = "/cbscripts/"
  58. config.useGlobal = true
  59. config.logFile = "chat.log"
  60. config.logChat = false
  61. helpList = {}
  62.  
  63. chatBoxSetLabel(config.label)
  64.  
  65. addHelp("help","Shows what other scripts have added to help you")
  66. addHelp("scripts","Shows all avaliable scripts")
  67. chatBoxSay("Im ready :D")
  68. while true do
  69. local _,_,username,message = os.pullEvent("chat_message")
  70. cmdArgs = {}
  71. for arg in message:gmatch("%w+") do table.insert(cmdArgs,arg) end
  72. cmdTxt = {}
  73. for i=1,#message,1 do table.insert(cmdTxt,string.sub(message,i,i)) end
  74. print(username .. ": " .. table.concat(cmdArgs,","))
  75. if config.logChat then
  76. log(config.logFile,"[" .. username .. "] " .. message)
  77. end
  78. if not config.useGlobal then
  79. local say = chatBoxSay
  80. function chatBoxSay(message)
  81. return chatBoxTell(username,message)
  82. end
  83. end
  84. if cmdTxt[1] == "!" then
  85. log(config.logFile,"[Execute] " .. username .. ": " .. table.concat(cmdArgs,","))
  86. if cmdArgs[1] == "scripts" then
  87. chatBoxSay("Scripts (" .. #fs.list(config.scriptRoot) .."): " ..table.concat(fs.list(config.scriptRoot),","))
  88. elseif cmdArgs[1] == "help" then
  89. chatBoxSay("Help:")
  90. for i,k in pairs(helpList) do
  91. chatBoxSay("!" .. i .. ": " .. k)
  92. end
  93. elseif fs.exists(fs.combine(config.scriptRoot,cmdArgs[1])) then
  94. shell.run(fs.combine(config.scriptRoot,cmdArgs[1]))
  95. else
  96. chatBoxSay("Unknown command. Type \"!help\" for help.")
  97. end
  98. end
  99. if not config.useGlobal then
  100. chatBoxSay = say
  101. end
  102. sleep(0)
  103. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement