How to make discord.js 11 not case sensitive?

折月煮酒 提交于 2021-02-11 14:19:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!