Advertisement
hbar

Untitled

Jun 7th, 2013
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.85 KB | None | 0 0
  1. local myname = "Jarvis"
  2. local delimiters = {",", ":", " "}
  3.  
  4. local getFromChat = function()
  5.     --wait for a message in the chat
  6.     while true do
  7.         -- a chat message triggers an event that we capture
  8.         local event, player, message = os.pullEvent("chat")
  9.         --[[ ---------------------
  10.             the following line detects the bots name
  11.            
  12.             message:sub(1,6) is the first 6 letters of the message, that's where we
  13.             expect the name to be. You can of course check the whole line by removing the
  14.             :sub(1,6)-part
  15.            
  16.             message:sub(1,6):lower() makes the first 6 letters lower case so we don't have to
  17.             worry if users type "jarvis" or "Jarvis"
  18.            
  19.             message:sub(1,6):lower():find("something") checks if there is a string "something"
  20.             in the first 6 letters of the message (that is converted to lower case). Because
  21.             we wand to find the name of the bot we use myname:lower() in place of "something".
  22.             (remember we converted the message to lower case so we should also convert our name)
  23.  
  24.             the line message:sub(1,6):lower():find(myname:lower()) equals a number if myname is in
  25.             the message and nil otherwise
  26.    
  27.             we could also write
  28.                 if message:sub(1,6):lower() == myname:lower() then
  29.             because we just check the first 6 letters anyway
  30.            
  31.         --]]----------------------
  32.         if message:sub(1,6):lower():find(myname:lower()) then
  33.             -- we set the index to the length of the message
  34.             local min_index = #message
  35.             -- and then try to find the delimiter (, : or " ") closest to the beginning of
  36.             -- the message
  37.             for a,del in ipairs(delimiters) do
  38.                 local i = message:find(del)
  39.                 if i and i < min_index then min_index = i end
  40.             end
  41.             -- if there is a delimiter before the end of the message we take everything after
  42.             -- that and send it to cleverbot
  43.             if min_index < #message then
  44.                 return message:sub(min_index+1)
  45.             end
  46.         end
  47.     end
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement