Javascript 你如何在不和谐的信息中找到一个词?

Javascript 你如何在不和谐的信息中找到一个词?,javascript,discord,discord.js,Javascript,Discord,Discord.js,所以我已经尽可能多地查阅了关于这个主题的信息,但似乎没有什么是我想要的。现在我正在学习编写自己的Discord机器人。我已经到达了一个临界点,我试图在一条信息中检测一个特定的词,比如亵渎过滤器 我找到答案的唯一方法是使用“message.content.includes()”,但这会在单词中找到模式,比如如果我试图删除单词“rot”,它也会删除“carrot” 我还知道排除“.includes()”意味着即使这个词在句子后面说,它也不会检测到它,因为它只会查找消息本身 我的目标是有一个代码,如果

所以我已经尽可能多地查阅了关于这个主题的信息,但似乎没有什么是我想要的。现在我正在学习编写自己的Discord机器人。我已经到达了一个临界点,我试图在一条信息中检测一个特定的词,比如亵渎过滤器

我找到答案的唯一方法是使用“message.content.includes()”,但这会在单词中找到模式,比如如果我试图删除单词“rot”,它也会删除“carrot”

我还知道排除“.includes()”意味着即使这个词在句子后面说,它也不会检测到它,因为它只会查找消息本身

我的目标是有一个代码,如果我想让它删除“rot”,它应该只删除它,如果它是一个单独的词在消息中的任何地方

多谢各位

编辑:我粘贴了当前代码:

const profanities = require('profanities/index.json');

for (let x = 0; x < profanities.length; x++) {
    if (message.content.toUpperCase().includes(profanities[x].toUpperCase())) {
        message.channel.send('Oooooooh you said a bad word!');
        client.channels.get('484375912389935126').send(`Message was deleted due to use of a blocked word:\n\n"${message.content}"`);
        message.delete();
        return;
    }
}
const profanities=require('profanities/index.json');
for(设x=0;x<亵渎。长度;x++){
if(message.content.toUpperCase().includes(亵渎[x].toUpperCase())){
message.channel.send('ooooooh你说了一句脏话!');
client.channels.get('484375912389935126').send(`Message由于使用了阻止字而被删除:\n\n“${Message.content}`);
message.delete();
返回;
}
}
编辑: 如果你是这个意思的话,恐怕不可能只从邮件中删除某个单词。但是,如果你试图删除包含一个单词的整个消息,我可以理解你在做什么。试试这个:

let checkMessage = message.content.toLowerCase(); // Puts the whole message to lowercase. If your profanities file is all caps change it to .toUpperCase(); but if it is all lowercase keep it the same.
for (let i = 0; i < checkMessage.length; i++) {
    let logchannel = message.guild.channels.find(x => x.name === `{name}`); // Name of the channel you want the logs to go to, this will help if you want to change the name and not grab the ID of the channel.
    const profanities = require('profanities/index.json'); // Make sure this is the correct file path of the profanities json.
    if(profanities.some(word => checkMessage.includes(word)) { // If the message includes any word from profanities.
        message.delete(); // Delete the message that was sent.
        message.channel.send(`Oooooooh you said a bad word!`).then(msg => {msg.delete(15000)}); // Send the channel a message saying that they said a bad word. Then delete the message after 15 seconds.
        client.channels.get(logchannel.id).send(`Message was deleted due to use of a blocked word:\n\n${message.content}`); //Send the log channel a message saying that there was a blocked word.
        return;
    }
让checkMessage=message.content.toLowerCase();//将整个消息设置为小写。如果您的亵渎文件是全部大写,则将其更改为.toUpperCase();但如果都是小写,则保持不变。
for(设i=0;ix.name===`{name}`);//您希望日志转到的频道的名称,如果您希望更改名称而不获取频道的ID,这将很有帮助。
const profanities=require('profanities/index.json');//确保这是profanities json的正确文件路径。
if(亵渎。some(word=>checkMessage.includes(word)){//如果消息包含亵渎的任何单词。
message.delete();//删除已发送的邮件。
message.channel.send(`ooooooh you said a bad word!`)。然后(msg=>{msg.delete(15000)});//向频道发送一条消息,说他们说了一个bad word。然后在15秒后删除该消息。
client.channels.get(logchannel.id).send(`Message由于使用了被阻止的单词而被删除:\n\n${Message.content}`);//向日志通道发送一条消息,说明存在被阻止的单词。
返回;
}

我建议将句子分成一个数组,查找特定的单词,然后在删除单词后加入后面的内容

var-station=“兔子吃了胡萝卜腐烂”;
功能特定(word){
返回语句.split(“”)。过滤器(each=>each!==word)。join(“”);
}
var res=特定于删除(“rot”);
console.log(res);
“兔子吃了胡萝卜腐烂”。替换(/(?)?

提供一个想法,使用正则表达式,比如这里的规则,
rot
在非字母存在之前和之后删除
rot

我很高兴不仅仅是我在一个问题上获得了选票。我在上面发布了我当前的代码,以便更好地澄清。我希望使用一些相关的东西。@QuazArxx我编辑了我的帖子,我希望底部的代码能帮助你。我用当前的代码编辑了我的原始帖子。我希望在那里使用一些东西,这样对我来说就不那么复杂了。@QuazArxx,我相信你的消息是一个句子,对吧,亵渎是被屏蔽的关键词?
let checkMessage = message.content.toLowerCase(); // Puts the whole message to lowercase. If your profanities file is all caps change it to .toUpperCase(); but if it is all lowercase keep it the same.
for (let i = 0; i < checkMessage.length; i++) {
    let logchannel = message.guild.channels.find(x => x.name === `{name}`); // Name of the channel you want the logs to go to, this will help if you want to change the name and not grab the ID of the channel.
    const profanities = require('profanities/index.json'); // Make sure this is the correct file path of the profanities json.
    if(profanities.some(word => checkMessage.includes(word)) { // If the message includes any word from profanities.
        message.delete(); // Delete the message that was sent.
        message.channel.send(`Oooooooh you said a bad word!`).then(msg => {msg.delete(15000)}); // Send the channel a message saying that they said a bad word. Then delete the message after 15 seconds.
        client.channels.get(logchannel.id).send(`Message was deleted due to use of a blocked word:\n\n${message.content}`); //Send the log channel a message saying that there was a blocked word.
        return;
    }
"rabbit ate the carrot rot".replace(/(?<![a-zA-Z])rot(?![A-Za-z])/,'')