Reputation: 105
I'm trying to toLowerCase, replace and then trim that replace so something like
Hello [- the re _- wo rl d.
will turn into a string with no spaces or punctuation like
hellothereworld
I'm using this
let msg = message.content.toLowerCase()
let originalMessage = msg.split(" ");
let removePunctuation = originalMessage.toString().replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
let checkMessage = removePunctuation.toString().trim();
then this to filter the message:
for (let i = 0; i < checkMessage.length; i++) {
if(allowed.includes(checkMessage[i])) {
continue;
}
if(curse.includes(checkMessage[i])) {
const filterViolation = new Discord.RichEmbed()
.setColor('#ff0000')
.setAuthor(message.author.username, message.author.avatarURL)
.setTitle('Filter Violation')
.setDescription(`${rule} **Profanity.** \n${ifcont}\n${ifmistake}`)
.setTimestamp()
.setFooter(copyright);
message.delete()
message.author.send(filterViolation)
message.channel.send(msgvio).then(msg => {msg.delete(15000)})
logger.write(logviolation)
}
//More code
}
Thanks to Tarazed for the help with the filter in general.
But when I type any message that goes against the filter, nothing happens and no errors are thrown in the console. Any ideas on what I did wrong?
New Code:
let checkMessage = message.content.toLowerCase().replace(/[^\w ]/g,"");
console.log(checkMessage);
// let checkMessage = msg.split(" ");
for (let i = 0; i < checkMessage.length; i++) {
if(checkMessage.includes(allowed[i])) {
continue;
}
//--------------------------------------- CURSE
if(checkMessage.includes(curse[i])) {
const filterViolation = new Discord.RichEmbed()
.setColor('#ff0000')
.setAuthor(message.author.username, message.author.avatarURL)
.setTitle('Filter Violation')
.setDescription(`${rule} **Profanity.** \n${ifcont}\n${ifmistake}`)
.setTimestamp()
.setFooter(copyright);
message.delete()
message.author.send(filterViolation)
message.channel.send(msgvio).then(msg => {msg.delete(15000)})
logger.write(logviolation)
}
}
It logs the check message correctly console.log(checkMessage);
but it doesn't go through the filter, neither allowed or unallowed.
It logs the message correctly but the word violates the filter, but nothing is done.
New Code 2:
if(curse.some(word => checkMessage.includes(word) && !allowed.some(allow => allow.includes(word) && checkMessage.includes(allow)))) {
const filterViolation = new Discord.RichEmbed()
.setColor('#ff0000')
.setAuthor(message.author.username, message.author.avatarURL)
.setTitle('Filter Violation')
.setDescription(`${rule} **Profanity.** \n${ifcont}\n${ifmistake}`)
.setTimestamp()
.setFooter(copyright);
message.delete()
message.author.send(filterViolation)
message.channel.send(msgvio).then(msg => {msg.delete(15000)})
logger.write(logviolation)
return;
}
Upvotes: 2
Views: 233
Reputation: 11080
Your current problem is that you're iterating over literally just a string like "test"
and checking if each letter is a curse word.
That is, checkMessage[0]
will be 't'
, checkMessage[1]
will be 'e'
, etc. I'm guessing no single character will match anything in your curse
or even allowed
array.
You can get rid of the loop entirely and simply check if(curse.includes(checkMessage))
, the entire message... but be wary this may easily return false positives. Heaven forbid someone sends a message like "it was a nice pic until i looked closer"
and pic until
put together triggers a certain c-word in your filter.
Anyway, I'd like to also point out that your code for stripping out spaces and punctuation does some rather strange things. I'll comment what I expect to be happening at each stage. Assume an input message of " Hello - world. "
let originalMessage = msg //" hello - world. "
.split(" "); //["", "hello", "-", "world.", ""]
let removePunctuation = originalMessage
.toString() //",hello,-,world.," (why even split?)
.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,""); //"helloworld"
let checkMessage = removePunctuation
.toString() //"helloworld" (does nothing ever)
.trim(); //"helloworld" (does nothing ever)
You can achieve the same thing very easily by using the regex class \W
which matches all non-word characters:
let checkMessage = message.content.toLowerCase().replace(/\W/g,""); //"helloworld"
It looks like there's still issues related to the loop. I suggest using Array#some
to test if the message contains any curse words:
let checkMessage = message.content.toLowerCase().replace(/[^\w ]/g,"");
if(curse.some(word => //search through the curse words
checkMessage.includes(word) && //if message has this curse word
!allowed.some(allow => //and there's no allowed word which:
allow.includes(word) && //1. contains this curse word
checkMessage.includes(allow) //2. is in the message
)
)) {
//then send violation
}
Upvotes: 3
Reputation: 7305
Since checkMessage
is a string, your for
loop is looping over and testing individual characters.
For example:
let checkMessage = "hellothereworld";
for (let i = 0; i < checkMessage.length; i++) {
console.log(checkMessage[i]);
}
Perhaps you'd rather keep the spaces in your string and operate on individual words:
let originalMessage = "Hello [-the world"
// Use regex that keeps spaces
let removePunctuation = originalMessage.toString().replace(/[^\w ]/g,"");
let checkMessage = removePunctuation.toString().trim();
// Split into words
let messageArray = checkMessage.split(' ');
// Loop over words
for (let i = 0; i < messageArray.length; i++) {
console.log(messageArray[i]);
}
Upvotes: 1