【发布时间】:2019-04-02 22:19:27
【问题描述】:
我正在制作一个 discord.js 机器人,我很好奇如何为其添加聊天过滤器。正如用户所说的f***,它会自动删除。
【问题讨论】:
标签: json discord discord.js
我正在制作一个 discord.js 机器人,我很好奇如何为其添加聊天过滤器。正如用户所说的f***,它会自动删除。
【问题讨论】:
标签: json discord discord.js
我创建了一个简单的方法,例如
let badwords = ['word1', 'word2']
client.on('message', async (message) => {
if (badwords.includes(message.content.toLowerCase())) {
message.delete();
message.channel.send(`HEY! you cant send that here`);
}
})
这是我用于我的机器人的过滤器。它首先将消息转换为小写,然后在数组中搜索单词。如果它发现单词与小写消息中的单词匹配,则删除该消息。顺便说一句,数组中的单词必须小写才能起作用。
【讨论】:
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
}
});
告诉我 如果一切正常。如果您有任何问题,请在此处联系/回答我。
【讨论】:
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()
});
如果我在哪里搞砸了,请告诉我。我就是这样做的。 (未测试)
【讨论】:
这是一个保证工作的聊天过滤器!
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!")
}
});
【讨论】:
你可以用一组单词来做到这一点,
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;
}
}
});
编辑: 这是非常基本的,它不会寻找替代字母,例如 $、@ 或数字/空格,除非您直接在其中编码,您可以拥有一个单词列表,然后让控制台记录每个带有替代字母的单词。
【讨论】:
client.on('message', message => {
message.delete(message.content.replace(/asshole/gi))
.catch(console.error);
});
这只是一个你可以做的例子,还有一个文本数组。
【讨论】: