问题
I have an anti swear made in discord js 11 and it uses a file called bannedwords.txt. Is there a way I can make it read the file without being case sensitive. For instance if one of the banned words was chicken, I wanna make it so it bans CHICKen, CHicken, Chicken, chicken, ChiCKeN, you get the point. How do I do this?
回答1:
You can use the toLowerCase()
method of strings to achieve this. Simply call it on both the message being checked for swearing, and/or on the banned word it is being checked for. Here's an example:
checkProfanity: function(message, bannedWords) {
var words = message.split(' ');
for (var word of words) {
if (bannedWords.indexOf(word.toLowerCase()) > -1) return true;
}
return false;
}
This is the modification necessary in your punishment.js file to make your filter case insensitive. Since all of the banned words in your bannedwords.txt
file are lowercase, we can simply convert each individual word of the message to lowercase to make the comparison between the word and banned word possible.
来源:https://stackoverflow.com/questions/65259080/how-to-make-discord-js-11-not-case-sensitive