discord.js - 如果允许某个变量,有没有办法覆盖“列入黑名单”的单词?
问题描述
const BannedLinks = ["https://", "http://", "www.", ".com", ".net", ".org", ".tv", ".xyz", ".blog"]
if(BannedLinks.some(word => message.content.includes(word)) ) {
message.delete();
message.reply("please dont post links, take that to DM's").then(m => m.delete(3000));
}
这是我的 Discord 机器人的反链接代码,但我想允许 Twitch.tv 和 cdn.discordapp.com 之类的链接,但我正在努力寻找正确的功能。
解决方案
你可以使用array.prototype.some
方法。
不和谐的解决方案
client.on('message', message => {
const whiteListLinks = ['Twitch', 'Youtube']
const blackListLinks = ['https', 'http']
let isBlacklisted = blackListLinks.some(checkInclude) && !whiteListLinks.some(checkInclude)
if (isBlacklisted) message.delete()
function checkInclude(element, index, array) {
return message.content.toUpperCase().includes(element.toUpperCase());
}
});
在线片段
const message = {
content: 'https:Twitch'
}
const whiteListLinks = ['Twitch', 'Youtube']
const blackListLinks = ['https', 'http']
let isBlacklisted = blackListLinks.some(checkInclude) && !whiteListLinks.some(checkInclude)
console.log(isBlacklisted)
function checkInclude(element, index, array) {
return message.content.toUpperCase().includes(element.toUpperCase());
}
推荐阅读
- mysql - MySQL INSERT INTO 跳过列的可空性
- asp.net - 当 ASP.NET Core MVC 中存在带有星号模板的路由 {*url} 时,不会触发其他控制器
- powershell - 提交 CSR 并使用 Get-Certificate 获取新证书
- javascript - 为什么 input[type="number"] 将十进制值插入为“.00”?
- typescript - 如何使对象的属性成为 Typescript 中的类型?
- linux - bash脚本(Makefile)嵌套for循环中的文件错误意外结束
- database - 如何让 DynamoDB 自创建数据后每小时删除一次数据?
- javascript - 对象引用给出未定义
- javascript - 反应代码旁边的 jQuery 代码示例(菜鸟)
- python - 是否可以忽略对一行的 pyright 检查?