Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local myname = "Jarvis"
- local delimiters = {",", ":", " "}
- local getFromChat = function()
- --wait for a message in the chat
- while true do
- -- a chat message triggers an event that we capture
- local event, player, message = os.pullEvent("chat")
- --[[ ---------------------
- the following line detects the bots name
- message:sub(1,6) is the first 6 letters of the message, that's where we
- expect the name to be. You can of course check the whole line by removing the
- :sub(1,6)-part
- message:sub(1,6):lower() makes the first 6 letters lower case so we don't have to
- worry if users type "jarvis" or "Jarvis"
- message:sub(1,6):lower():find("something") checks if there is a string "something"
- in the first 6 letters of the message (that is converted to lower case). Because
- we wand to find the name of the bot we use myname:lower() in place of "something".
- (remember we converted the message to lower case so we should also convert our name)
- the line message:sub(1,6):lower():find(myname:lower()) equals a number if myname is in
- the message and nil otherwise
- we could also write
- if message:sub(1,6):lower() == myname:lower() then
- because we just check the first 6 letters anyway
- --]]----------------------
- if message:sub(1,6):lower():find(myname:lower()) then
- -- we set the index to the length of the message
- local min_index = #message
- -- and then try to find the delimiter (, : or " ") closest to the beginning of
- -- the message
- for a,del in ipairs(delimiters) do
- local i = message:find(del)
- if i and i < min_index then min_index = i end
- end
- -- if there is a delimiter before the end of the message we take everything after
- -- that and send it to cleverbot
- if min_index < #message then
- return message:sub(min_index+1)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement