javascript - 仅当 author.name 的内容是准确的或剩余的单词时才有效
问题描述
机器人只有在内容author.name
准确时才会做出反应,但如果它有一个额外的单词它就不起作用,但它不会抛出错误示例:
const ListClaims = ["rick sanchez", "alex", "juan"];
rick sanchez
没有错误,因为它是准确的ListClaims
rick sanchez morty
有一个错误,因为它有额外的字母morty
,
var ListClaims = ["rick sanchez","alex","juan"];
if(message.embeds.length >= 0)
// Check if the Message has embed or not
{
let embed = message.embeds
// console.log(embed) just a console.log
for(let i = 0; i < embed.length; i++)
{
if (!embed[i] || !embed[i].author || embed[i].author.name === null) return;
// check each embed if it has setAuthor or not, if it doesnt then do nothing
{
if(embed[i].author.name.toLowerCase().includes(ListClaims))
// check each embed if it includes word
{
message.react('')
}
}
}
}
解决方案
我已经重新格式化了你的代码,所以它更简单,更容易阅读。如果您给出一个更好的例子来说明您想要发生的事情,我可以编辑这个答案以更好地解决它。
function messageHandler() {
const msg = {
embeds: [
{ author: { name: "alex" }},
{ author: { name: "john"}},
{ author: { name: "rick sanchez morty"}},
]
}
const listClaims = ["rick sanchez", "alex", "juan"];
// 0 is falsey by default, so you dont have to check if it's == 0.
if (!msg.embeds.length) return;
// Check if the Message has embed or not
msg.embeds.forEach(embed => {
// "", null and undefined are also falsey, so we don't need to check them.
// also this if statement is not needed, since you can just do Array.includes().
if (!embed.author.name) return;
// .includes() is an array method, not a string method, so you have to do Array.includes(target), not target.includes(array).
if (listClaims.includes(embed.author.name.toLowerCase())) message.react('');
});
}
const message = {
react: console.log
};
messageHandler()
推荐阅读
- python - 如何使用 Python 从预定义的 C++ 函数中获取变量的值?
- python - 在 seaborn 中恢复默认颜色代码
- angular - Angular Alias Import 找不到识别定义的文件夹
- python - MPI - Python Anaconda 中的 HelloWorld 程序
- ios - 错误域=CBATTErrorDomain 代码=2“不允许读取。” UserInfo={NSLocalizedDescription=不允许读取。}
- git - “git stash”后无法恢复本地更改
- reactjs - 为什么我在使用 redux 时收到 TypeError: Object(...) is not a function 错误?
- sonarqube - SonarQube Jacoco 覆盖率报告 (.ec & xml) 报告未一起显示
- vue.js - 在 vue js mdb footer get break 如何解决这个问题?
- button - 响应多次鼠标点击以产生不同的输出 - Tkinter