亵渎过滤器

时间:2019-01-23 01:39:10

标签: node.js discord discord.js

伙计们。我试图做一个亵渎的过滤器。我已经有一个什么样子了:

for(x = 0; x < profanities.length; x++) {
  if(message.content.toUpperCase() == profanities[x].toUpperCase()) {
    message.channel.send('Hey! Don\'t say that!')
    message.delete();
    return;
  }
}

但是这会删除整个味精。我想做一个过滤掉亵渎的东西,我做到了:

for(x = 0; x < profanities.length; x++) {
var msg2 = message.content.toUpperCase().includes(profanities[x]).toUpperCase()
  if (msg2) {
    while (msg2) {
      censor(msg, profanities[x].toUpperCase(), ';)')
    }
    message.channel.send(msg)
    message.delete();
    return;
  }
}

各种乐趣可能很重要:

var msg = message.content.toUpperCase();

function censor(text, toChange, newWord) {


var toReturn = text;
while (toReturn.includes(toChange)) {
    toReturn = toReturn.replace(toChange, newWord);
}


return toReturn;
}`var profanities = require('profanities');`

2 个答案:

答案 0 :(得分:3)

不可能,您不能通过Discord机器人编辑用户消息。您可以删除该消息,然后通过机器人自动发送该消息

答案 1 :(得分:0)

亵渎过滤器实际上很容易创建。只需使用此代码!

const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'XXXXXXXXX'; //Token (DO NOT SHARE WITH OTHERS!!!)

client.login(token);

client.on('message', message => {

    var msg = message.content.toUpperCase();

    // Profanity Filter //
    if(msg.includes('PROFAIN WORD HERE')) { //Checks message for profanity
        message.delete(); //Deletes the message
        message.author.send(message.author + ', PLEASE DO NOT USE PROFANITY. YOU WILL BE TEMPORARILY MUTED ASAP.') //Sends warning to user in DM
    }
});

如果您希望代码正常工作,请始终将msg.includes词大写。单词或短语中的小写字母将使代码无法正常工作。