in my bot I have implemented a keyword filter that the bot reviews in each message that is written in the chat, until now it works, but I want to improve it, for reasons of respect I will not put words here, so I will put some others example,
The bot detects if you write for example "vulgar", "badword", "hello"
But what I want to achieve is to detect if they write "hellooo", "vuulgarr", vulg4rr"
This is my base where I have the words stored:
badwords.js
var words = ["vulgar", "vulg4r", "hello", "badword4", "badword5"]
module.exports = words;
This is my function that checks if a bad word comes on the way, split any words and then deletes the message if it finds a result, with indexOf()
index.js
const _ = require('lodash');
const badwords = require('./badwords');
/**
* Functions
*/
// compares every word to badWords array from badWords.js
function checkWord(word) {
return badwords.indexOf(word) > -1;
}
/**
* Main Module
*/
module.exports = function (self, nick, channel, message) {
'use strict';
message = message.toLowerCase();
message = message.split(' ');
nick = nick;
channel = channel.toLowerCase();
for (var i = 0, len = message.length; i < len; i++) {
if (checkWord(message[i])) {
self.send('.ban', channel, nick);
}
}
}
Any idea to improve it?, thank's
A more complicated method
We can have two pointers on both strings to compare, but skipping offsets upon duplicates:
A simpler method
Just scan the repetitions in a string and delete them first.
Then you can compare directly:
You can apply it like this: