【发布时间】:2020-06-12 00:25:30
【问题描述】:
我正在尝试 toLowerCase,替换然后修剪该替换,例如
Hello [- the re _- wo rl d.
将变成一个没有空格或标点符号的字符串
hellothereworld
我正在使用这个
let msg = message.content.toLowerCase()
let originalMessage = msg.split(" ");
let removePunctuation = originalMessage.toString().replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
let checkMessage = removePunctuation.toString().trim();
然后这个来过滤消息:
for (let i = 0; i < checkMessage.length; i++) {
if(allowed.includes(checkMessage[i])) {
continue;
}
if(curse.includes(checkMessage[i])) {
const filterViolation = new Discord.RichEmbed()
.setColor('#ff0000')
.setAuthor(message.author.username, message.author.avatarURL)
.setTitle('Filter Violation')
.setDescription(`${rule} **Profanity.** \n${ifcont}\n${ifmistake}`)
.setTimestamp()
.setFooter(copyright);
message.delete()
message.author.send(filterViolation)
message.channel.send(msgvio).then(msg => {msg.delete(15000)})
logger.write(logviolation)
}
//More code
}
感谢 Tarazed 对过滤器的总体帮助。
但是当我输入任何违反过滤器的消息时,什么都不会发生,控制台中也不会抛出任何错误。关于我做错了什么有什么想法吗?
新代码:
let checkMessage = message.content.toLowerCase().replace(/[^\w ]/g,"");
console.log(checkMessage);
// let checkMessage = msg.split(" ");
for (let i = 0; i < checkMessage.length; i++) {
if(checkMessage.includes(allowed[i])) {
continue;
}
//--------------------------------------- CURSE
if(checkMessage.includes(curse[i])) {
const filterViolation = new Discord.RichEmbed()
.setColor('#ff0000')
.setAuthor(message.author.username, message.author.avatarURL)
.setTitle('Filter Violation')
.setDescription(`${rule} **Profanity.** \n${ifcont}\n${ifmistake}`)
.setTimestamp()
.setFooter(copyright);
message.delete()
message.author.send(filterViolation)
message.channel.send(msgvio).then(msg => {msg.delete(15000)})
logger.write(logviolation)
}
}
它正确记录了检查消息console.log(checkMessage);,但它没有通过过滤器,既不允许也不允许。
它正确记录了消息,但单词违反了过滤器,但什么也没做。
新代码 2:
if(curse.some(word => checkMessage.includes(word) && !allowed.some(allow => allow.includes(word) && checkMessage.includes(allow)))) {
const filterViolation = new Discord.RichEmbed()
.setColor('#ff0000')
.setAuthor(message.author.username, message.author.avatarURL)
.setTitle('Filter Violation')
.setDescription(`${rule} **Profanity.** \n${ifcont}\n${ifmistake}`)
.setTimestamp()
.setFooter(copyright);
message.delete()
message.author.send(filterViolation)
message.channel.send(msgvio).then(msg => {msg.delete(15000)})
logger.write(logviolation)
return;
}
【问题讨论】:
-
您是否在任何时候登录
checkMessage以确保它符合您的期望...? -
是的,我是,当我输入“t - e st”时,它会记录“test”
-
这是您所期待的吗?正如我现在看到的那样,您循环遍历每个字符(在
checkMessage中)以检查它是否在允许的字符中,但是如果“坏”字符已经被过滤掉了,那又有什么意义呢? -
您可以简化正则表达式以将非字母数字字符替换为
/[\W]/。 -
@PrinceBunBun981 您没有告诉它要替换什么,该代码只会将第一个字符替换为
undefined- 请参阅我的答案以了解正确用法
标签: javascript discord.js