我正在制作discord.js机器人,我很好奇如何向其添加聊天过滤器。如用户所说的f***
,它将自动删除。
答案 0 :(得分:1)
const blacklisted = ["word1", "word2", "word3"] // and continue doing this until you have entered all your desired words
client.on("message", message => {
let shouldDelete = false
for(word in blacklisted){
if(message.content.includes(word)) shouldDelete = true
}
if(shouldDelete) message.delete()
});
如果我在任何地方搞砸了,请告诉我。这就是我要做的。 (未经测试)
答案 1 :(得分:0)
您可以使用一系列单词来完成此操作,
var profanities = ["test1", "test2", "..."];
然后在您的bot.on消息处理程序中,或者您如何使用它来处理消息,
bot.on('message', async message => {
let msg = message.content.toLowerCase();
let Admin = message.guild.roles.find('name', "Admin"); // Change this to meet what you want
let General = message.guild.channels.find(`name`, "general"); // All text channels are lowecase. Change this to meet what you want
for (x = 0; x < profanities.length; x++) {
if(message.member.roles.has(Admin.id) || message.channel.id === General.id) return; // if you changed the name of the variables above, change these too.
if (msg.includes(profanities[x])){
await message.reply("You cannot say that here!")
message.delete()
// Your code here
return;
}
}
});
编辑: 这是非常基本的操作,除非您直接在其中进行编码,否则它不会查找$,@或数字/空格之类的替代字母,您可以列出单词列表,然后在控制台中记录每个带有替代字母的单词。>
答案 2 :(得分:0)
这是一个可以正常工作的ChatFilter!
bot.on("message", async message => {
let blacklisted = ['yourbadword1', 'yourbadword2', 'and some more bad words :3'];
let foundInText = false;
for (var i in blacklisted) {
if(message.content.toLowerCase().includes(blacklisted[i].toLowerCase())) foundInText = true;
}
if(foundInText) {
message.delete();
message.channel.send("Stop using this bad word!")
}
});
答案 3 :(得分:0)
bot.on("message", async message => {
//Define the words that should be deleted after a successfull detection
let blacklisted2 = ['Some', 'Random', 'word'];
//Set foundInText to false
let foundInText2 = false;
//Check for the blacklisted words in blacklisted2
for (var i in blacklisted2) {
if (message.content.toLowerCase().includes(blacklisted2[i].toLowerCase()))
//If a blacklisted word has been detected in any sentences, it's going to set foundInText2 to true.
foundInText2 = true;
}
//If something has been detected, delete the message that has a blacklisted word inside of it.
if (foundInText2) {
//Delete the message.
message.delete();
//Then do whatever you want
}
});
告诉我 如果一切正常。如果您有任何问题,请在此处联系/回答我。
答案 4 :(得分:-1)
client.on('message', message => {
message.delete(message.content.replace(/asshole/gi))
.catch(console.error);
});
这只是一个示例,您也可以使用文本数组。