You do not need to use string.find, since it does not matter
where in the message the keyword is located, only that it
is in the keyrord.
Code:
if mas[msg] then
-- the string msg is a key in the table mas, eg. the message could be "!panda" or "!mop"
end
You could also add some "safety checks" like changing the message to all-lowercase, and removing leading and trailing spaces before looking it up in the table:
Code:
msg = msg:lower():trim()
if mas[msg] then
-- the string msg is a key in the table mas, eg. the message could be "!panda" or "!mop"
end
If you want to respond to messages that contain a keyword
and other text, you can use do this:
Code:
msg = msg:lower()
for keyword in pairs(mas) do
if msg:match(keyword) then
-- the message contains this keyword
break
end
end
Again, lowercasing the string is important, since users might type "!MOP" or "!MoP" or "!mOP" or "!mop" or any combination, and you don't want to have to list every possible combination in your table. Finally, after you find the keyword, you should
break out of the loop so you don't waste CPU time looking for a keyword after you already found one!
If you want to count how many keywords are in the message:
Code:
msg = msg:lower()
local count = 0
for keyword in pairs(mas) do
if message:match(keyword) then
count = count + 1
end
end
-- the message contains count keywords
If you only want to identify messages that
begin with a keyword:
Code:
msg = msg:lower():trim()
local firstWord = msg:match("^[^%s+]")
if mas[firstWord] then
-- the message starts with the keyword firstWord
end