javascript - 检查 Discord 消息中的特定单词
问题描述
我试图弄清楚如何从整条消息中获取特定单词。就像使用亵渎过滤器一样。
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;
}
}
现在这可行,除了如果在另一个词中说出一个词,它也会找到它,因为 .includes 就像如果我要阻止“bum”而有人说“bumble”,它也会删除“bumble”。这对于亵渎过滤器来说很好,但我想为会员做一个有趣的过滤器:
const words = message.content.toLowerCase();
if (words.includes('bum')) {
setTimeout(function() {
message.channel.send('Are we talking about <@memberID>?!');
}, 1500);
}
我使用“memberID”而不是真实 ID。但这会在“bumble”中找到“bum”,但我只希望它在消息中找到“bum”作为单独的词。就像“这个流浪汉很奇怪”之类的。
解决方案
就像其他答案所说,使用正则表达式:
if (/\bbum\b/i.test(message.content)) {
setTimeout(function() {
message.channel.send('Are we talking about <@memberID>?!');
}, 1500)
}
\b
检查单词边界并使其i
不区分大小写。
推荐阅读
- c# - OrderBy 和 OrderByDescending 查询存储为字符串的小数
- google-tag-manager - 在 Google 跟踪代码管理器中无法跟踪事件
- java - Spring - 参考 Java bean 在 XML 中创建 bean,反之亦然
- python - Keras:使用 U-net 架构进行多标签分类,以实现有效的背景/前景图像分割
- reactjs - 导入组件时,SCSS 文件的赛普拉斯问题
- sql - Excel 中的 SQL 子查询参数
- angular - 当我想导出 excel 文件时,自动过滤器在 Angular 应用程序中不起作用
- sails.js - 如何在sails js中使用填充子标准(例如:.populate('something',{select:[]}))
- android - 验证电子邮件在 Android 上无法正常工作
- ios - 你可以强制 ios 启动 safari 而不是任何其他浏览器,甚至提供一个选项吗?