首页 > 解决方案 > Discord.js - 如果消息包含,不区分大小写

问题描述

我想用我的 Discord Bot 阻止脏话。因此,我创建了一个我想要阻止的 JSON 文件并将其命名为swear_words. 作为测试,我添加了 sware 进行测试。bot.js然后我将它包含在我的文件顶部:

const { swear_words, mute_content } = require(`./swear-words.json`);

然后是我的阻塞代码,在底部:

client.on(`message`, message => {
    for (var i=0; i < swear_words.length; i++) {
        if (message.content.includes(swear_words[i])) {
            message.delete();
            message.channel.send(mute_content);
            console.log("Someone tried to swear.");
        }
    }
});

这在我键入时有效sware,但不是Sware。所以我补充swear_words.prototype.toLowerCase()说:

if (message.content.includes(swear_words.prototype.toLowerCase() === swear_words[i])) {}

然后我得到:

TypeError: Cannot read property 'toLowerCase' of undefined

这是什么?它不能读取我的 JSON 吗?

提前感谢您的帮助。

标签: javascriptdiscord.js

解决方案


用于.toLowerCase( )消息内容,因为它返回一个字符串。

if (message.content.toLowerCase( ).includes(swear_words[i])) {

推荐阅读