# Censoring words in lua

Im currently making a discord censor bot for my friend, and I was wondering if anyone with experience could help me, or if theres an easier way to complete my code.

local BadWords = {
['poop']=true,
}

client:on('messageCreate', function(msg)
Msg = msg.content
local msgs = string.split(Msg,' ')
for i,v in pairs(msgs) do
embed = {
fields = {
{name = "Bad Word",value=v,inline = true},
},
color = discordia.Color.fromRGB(114, 137, 218).value,

--footer = os.time(),
}
})
end
end
--[[table.foreach(msgs,function(i,v)
end
end)--]]
end)
client:run(Settings.Token)


There's nothing wrong with it, nor is it complete but my idea was to split, or concatenate all the words they send respectively, mutate into all versions of the word (i.e. farm, f4rm, type shit) and then checking the BadWord list.

Mostly what I was wondering was if there's a more efficient way to do this, or if I just gotta do what I gotta do

• This won't catch words like poop, and several others. – Roland Illig Jan 9 '20 at 16:25
• It works, I dont know what you're talking about, im j wondering if theres a efficient way to parse a sentence and check for bad words, and then mutated bad words aswell. im not that advanced in string manipulation, or regex. – Albedo Jan 9 '20 at 18:42
• Sorry, it was a bit hard to understand. What I really mean was: If you let your code analyze my first comment, it won't flag the word poop as a bad word since it is followed by a comma. This is because you are splitting the string at spaces and you further assume that everything between these spaces is a word. – Roland Illig Jan 10 '20 at 0:45

If you care about performance, you should have a look at LPeg. It makes it way easier to handle large numbers of string matching and substitution rules.

For example, you could define patterns and their substitution values like this:

local lpeg = require 'lpeg'

local p = lpeg.P