Reputation: 1
I have a discord bot: https://github.com/LebryantJohnson/ultimatebot. It has a censor that works great except for one shortcoming. In the banned words.txt there are my banned words. I don't want to curse so let's say one of the words is Chicken. If I were to Spam Chicken without spacing it, the anti swear won't remove it. Is there a way I can get it to remove the message even if a swear word was used?
Discord JS V 11 btw
Upvotes: 0
Views: 561
Reputation: 4520
As mentioned by @Sfue in the comments, you can use Regex to achieve this. In fact, regex could be used to solve both this issue and the issue I helped you with earlier at once. Here's how it would go:
checkProfanity: function(message, bannedWords) {
var words = message.split(' ');
for (var word of words) {
if (bannedWords.some(element => word.match(new RegExp(element, "i")) && element != "")) return true;
}
return false;
}
This uses a combination of a few things that weren't in your code before. First off is Array.some(), which is used to check if at least one element in an array passes the test specified by the supplied function. This is very useful in your situation, since you have an array of banned words and you want to check to see if any one of those banned words is present in your message. Second is RegExp, which is used for matching text with a pattern. We are using one RegExp
flag here: i
(meaning case insensitive). This flag is crucial to solving your earlier issue. Lastly, we pull together our use of all of the components mentioned above with String.match(), which is used to retrieve the result of attempting to match a String to a Regex pattern. Match()
paired with our Regex will return a value if any banned word appears in one of the message's words, and in a case insensitive manner due to our i
flag, solving both issues at once.
Upvotes: 1