Search code examples
stringluafindmatch

How to check if matching words is found in a string in Lua?


Using:

if string.find(message.content, "hi") then
  global:printError("[INFO] Someone sent you hi")
  
end  

Works good, however I'd like to give it a word list to actually trigger the print message.

EG: hello,salut,bonjour,hi,hola, And my message.content = hi comment va tu ? I can say hello, speak spanish hola If any of the words is found in the string trigger the message.

How do I achieve this ?


Solution

  • Make a list of greetings and iterate over the list directly. For example:

    local greetings = { "hello", "salut", "bonjour", "hola", "hi" }

    for _,v in pairs(greetings) do
    if v == message.content then
    -- do something
     break
     end
    end